vit has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I have a few questions regarding Cache::Cache, which I did not find an answer for anywhere including this forum.
1) Can I use this module in order to store my hash between sessions
2) In the CPAN tutorial I found an example of usage. What are $name and $customer there.
use Cache::FileCache; my $cache = new Cache::FileCache( ); my $customer = $cache->get( $name ); if ( not defined $customer ) { $customer = get_customer_from_db( $name ); $cache->set( $name, $customer, "10 minutes" ); } return $customer;
3) Can somebody who has a similar task give me an example of working usage.

Replies are listed 'Best First'.
Re: Cache::Cache questions
by moritz (Cardinal) on Jun 30, 2009 at 16:56 UTC
    1) Can I use this module in order to store my hash between sessions

    It might mostly work, but it's not at all advisable. Caches are there to speed up lookups that would otherwise take longer, but they are not intended to keep always all information. (It's not the primary goal to avoid data loss, as opposed to "real" storage solutions).

    for sessions there's CGI::Session or Storable, depending on the abstraction level you want.

    In the CPAN tutorial I found an example of usage. What are $name and $customer there.

    $customer is a data structure to be cached, and $name is a name uniquely identifying that structure.

    3) Can somebody who has a similar task give me an example of working usage.

    Most CPAN modules come with test files which are also working examples. You can find them by clicking on the MANIFEST link on the distribution overview page, for Cache::Cache it's here.

      /*
      for sessions there's CGI::Session or Storable, depending on the abstraction level you want.
      */
      I do use Starable but simply for storring compactly the hash in the file.
      Do you mean I can use use Storable to store my hash in memory between sessions? Give me please just a hint how.
      What do you mean by "abstraction level you want".
        Do you mean I can use use Storable to store my hash in memory between sessions?

        No, but in a file.

        What do you mean by "abstraction level you want".

        Storable's interface is roughly "store $this data structure in $that file", or the other way round. That means it's rather low-level. For example if you have two processes, you have to take care yourself that they don't simultaneously write to the file, and completely mess it up in the process.

        CGI::Session is much more high-level; it's interface is more like "given $this HTTP header, give me an object that I stored previously in the same session". That means it takes care of storage location, locking and so on for you.

        What you need really depends on what you want to achieve in the end, and you haven't told us that.

Re: Cache::Cache questions
by Your Mother (Archbishop) on Jun 30, 2009 at 16:58 UTC

    1) Sure, but realize that a cache is meant to be transitory. It's not where you should be keeping anything you really care about like an order or something where losing cache data might mean losing money. Some of the caches engines are less likely than others to dump data on their own. IIRC, FileCache is one of the ones that is not going to do it.

    2) Of course.

    3) $user is just going to be something like "Egil Kveldulfr." Or better, a user id, like 1005. It's just a unique key (or it's not safe because a user might see another user's cache data) for the user. The $customer is just some theoretical data object, like a DBIx::Class object of a customer account and its related data like orders.

    That is a working example you gave, I think. You just need to fill in the data. I'd try something more simple to start... Oh, I see mortiz just replied. His advice is good: play with the test files.