Made changes to idmapper that might help alleviate issue101 (more people need to run it to make sure). Moved around default command modules to be more logically named and distributed.

This commit is contained in:
Griatch 2010-10-31 08:10:02 +00:00
parent 19dd476115
commit 3f703efc2d
17 changed files with 1920 additions and 1851 deletions

View file

@ -1,9 +1,11 @@
from weakref import WeakValueDictionary
from weakref import WeakValueDictionary, ref
from django.db.models.base import Model, ModelBase
from manager import SharedMemoryManager
TCACHE = {}
class SharedMemoryModelBase(ModelBase):
def __new__(cls, name, bases, attrs):
super_new = super(ModelBase, cls).__new__
@ -37,7 +39,7 @@ class SharedMemoryModelBase(ModelBase):
return cached_instance
def _prepare(cls):
cls.__instance_cache__ = WeakValueDictionary()
cls.__instance_cache__ = {} #WeakValueDictionary()
super(SharedMemoryModelBase, cls)._prepare()
@ -91,7 +93,11 @@ class SharedMemoryModel(Model):
Method to store an instance in the cache.
"""
if instance._get_pk_val() is not None:
cls.__instance_cache__[instance._get_pk_val()] = instance
cls.__instance_cache__[instance._get_pk_val()] = instance
#key = "%s-%s" % (cls, instance.pk)
#TCACHE[key] = instance
#print "cached: %s (%s: %s) (total cached: %s)" % (instance, cls.__name__, len(cls.__instance_cache__), len(TCACHE))
cache_instance = classmethod(cache_instance)
def _flush_cached_by_key(cls, key):
@ -104,6 +110,10 @@ class SharedMemoryModel(Model):
since this is most likely called from delete(), and we want to make sure we don't cache dead objects.
"""
cls._flush_cached_by_key(instance._get_pk_val())
#key = "%s-%s" % (cls, instance.pk)
#del TCACHE[key]
#print "uncached: %s (%s: %s) (total cached: %s)" % (instance, cls.__name__, len(cls.__instance_cache__), len(TCACHE))
flush_cached_instance = classmethod(flush_cached_instance)
def save(self, *args, **kwargs):
@ -126,4 +136,4 @@ pre_delete.connect(flush_singleton_cache)
# def update_singleton_cache(sender, instance, **kwargs):
# if isinstance(instance.__class__, SharedMemoryModel):
# instance.__class__.cache_instance(instance)
# post_save.connect(flush_singleton_cache)
# post_save.connect(flush_singleton_cache)