in reply to Re: Catalyst fcgi engine w. persistent variables
in thread Catalyst fcgi engine w. persistent variables

thanks for the reply, it may be an XY problem :)

Basically I want some data structures to persist in memory between requests. I don't want to write them to a file, a db, memcached etc, because that's somewhat slower and memory will work just fine.

Ideally they'd expire after so long and then I'll reupdate them when I see they've expired. It doesn't matter to me if a module handles the expiration or I do it myself from a time stamp in the structure. The data itself doesn't literally need to be global everywhere, if I can get it from the context that's fine.

Something like Catalyst::Plugin::Cache with Cache::Memory as a backend would work, but from my experimenting it doesn't look like the cache persists between requests :/. Perhaps I am no utilizing it correctly however.

In my application class I merely do

__PACKAGE__->config->{'Plugin::Cache'}{backend} = { + class => "Cache::Memory", default_expires => '600 sec' + };

Then access it via $cache = $c->cache; in my controller. But the data isn't persisting between requests alas

thank you again

Replies are listed 'Best First'.
Re^3: Catalyst fcgi engine w. persistent variables
by Your Mother (Archbishop) on Jul 07, 2011 at 21:20 UTC

    Yeah, I didn't check in before just trying this so I missed that you'd solved it. I'll leave some code for future readers. The docs are thin so I'm not surprised you didn't get it right the first time.

    # in MyApp.pm __PACKAGE__->config( "Plugin::Cache" => { backend => { class => "Cache::MemoryCache", } }); # in MyApp::Controller::Root sub counter :Local Args(0) { my ( $self, $c ) = @_; my $counter = $c->cache->get("counter") + 1; $c->cache->set( counter => $counter ); $c->response->body("Count #" . $counter); }

    Sounds like you already figured out the other parts like expiration. Have fun and realize these are volatile and if I read the docs right they share data or don't depending on the engine the app is running.

Re^3: Catalyst fcgi engine w. persistent variables
by damian45 (Novice) on Jul 07, 2011 at 19:24 UTC
    Okay looks like the Cache modules I mentioned previously do indeed work, I typoed the timeout in my code, so now it appears to persist between requests. Thanks again, and if there's any smarter way to do this I'd be curious to know.
    cheers