Added at_idmapper_flush to allow objects more control of their flush characteristics.

This commit is contained in:
Griatch 2016-10-02 19:14:22 +02:00
parent cd616caf29
commit 15025c0586
6 changed files with 74 additions and 33 deletions

View file

@ -79,7 +79,6 @@ class SharedMemoryModelBase(ModelBase):
# the dbmodel is either the proxy base or ourselves
dbmodel = cls._meta.proxy_for_model if cls._meta.proxy else cls
cls.__dbclass__ = dbmodel
dbmodel._idmapper_recache_protection = False
if not hasattr(dbmodel, "__instance_cache__"):
# we store __instance_cache__ only on the dbmodel base
dbmodel.__instance_cache__ = {}
@ -291,8 +290,10 @@ class SharedMemoryModel(with_metaclass(SharedMemoryModelBase, Model)):
"""
try:
if force or not cls._idmapper_recache_protection:
if force or cls.at_idmapper_flush():
del cls.__dbclass__.__instance_cache__[key]
else:
cls._dbclass__.__instance_cache__[key].refresh_from_db()
except KeyError:
pass
@ -319,27 +320,37 @@ class SharedMemoryModel(with_metaclass(SharedMemoryModelBase, Model)):
cls.__dbclass__.__instance_cache__ = {}
else:
cls.__dbclass__.__instance_cache__ = dict((key, obj) for key, obj in cls.__dbclass__.__instance_cache__.items()
if obj._idmapper_recache_protection)
if not obj.at_idmapper_flush())
for ins in cls.__dbclass__.__instance_cache__.itervalues():
ins.refresh_from_db()
#flush_instance_cache = classmethod(flush_instance_cache)
# per-instance methods
def at_idmapper_flush(self):
"""
This is called when the idmapper cache is flushed and
allows customized actions when this happens.
Returns:
do_flush (bool): If True, flush this object as normal. If
False, don't flush and expect this object to handle
the flushing on its own.
"""
return True
def flush_from_cache(self, force=False):
"""
Flush this instance from the instance cache. Use
`force` to override recache_protection for the object.
`force` to override the result of at_idmapper_flush() for the object.
"""
pk = self._get_pk_val()
if pk and (force or not self._idmapper_recache_protection):
self.__class__.__dbclass__.__instance_cache__.pop(pk, None)
def set_recache_protection(self, mode=True):
"""
Set if this instance should be allowed to be recached.
"""
self._idmapper_recache_protection = bool(mode)
if pk:
if force or self.at_idmapper_flush():
self.__class__.__dbclass__.__instance_cache__.pop(pk, None)
else:
self.refresh_from_db()
def delete(self, *args, **kwargs):
"""
@ -415,7 +426,6 @@ class WeakSharedMemoryModelBase(SharedMemoryModelBase):
def _prepare(cls):
super(WeakSharedMemoryModelBase, cls)._prepare()
cls.__dbclass__.__instance_cache__ = WeakValueDictionary()
cls._idmapper_recache_protection = False
class WeakSharedMemoryModel(with_metaclass(WeakSharedMemoryModelBase, SharedMemoryModel)):
@ -429,10 +439,9 @@ class WeakSharedMemoryModel(with_metaclass(WeakSharedMemoryModelBase, SharedMemo
def flush_cache(**kwargs):
"""
Flush idmapper cache. When doing so the cache will
look for a property `_idmapper_cache_flush_safe` on the
class/subclass instance and only flush if this
is `True`.
Flush idmapper cache. When doing so the cache will fire the
at_idmapper_flush hook to allow the object to optionally handle
its own flushing.
Uses a signal so we make sure to catch cascades.