mirror of
https://github.com/Hopiu/django-cachalot.git
synced 2026-05-10 21:53:12 +00:00
Conflicts: cachalot/cache.py cachalot/monkey_patch.py cachalot/tests/read.py cachalot/utils.py tox.ini
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
# coding: utf-8
|
|
|
|
from __future__ import unicode_literals
|
|
from threading import local
|
|
|
|
from django.core.cache import caches
|
|
|
|
from .settings import cachalot_settings
|
|
from .transaction import AtomicCache
|
|
|
|
|
|
class CacheHandler(local):
|
|
@property
|
|
def atomic_caches(self):
|
|
if not hasattr(self, '_atomic_caches'):
|
|
self._atomic_caches = []
|
|
return self._atomic_caches
|
|
|
|
def get_atomic_cache(self, cache_alias, level):
|
|
if cache_alias not in self.atomic_caches[level]:
|
|
self.atomic_caches[level][cache_alias] = AtomicCache(
|
|
self.get_cache(cache_alias, level-1))
|
|
return self.atomic_caches[level][cache_alias]
|
|
|
|
def get_cache(self, cache_alias=None, atomic_level=-1):
|
|
if cache_alias is None:
|
|
cache_alias = cachalot_settings.CACHALOT_CACHE
|
|
|
|
min_level = -len(self.atomic_caches)
|
|
if atomic_level < min_level:
|
|
return caches[cache_alias]
|
|
return self.get_atomic_cache(cache_alias, atomic_level)
|
|
|
|
def enter_atomic(self):
|
|
self.atomic_caches.append({})
|
|
|
|
def exit_atomic(self, commit):
|
|
atomic_caches = self.atomic_caches.pop().values()
|
|
if commit:
|
|
for atomic_cache in atomic_caches:
|
|
atomic_cache.commit()
|
|
|
|
|
|
cachalot_caches = CacheHandler()
|