move get_cache to base request
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 13 Aug 2009 09:41:24 +0200
changeset 2796 14d2c69e12c4
parent 2795 59965167bef3
child 2797 de0fcdb65e30
move get_cache to base request
appobject.py
req.py
--- a/appobject.py	Thu Aug 13 09:41:05 2009 +0200
+++ b/appobject.py	Thu Aug 13 09:41:24 2009 +0200
@@ -11,25 +11,12 @@
 
 import types
 from logging import getLogger
-from datetime import datetime, timedelta
 
 from logilab.common.decorators import classproperty
 from logilab.common.deprecation import deprecated
 from logilab.common.logging_ext import set_log_methods
 
 from cubicweb import Unauthorized, NoSelectableObject
-from cubicweb.utils import UStringIO
-
-ONESECOND = timedelta(0, 1, 0)
-CACHE_REGISTRY = {}
-
-
-class Cache(dict):
-    def __init__(self):
-        super(Cache, self).__init__()
-        _now = datetime.now()
-        self.cache_creation_date = _now
-        self.latest_cache_lookup = _now
 
 
 # selector base classes and operations ########################################
@@ -330,27 +317,6 @@
         self.col = col
         self.extra_kwargs = extra
 
-    def get_cache(self, cachename):
-        """
-        NOTE: cachename should be dotted names as in :
-        - cubicweb.mycache
-        - cubes.blog.mycache
-        - etc.
-        """
-        if cachename in CACHE_REGISTRY:
-            cache = CACHE_REGISTRY[cachename]
-        else:
-            cache = CACHE_REGISTRY[cachename] = Cache()
-        _now = datetime.now()
-        if _now > cache.latest_cache_lookup + ONESECOND:
-            ecache = self.req.execute('Any C,T WHERE C is CWCache, C name %(name)s, C timestamp T',
-                                      {'name':cachename}).get_entity(0,0)
-            cache.latest_cache_lookup = _now
-            if not ecache.valid(cache.cache_creation_date):
-                cache.clear()
-                cache.cache_creation_date = _now
-        return cache
-
     def propval(self, propid):
         assert self.req
         return self.req.property_value(self.propkey(propid))
@@ -412,6 +378,10 @@
     def initialize_varmaker(self):
         self.varmaker = self.req.varmaker
 
+    @deprecated('[3.5] use req.get_cache')
+    def get_cache(self, cachename):
+        return self.req.get_cache(cachename)
+
     @deprecated('[3.5] use rset.limited_rql')
     def limited_rql(self):
         return self.rset.limited_rql()
--- a/req.py	Thu Aug 13 09:41:05 2009 +0200
+++ b/req.py	Thu Aug 13 09:41:24 2009 +0200
@@ -7,7 +7,7 @@
 """
 __docformat__ = "restructuredtext en"
 
-from datetime import time
+from datetime import time, datetime, timedelta
 
 from logilab.common.decorators import cached
 
@@ -15,6 +15,18 @@
 from cubicweb.rset import ResultSet
 from cubicweb.utils import ustrftime, strptime, todate, todatetime
 
+ONESECOND = timedelta(0, 1, 0)
+CACHE_REGISTRY = {}
+
+
+class Cache(dict):
+    def __init__(self):
+        super(Cache, self).__init__()
+        _now = datetime.now()
+        self.cache_creation_date = _now
+        self.latest_cache_lookup = _now
+
+
 class RequestSessionBase(object):
     """base class containing stuff shared by server session and web request
     """
@@ -89,6 +101,28 @@
         if first in ('insert', 'set', 'delete'):
             raise Unauthorized(self._('only select queries are authorized'))
 
+    def get_cache(self, cachename):
+        """
+        NOTE: cachename should be dotted names as in :
+        - cubicweb.mycache
+        - cubes.blog.mycache
+        - etc.
+        """
+        if cachename in CACHE_REGISTRY:
+            cache = CACHE_REGISTRY[cachename]
+        else:
+            cache = CACHE_REGISTRY[cachename] = Cache()
+        _now = datetime.now()
+        if _now > cache.latest_cache_lookup + ONESECOND:
+            ecache = self.execute(
+                'Any C,T WHERE C is CWCache, C name %(name)s, C timestamp T',
+                {'name':cachename}).get_entity(0,0)
+            cache.latest_cache_lookup = _now
+            if not ecache.valid(cache.cache_creation_date):
+                cache.clear()
+                cache.cache_creation_date = _now
+        return cache
+
     # url generation methods ##################################################
 
     def build_url(self, *args, **kwargs):