[Catalyst] Access to $c->cache from a script

Kevin Old kevinold at gmail.com
Sat Aug 2 18:24:00 BST 2008


Hi everyone,

My config for Catalyst::Plugin::Cache is and works well within controllers:

<cache>
    <backend>
        cache_root   /tmp
        default_expires_in   1d
        namespace   AW
        class   Cache::FileCache
    </backend>
</cache>

I'm writing a script to "prime the cache" which calculates data and
inserts them into the cache for the controller to find later.

The problem is, when I instantiate the cache in the script below, and
store the data, my catalyst app doesn't find the content.

Is there a better way?

use FindBin;
use Config::Any::General;
use lib "$FindBin::Bin/../lib";
my $cfg = Config::Any::General->load("$FindBin::Bin/../aw.conf") || die $!;

sub search {
    my ($self, $term, $cache) = @_;

    my %params = %{$cfg->{cache}{backend}};
    my $class  = delete $params{class};

    eval("use $class;");    ## no critic (ProhibitStringyEval)
    unless ($cache) {
        $cache = $class->new(\%params);
    }

    my $cachekey = $term;
    $cachekey =~ s/ /_/g;

    my $records;
    unless ($records = $cache->get($cachekey)) {
          # perform search
          $cache->set($cachekey, $records);
    }

    return $records;
}


When I call the search in my controller I pass it $c->cache:

my $records = Search->new->search("$term", $c->cache);


When I dump the $cache object when run from Catalyst I see it is a
Catalyst::Plugin::Cache::Curried object, but the following dump is
when I run it from a script and $cache is made up from my code inside
search():

$VAR1 = bless( {
                 '_Backend' => bless( {
                                        '_Directory_Umask' => 0,
                                        '_Root' => '/tmp',
                                        '_Depth' => 3
                                      }, 'Cache::FileBackend' ),
                 '_Auto_Purge_On_Set' => 0,
                 '_Default_Expires_In' => '1d',
                 '_Options_Hash_Ref' => {
                                          'cache_root' => '/tmp',
                                          'default_expires_in' => '1d',
                                          'namespace' => 'AW'
                                        },
                 '_Auto_Purge_On_Get' => 0,
                 '_Namespace' => 'AW'
               }, 'Cache::FileCache' );

I've got to be doing something wrong, but keep going around in circles.

Any ideas?

Thanks,
Kevin



More information about the Catalyst mailing list