cache clearing from within a Pylons controller?

57 views
Skip to first unread message

Isaac Csandl

unread,
Sep 23, 2008, 11:34:09 AM9/23/08
to Mako Templates for Python
Hi,

I'm thinking of the common situation where a controller is updating an
object: it should be possible to manually pluck out the relevant defs
and/or <%page>'s to clear the now-invalid data. Anybody know how?

--i

Michael Bayer

unread,
Sep 23, 2008, 6:53:26 PM9/23/08
to mako-d...@googlegroups.com

its something I've been considering adding an API for. Currently it
would look something like (this is against latest trunk):

template = lookup.get_template("/path/to/my/template.mako")
cache = template.module._template_cache
cache._get_container(keyname, type="file").clear_value()

"keyname" is the name of the def ("body" for the whole page) or
whatever key you use for cache_key.

Isaac Csandl

unread,
Sep 23, 2008, 9:19:25 PM9/23/08
to Mako Templates for Python


On Sep 23, 5:53 pm, Michael Bayer <mike...@zzzcomputing.com> wrote:
> its something I've been considering adding an API for.   Currently it  
> would look something like (this is against latest trunk):
>

Consider it requested! I had a feeling you were already on top of it.

Thanks!

Michael Bayer

unread,
Sep 24, 2008, 10:46:51 PM9/24/08
to mako-d...@googlegroups.com

On Sep 23, 2008, at 9:19 PM, Isaac Csandl wrote:

>
>
>
> On Sep 23, 5:53 pm, Michael Bayer <mike...@zzzcomputing.com> wrote:
>> its something I've been considering adding an API for. Currently it
>> would look something like (this is against latest trunk):
>>
>
> Consider it requested! I had a feeling you were already on top of it.

maybe if you want to add a trac ticket to remind me. not sure when I
can get to this.

Michael Bayer

unread,
Sep 27, 2008, 10:56:38 AM9/27/08
to mako-d...@googlegroups.com
OK, check out this feature in http://www.makotemplates.org/trac/changeset/405
. You'd have to get at your Pylons-configured TemplateLookup to
get at the Template.

Isaac Csandl

unread,
Sep 29, 2008, 3:30:33 PM9/29/08
to Mako Templates for Python


On Sep 27, 9:56 am, Michael Bayer <mike...@zzzcomputing.com> wrote:
> OK, check out this feature inhttp://www.makotemplates.org/trac/changeset/405
>   .   You'd have to get at your Pylons-configured TemplateLookup to  
> get at the Template.

Wow, thanks! I'll take it for a spin today and let you know. :D

--i


Isaac Csandl

unread,
Sep 30, 2008, 10:41:11 AM9/30/08
to Mako Templates for Python
It works! Thanks again.

Isaac Csandl

unread,
Sep 30, 2008, 11:42:57 AM9/30/08
to Mako Templates for Python


On Sep 30, 9:41 am, Isaac Csandl <nerk...@gmail.com> wrote:
> On Sep 29, 2:30 pm, Isaac Csandl <nerk...@gmail.com> wrote:
>
> > On Sep 27, 9:56 am, Michael Bayer <mike...@zzzcomputing.com> wrote:
>
> > > OK, check out this feature inhttp://www.makotemplates.org/trac/changeset/405
> > >   .   You'd have to get at your Pylons-configured TemplateLookup to  
> > > get at the Template.
> It works! Thanks again.

Update:

mytemplate.cache.invalidate('somekey') works, but invalidate_def
appears not to. There's no error message, but the def shows the same
data until the cache expires. (I'm testing using FF with WebDev
extension set to disable cache, to rule out browser caching)

For what I need, this will do, so no pressure to fix it on my
account. :)

--i

Michael Bayer

unread,
Oct 1, 2008, 8:21:11 PM10/1/08
to mako-d...@googlegroups.com

On Sep 30, 2008, at 11:42 AM, Isaac Csandl wrote:

>
>
>
> On Sep 30, 9:41 am, Isaac Csandl <nerk...@gmail.com> wrote:
>> On Sep 29, 2:30 pm, Isaac Csandl <nerk...@gmail.com> wrote:
>>
>>> On Sep 27, 9:56 am, Michael Bayer <mike...@zzzcomputing.com> wrote:
>>
>>>> OK, check out this feature inhttp://www.makotemplates.org/trac/changeset/405
>>>> . You'd have to get at your Pylons-configured TemplateLookup to
>>>> get at the Template.
>> It works! Thanks again.
>
> Update:
>
> mytemplate.cache.invalidate('somekey') works, but invalidate_def
> appears not to. There's no error message, but the def shows the same
> data until the cache expires. (I'm testing using FF with WebDev
> extension set to disable cache, to rule out browser caching)
>

does your <%def> use cache_key ? the feature isn't looking at custom
cache_keys right now.

Isaac Csandl

unread,
Oct 3, 2008, 10:46:49 AM10/3/08
to Mako Templates for Python
Yeah, that must be why.

Isaac Csandl

unread,
Oct 3, 2008, 6:49:51 PM10/3/08
to Mako Templates for Python
On further consideration, I may have misunderstood how it was intended
to be used. What is working here is actually clearing the value of a
cached def (with cache_key) by using
mytemplate.cache.invalidate('key'). I was probably assuming
invalidate_def would simply clear *all* cached values for the def,
regardless of cache_key (or lack thereof). So... it seems there is
nothing to fix, but it may be worthwhile to make the docs on this
extra clear (e.g., "invalidate_def does nothing to defs with a
cache_key") so somebody else doesn't make the same mistake.

happy friday!

Reply all
Reply to author
Forward
0 new messages