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

Kevin Old kevinold at gmail.com
Sun Aug 3 21:51:54 BST 2008


Well, after playing with this for days and not getting anywhere I
assume it can't be done gracefully.

In order to "prime the cache" I built a script using LWP::Simple to
visit the urls that need caching, thus achieving my ultimate result.

Hope this helps someone.

On Sat, Aug 2, 2008 at 1:24 PM, Kevin Old <kevinold at gmail.com> wrote:
> 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
>



-- 
Kevin Old
kevin at kevinold.com



More information about the Catalyst mailing list