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?
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
On Nov 14, 4:20 pm, "Honza Král" <honza.k...@gmail.com> wrote:
> Honza Kr?l
> E-Mail: Honza.K...@gmail.com
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