Anonymous avatar Anonymous committed fbd45d4

Readme upgrade. And tiny fix.

Comments (0)

Files changed (7)

 	    model1 = ListFieldCached(ReferenceField(Model1, reverse_delete_rule=REF_CASCADE, dbref=False), required=True)
 	    meta = { 'queryset_class': CachedQuerySet, 'cascade' : False }
 
-For new mongoengine version accepted dbref=False for reference field.
+For new mongoengine version accepted dbref=True for reference field.
 
 If you use Django make sure the 'mongoengine_rediscache' after a 'you_application' in INSTALLED_APPS (all your applications)::
 
   8) "model1:get:-0x445aa237"
   9) "model1:get:-0x18b616c0"
 
-This will be usefull if you have a lot of different samples of one collection.
+How to simple flush cahce? It is not necessary run FLUSHALL in redis-cli.
+
+You only can change version of needed collection. For flush cache of Model1 you can::
+
+	redis 127.0.0.1:6379> SELECT 1
+	OK
+	redis 127.0.0.1:6379[1]> INCRBY "version:model1" 1
+	(integer) 12
+
+If you want flush cache for all collection try this::
+
+	$redis-cli -n 1 keys '*version:*' | grep '^version:[a-z0-9]\{1,32\}$' | xargs redis-cli -n 1 incr
+
 
 Simple tests
 =====

django_test_application/mongoengine_rediscache/fields.py

     ReferenceField,
     ListField,
     Document)
+from mongoengine.queryset import QuerySet
 from helper import _queryset_list
 from base_cache import _internal_cache as cache
 from bson.dbref import DBRef
                 return super(ListFieldCached, self).__get__(instance, owner)
             
             DBRef_list = instance._data.get(self.name)
-            if isinstance(DBRef_list, _queryset_list):
-                return DBRef_list
+
+            if isinstance(DBRef_list, (list, tuple, QuerySet)):
+                for d in DBRef_list:
+                    if isinstance(d, Document):
+                        return DBRef_list
+
             if DBRef_list and len(DBRef_list) > 0:
                 keys = []
     
                     if isinstance(dbref_obj, DBRef):
                         keys.append('%s:get:%s' % (dbref_obj.collection,
                                                    CacheNameMixer({ 'pk' : str(dbref_obj.id) }) ) )
-                    else:
+                    elif isinstance(dbref_obj, (str, unicode)):
                         keys.append('%s:get:%s' % (self.document_type._get_collection_name(),
                                                    CacheNameMixer({ 'pk' : str(dbref_obj)}) ) )
     
                     if isinstance(value, DBRef):
                         core = (value.collection,
                                 CacheNameMixer({ 'pk' : str(value.id) }) )
-                    else:
+                    elif isinstance(value, (str, unicode)):
                         core = (self.document_type._get_collection_name(),
                                 CacheNameMixer({ 'pk' : str(value) }) )
-
-                    cache_key = '%s:get:%s' % core
-                    obj = cache.get(cache_key)
-
-                    if obj is None:
-                        obj = super(ReferenceFieldCached, self).__get__(instance, owner)
-                        if obj:
-                            cache.set(cache_key, obj, timeout)
-                    instance._data[self.name] = obj
+                    if core:
+                        cache_key = '%s:get:%s' % core
+                        obj = cache.get(cache_key)
+    
+                        if obj is None:
+                            obj = super(ReferenceFieldCached, self).__get__(instance, owner)
+                            if obj:
+                                cache.set(cache_key, obj, timeout)
+                        instance._data[self.name] = obj
 
         return super(ReferenceFieldCached, self).__get__(instance, owner)

django_test_application/mongoengine_rediscache/helper.py

 class SecondaryKey(object):
     key = None
     pk = None
-    def __init__(self, key, pk):
+    version = None
+    def __init__(self, key, pk, version):
         self.key = key
         self.pk = pk
+        self.version = version

django_test_application/mongoengine_rediscache/queryset.py

             document = cache.get(cache_key)
             if isinstance(document, SecondaryKey):
                 original_pk = document.pk
+                v = document.version
                 document = cache.get(document.key)
-                if not isinstance(document, Document):
+                if not isinstance(document, Document) or v<self.cache_version:
                     document = self.get(pk=original_pk)
+
             elif document is None:
                 self.__call__(*q_objs, **query)
                 count = super(CachedQuerySet, self).count()
                 original_cache_key = "%s:get:%s" % (self._document._get_collection_name(),
                                                     CacheNameMixer({ 'pk' : str(document.pk) }))
                 if original_cache_key != cache_key:
-                    cache.set(cache_key, SecondaryKey(original_cache_key, str(document.pk)), timeout)
+                    cache.set(cache_key,
+                              SecondaryKey(original_cache_key, str(document.pk), self.cache_version),
+                              timeout)
 
                 cache.set(original_cache_key, document, timeout)
         else:

mongoengine_rediscache/fields.py

     ReferenceField,
     ListField,
     Document)
+from mongoengine.queryset import QuerySet
 from helper import _queryset_list
 from base_cache import _internal_cache as cache
 from bson.dbref import DBRef
                 return super(ListFieldCached, self).__get__(instance, owner)
             
             DBRef_list = instance._data.get(self.name)
-            if isinstance(DBRef_list, _queryset_list):
-                return DBRef_list
+
+            if isinstance(DBRef_list, (list, tuple, QuerySet)):
+                for d in DBRef_list:
+                    if isinstance(d, Document):
+                        return DBRef_list
+
             if DBRef_list and len(DBRef_list) > 0:
                 keys = []
     
                     if isinstance(dbref_obj, DBRef):
                         keys.append('%s:get:%s' % (dbref_obj.collection,
                                                    CacheNameMixer({ 'pk' : str(dbref_obj.id) }) ) )
-                    else:
+                    elif isinstance(dbref_obj, (str, unicode)):
                         keys.append('%s:get:%s' % (self.document_type._get_collection_name(),
                                                    CacheNameMixer({ 'pk' : str(dbref_obj)}) ) )
     
                     if isinstance(value, DBRef):
                         core = (value.collection,
                                 CacheNameMixer({ 'pk' : str(value.id) }) )
-                    else:
+                    elif isinstance(value, (str, unicode)):
                         core = (self.document_type._get_collection_name(),
                                 CacheNameMixer({ 'pk' : str(value) }) )
-
-                    cache_key = '%s:get:%s' % core
-                    obj = cache.get(cache_key)
-
-                    if obj is None:
-                        obj = super(ReferenceFieldCached, self).__get__(instance, owner)
-                        if obj:
-                            cache.set(cache_key, obj, timeout)
-                    instance._data[self.name] = obj
+                    if core:
+                        cache_key = '%s:get:%s' % core
+                        obj = cache.get(cache_key)
+    
+                        if obj is None:
+                            obj = super(ReferenceFieldCached, self).__get__(instance, owner)
+                            if obj:
+                                cache.set(cache_key, obj, timeout)
+                        instance._data[self.name] = obj
 
         return super(ReferenceFieldCached, self).__get__(instance, owner)

mongoengine_rediscache/helper.py

 class SecondaryKey(object):
     key = None
     pk = None
-    def __init__(self, key, pk):
+    version = None
+    def __init__(self, key, pk, version):
         self.key = key
         self.pk = pk
+        self.version = version

mongoengine_rediscache/queryset.py

             document = cache.get(cache_key)
             if isinstance(document, SecondaryKey):
                 original_pk = document.pk
+                v = document.version
                 document = cache.get(document.key)
-                if not isinstance(document, Document):
+                if not isinstance(document, Document) or v<self.cache_version:
                     document = self.get(pk=original_pk)
+
             elif document is None:
                 self.__call__(*q_objs, **query)
                 count = super(CachedQuerySet, self).count()
                 original_cache_key = "%s:get:%s" % (self._document._get_collection_name(),
                                                     CacheNameMixer({ 'pk' : str(document.pk) }))
                 if original_cache_key != cache_key:
-                    cache.set(cache_key, SecondaryKey(original_cache_key, str(document.pk)), timeout)
+                    cache.set(cache_key,
+                              SecondaryKey(original_cache_key, str(document.pk), self.cache_version),
+                              timeout)
 
                 cache.set(original_cache_key, document, timeout)
         else:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.