Opened 10 years ago

Closed 10 years ago

#24402 closed New feature (duplicate)

Missing support for rollback in django.core.cache

Reported by: Vlada Macek Owned by: nobody
Component: Core (Cache system) Version:
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Tried unsuccessfully to find existing ticket for this, so starting an idea discussion.

In my project, I use memcached via django.core.cache to cache data resulting from some ORM operations. But when the database rolls back, the memcache is kept in the wrong state.

Currently I'm adding another layer with local dict (which has some speed improvements effects):

class DeferredMemcache(object):
    def __init__(self):
        from django.core import cache  # Importing this way so debug_toolbar can patch it later.

        self.real_cache = cache.cache
        self.local_cache = {}

    def get(self, key, default=None):
        try:
            value, deadline = self.local_cache[key]
        except KeyError:
            return self.real_cache.get(key, default)
        else:
            return value if deadline > time.time() else default

    def set(self, key, value, timeout):
        assert timeout > 0
        self.local_cache[key] = value, timeout + time.time()

    def commit(self):
        for key, (value, deadline) in self.local_cache.iteritems():
            timeout = deadline - time.time()
            if timeout >= 0:
                self.real_cache.set(key, value, timeout)

        self.local_cache.clear()

What do you think about adding some support for cache rollback in Django?

Change History (3)

comment:1 by Tim Graham, 10 years ago

Could you please open a discussion on the DevelopersMailingList to get feedback on the idea?

comment:2 by Aymeric Augustin, 10 years ago

I believe this is a duplicate of #21803.

comment:3 by Tim Graham, 10 years ago

Resolution: duplicate
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top