Implemented and tested set_recache_protection() on all idmapped objects, to avoid them getting recached. Added successfully to nattribute handler.

This commit is contained in:
Griatch 2014-05-15 10:05:41 +02:00
parent a617924fb0
commit 5e4af3f851
2 changed files with 10 additions and 6 deletions

View file

@ -29,6 +29,7 @@ these to create custom managers.
import sys import sys
import re import re
import traceback import traceback
import weakref
from django.db import models from django.db import models
from django.conf import settings from django.conf import settings
@ -453,6 +454,7 @@ class NAttributeHandler(object):
def __init__(self, obj): def __init__(self, obj):
"initialized on the object" "initialized on the object"
self._store = {} self._store = {}
self.obj = weakref.proxy(obj)
def has(self, key): def has(self, key):
"Check if object has this attribute or not" "Check if object has this attribute or not"
@ -465,11 +467,13 @@ class NAttributeHandler(object):
def add(self, key, value): def add(self, key, value):
"Add new key and value" "Add new key and value"
self._store[key] = value self._store[key] = value
self.obj.set_recache_protection()
def remove(self, key): def remove(self, key):
"Remove key from storage" "Remove key from storage"
if key in self._store: if key in self._store:
del self._store[key] del self._store[key]
self.obj.set_recache_protection(self._store)
def all(self, return_tuples=False): def all(self, return_tuples=False):
"List all keys or (keys, values) stored, except _keys" "List all keys or (keys, values) stored, except _keys"

View file

@ -67,7 +67,7 @@ class SharedMemoryModelBase(ModelBase):
def _prepare(cls): def _prepare(cls):
cls.__instance_cache__ = {} cls.__instance_cache__ = {}
cls._idmapper_cache_flush_safe = True cls._idmapper_recache_protection = False
super(SharedMemoryModelBase, cls)._prepare() super(SharedMemoryModelBase, cls)._prepare()
def __new__(cls, classname, bases, classdict, *args, **kwargs): def __new__(cls, classname, bases, classdict, *args, **kwargs):
@ -232,16 +232,16 @@ class SharedMemoryModel(Model):
def _flush_cached_by_key(cls, key, force=True): def _flush_cached_by_key(cls, key, force=True):
"Remove the cached reference." "Remove the cached reference."
try: try:
if force or cls._idmapper_cache_flush_safe: if force or not cls._idmapper_recache_protection:
del cls.__instance_cache__[key] del cls.__instance_cache__[key]
except KeyError: except KeyError:
pass pass
_flush_cached_by_key = classmethod(_flush_cached_by_key) _flush_cached_by_key = classmethod(_flush_cached_by_key)
def _set_recache(cls, mode=True): def set_recache_protection(cls, mode=True):
"set if this instance should be allowed to be recached." "set if this instance should be allowed to be recached."
cls._idmapper_cache_flush_safe = bool(mode) cls._idmapper_recache_protection = bool(mode)
_set_recache = classmethod(_set_recache) set_recache_protection = classmethod(set_recache_protection)
def flush_cached_instance(cls, instance, force=True): def flush_cached_instance(cls, instance, force=True):
""" """
@ -263,7 +263,7 @@ class SharedMemoryModel(Model):
cls.__instance_cache__ = {} cls.__instance_cache__ = {}
else: else:
cls.__instance_cache__ = dict((key, obj) for key, obj in cls.__instance_cache__.items() cls.__instance_cache__ = dict((key, obj) for key, obj in cls.__instance_cache__.items()
if not obj._idmapper_cache_flush_safe) if obj._idmapper_recache_protection)
flush_instance_cache = classmethod(flush_instance_cache) flush_instance_cache = classmethod(flush_instance_cache)
def save(cls, *args, **kwargs): def save(cls, *args, **kwargs):