django-cachalot/cachalot/transaction.py
Bertrand Bordage 352fb2d829 Small Python optimizations.
While keeping code compatible with Python 2.6 to 3.4, of course…
2015-01-03 06:38:16 +01:00

34 lines
1,013 B
Python

# coding: utf-8
from __future__ import unicode_literals
from .utils import _invalidate_table_cache_keys
class AtomicCache(dict):
def __init__(self, parent_cache):
super(AtomicCache, self).__init__()
self.parent_cache = parent_cache
self.to_be_invalidated = set()
def set(self, k, v, timeout):
self[k] = v
def get_many(self, keys):
data = {}
for k in keys:
if k in self:
data[k] = self[k]
missing_keys = set(keys)
missing_keys.difference_update(data)
data.update(self.parent_cache.get_many(missing_keys))
return data
def set_many(self, data, timeout):
self.update(data)
def commit(self):
self.parent_cache.set_many(self, None)
# The previous `set_many` is not enough. The parent cache needs to be
# invalidated in case another transaction occurred in the meantime.
_invalidate_table_cache_keys(self.parent_cache, self.to_be_invalidated)