[repo] Consistent API for cache clearing
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 15 Mar 2017 08:30:27 +0100
changeset 12061 94ae25593c38
parent 12060 0cdf5fafd234
child 12062 601d65193619
[repo] Consistent API for cache clearing clear_caches, reset_caches, clear_eid_caches are now all named 'clear_caches' with optional eids/etypes arguments to clear eid specific cache entry,
cubicweb/devtools/__init__.py
cubicweb/server/repository.py
cubicweb/server/sources/native.py
--- a/cubicweb/devtools/__init__.py	Fri Mar 03 13:09:11 2017 +0100
+++ b/cubicweb/devtools/__init__.py	Wed Mar 15 08:30:27 2017 +0100
@@ -124,9 +124,8 @@
     if repo._needs_refresh:
         for cnxset in repo.cnxsets:
             cnxset.reconnect()
-        repo._type_cache = {}
         repo.querier.set_schema(repo.schema)
-        repo.system_source.reset_caches()
+        repo.clear_caches()
         repo._needs_refresh = False
 
 
--- a/cubicweb/server/repository.py	Fri Mar 03 13:09:11 2017 +0100
+++ b/cubicweb/server/repository.py	Wed Mar 15 08:30:27 2017 +0100
@@ -660,17 +660,23 @@
     # * correspondance between eid and type
     # * correspondance between eid and local id (i.e. specific to a given source)
 
-    def clear_caches(self, eids):
-        etcache = self._type_cache
-        rqlcache = self.querier.rql_cache
-        for eid in eids:
-            try:
-                etype = etcache.pop(int(eid))  # may be a string in some cases
-                rqlcache.pop(('%s X WHERE X eid %s' % (etype, eid),), None)
-            except KeyError:
-                etype = None
-            rqlcache.pop(('Any X WHERE X eid %s' % eid,), None)
-            self.system_source.clear_eid_cache(eid, etype)
+    def clear_caches(self, eids=None):
+        if eids is None:
+            self._type_cache = {}
+            etypes = None
+        else:
+            etypes = []
+            etcache = self._type_cache
+            rqlcache = self.querier.rql_cache
+            for eid in eids:
+                try:
+                    etype = etcache.pop(int(eid))  # may be a string in some cases
+                    rqlcache.pop(('%s X WHERE X eid %s' % (etype, eid),), None)
+                except KeyError:
+                    etype = None
+                rqlcache.pop(('Any X WHERE X eid %s' % eid,), None)
+                etypes.append(etype)
+        self.system_source.clear_caches(eids, etypes)
 
     def type_from_eid(self, eid, cnx):
         """Return the type of the entity with id `eid`"""
--- a/cubicweb/server/sources/native.py	Fri Mar 03 13:09:11 2017 +0100
+++ b/cubicweb/server/sources/native.py	Wed Mar 15 08:30:27 2017 +0100
@@ -30,7 +30,7 @@
 import sys
 
 from six import PY2, text_type, string_types
-from six.moves import range, cPickle as pickle
+from six.moves import range, cPickle as pickle, zip
 
 from logilab.common.decorators import cached, clear_cache
 from logilab.common.configuration import Method
@@ -361,15 +361,17 @@
         authentifier.source = self
         authentifier.set_schema(self.schema)
 
-    def reset_caches(self):
-        """method called during test to reset potential source caches"""
-        self._cache = QueryCache(self.repo.config['rql-cache-size'])
-
-    def clear_eid_cache(self, eid, etype):
-        """clear potential caches for the given eid"""
-        self._cache.pop('Any X WHERE X eid %s, X is %s' % (eid, etype), None)
-        self._cache.pop('Any X WHERE X eid %s' % eid, None)
-        self._cache.pop('Any %s' % eid, None)
+    def clear_caches(self, eids, etypes):
+        """Clear potential source caches."""
+        if eids is None:
+            self._cache = QueryCache(self.repo.config['rql-cache-size'])
+        else:
+            cache = self._cache
+            for eid, etype in zip(eids, etypes):
+                cache.pop('Any X WHERE X eid %s' % eid, None)
+                cache.pop('Any %s' % eid, None)
+                if etype is not None:
+                    cache.pop('Any X WHERE X eid %s, X is %s' % (eid, etype), None)
 
     @statsd_timeit
     def sqlexec(self, cnx, sql, args=None):