Signal-based cache invalidation & cached function decorator

134 views
Skip to first unread message

Sergey Kirillov

unread,
Nov 14, 2007, 8:41:36 AM11/14/07
to Django developers
Hi all

In my project I frequently encountered a situation where I need to
cache some data, and then invalidate it on signal.

So I wrote following decorator:

def cached(slot_name, timeout=None):
def decorator(function):
def invalidate():
cache.delete(slot_name)

def wrapped(*args, **kwargs):
result = cache.get(slot_name)
if result is None:
result = function(*args, **kwargs)
cache.set(slot_name, result, timeout)
return result
wrapped.invalidate = invalidate
return wrapped
return decorator


And here is example usage:

@cached('/data/something_hard')
def get_something_hard():
....
return result

dispatcher.connect(get_something_hard.invalidate,
django.db.models.signals.post_save, Model)


I think it's generic enough and can be included in Django itself.

What do you think?

Honza Král

unread,
Nov 14, 2007, 9:20:17 AM11/14/07
to django-d...@googlegroups.com

this wouldn't work across more boxes and/or processes - one process
creates the cache and registers the signal, another process changes
the object and fires the signal, which isn't trapped.

>
>
> >
>

--
Honza Král
E-Mail: Honza...@gmail.com
ICQ#: 107471613
Phone: +420 606 678585

Sergey Kirillov

unread,
Nov 14, 2007, 3:13:23 PM11/14/07
to Django developers
For shared cache backends, like Memcached it works fine. You just need
to be sure that signal handlers will be registered in all processes
(i.e. put them in models.py)

On Nov 14, 4:20 pm, "Honza Král" <honza.k...@gmail.com> wrote:

> Honza Kr?l
> E-Mail: Honza.K...@gmail.com

Honza Král

unread,
Nov 14, 2007, 7:27:35 PM11/14/07
to django-d...@googlegroups.com
On Nov 14, 2007 9:13 PM, Sergey Kirillov <sergey....@gmail.com> wrote:
>
> For shared cache backends, like Memcached it works fine. You just need
> to be sure that signal handlers will be registered in all processes
> (i.e. put them in models.py)

sorry, my bad - we do a similar thing but dynamic, and that where this
problem occurs, when the binding to a signal is static, its not an
issue

--
Honza Král
E-Mail: Honza...@gmail.com

Sergey Kirillov

unread,
Nov 19, 2007, 3:36:04 AM11/19/07
to Django developers
That's true.

So, does it looks interesting for someone?

Or I'm the only one who do signal-based cache invalidation? :)



On Nov 15, 2:27 am, "Honza Král" <honza.k...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages