Disabling Caching

29 views
Skip to first unread message

bluefeet

unread,
Dec 28, 2009, 11:04:45 AM12/28/09
to Perl-Cache Discuss
Hi all,

I've developed a web application that uses CHI to cache data in
memcached and local file caches. When developing I often like to
disable caching and I do this by using CHI::Driver::Null. But, I've
found that this is too global and doesn't actually fit my needs. I
need a way to tell CHI to stop caching within a smaller scope. Being
able to do something like:

$chi->disable();

$chi->enable();

Anyone have any thoughts? I was thinking about making a Moose Role
the overrides fetch and returns undef, when caching is turned off, but
that only works for the first cache level, not any l1_caches/
sub_caches. I'd have to apply this role to all the caches for it to
work, and I'd have to have some way for the children caches to walk
their way up to the main parent cache to find out whether caching is
disabled. Sound funky and painful, but doable. It feels like a hack
too. Anyone have a better way of doing this?

Thanks much,

Aran

Jonathan Swartz

unread,
Dec 28, 2009, 11:25:44 AM12/28/09
to Perl-Cache Discuss
Hi Aran,

Are you wanting to disable caching temporarily for a particular cache
object, or for all cache objects created anywhere in your application?
e.g. in your example is $chi a cache object returned by CHI->new?

Jon

bluefeet

unread,
Dec 28, 2009, 2:57:36 PM12/28/09
to Perl-Cache Discuss
Either way would work, actually. I'm thinking globally, but I don't
create more than one cache object and I almost always have access to
it, so doing it per-chi object would be fine too.

I wonder - another solution might be to create a new driver that is a
gate-keeper of sorts. It is aware of whether caching is enabled or
not, if its not it never passes control to subcaches, otherwise it
always passes control on to the subcaches. From what I remember of
the driver architecture this would be pretty doable.

Aran

Jonathan Swartz

unread,
Dec 29, 2009, 10:02:04 AM12/29/09
to perl-cach...@googlegroups.com
Do you have your own CHI subclass* that you create objects with (i.e.
MyApp::CHI->new)? If so you could have a switch that automatically
switches to a Null cache:

sub new {
my $class = shift;
if ($CACHING_DISABLED) {
return $class->SUPER::new(driver => 'Null');
}
else {
return $class->SUPER::new(@_);
}
}

The new() will be called for main caches and subcaches, so it should
catch everything. Or, you could apply your role here.

Creating a private CHI subclass is a good practice in general because
it gives you the flexibility to do these sorts of things. It's
something I want to start encouraging in the documentation ala Rose::DB.

Jon

> --
>
> You received this message because you are subscribed to the Google
> Groups "Perl-Cache Discuss" group.
> To post to this group, send email to perl-cach...@googlegroups.com
> .
> To unsubscribe from this group, send email to perl-cache-disc...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/perl-cache-discuss?hl=en
> .
>
>

Reply all
Reply to author
Forward
0 new messages