server/sources/pyrorql.py
changeset 1238 fa29b5b60107
parent 520 29342c0cf55f
child 1239 7eda09aa7553
--- a/server/sources/pyrorql.py	Sat Apr 04 15:16:37 2009 +0200
+++ b/server/sources/pyrorql.py	Sat Apr 04 15:45:57 2009 +0200
@@ -1,7 +1,7 @@
 """Source to query another RQL repository using pyro
 
 :organization: Logilab
-:copyright: 2007-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+:copyright: 2007-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
 """
 __docformat__ = "restructuredtext en"
@@ -21,7 +21,7 @@
 from cubicweb import dbapi, server
 from cubicweb import BadConnectionId, UnknownEid, ConnectionError
 from cubicweb.cwconfig import register_persistent_options
-from cubicweb.server.sources import AbstractSource, ConnectionWrapper
+from cubicweb.server.sources import AbstractSource, ConnectionWrapper, TimedCache
 
 class ReplaceByInOperator:
     def __init__(self, eids):
@@ -129,6 +129,7 @@
                        'group': 'sources', 
                        }),)
         register_persistent_options(myoptions)
+        self._query_cache = TimedCache(30)
 
     def last_update_time(self):
         pkey = u'sources.%s.latest-update-time' % self.uri
@@ -153,6 +154,7 @@
         """method called by the repository once ready to handle request"""
         interval = int(self.config.get('synchronization-interval', 5*60))
         self.repo.looping_task(interval, self.synchronize) 
+        self.repo.looping_task(self._query_cache.ttl.seconds/10, self._query_cache.clear_expired) 
 
     def synchronize(self, mtime=None):
         """synchronize content known by this repository with content in the
@@ -240,9 +242,20 @@
         # try to reconnect
         return self.get_connection()
         
-    
     def syntax_tree_search(self, session, union, args=None, cachekey=None,
                            varmap=None):
+        assert not varmap, (varmap, union)
+        rqlkey = union.as_string(kwargs=args)
+        try:
+            results = self._query_cache[rqlkey]
+            print 'cache hit', rqlkey
+        except KeyError:
+            results = self._syntax_tree_search(session, union, args)
+            print 'cache miss', rqlkey
+            self._query_cache[rqlkey] = results
+        return results
+    
+    def _syntax_tree_search(self, session, union, args):
         """return result from this source for a rql query (actually from a rql 
         syntax tree and a solution dictionary mapping each used variable to a 
         possible type). If cachekey is given, the query necessary to fetch the