[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/OFS/Content/SQLScript - ISQLScript.py:1.11 SQLScript.py:1.8

Marius Gedminas mgedmin@codeworks.lt
Mon, 7 Oct 2002 05:55:10 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Content/SQLScript
In directory cvs.zope.org:/tmp/cvs-serv26898

Modified Files:
	ISQLScript.py SQLScript.py 
Log Message:
SQLScripts use the new caching framework

=== Zope3/lib/python/Zope/App/OFS/Content/SQLScript/ISQLScript.py 1.10 => 1.11 ===
--- Zope3/lib/python/Zope/App/OFS/Content/SQLScript/ISQLScript.py:1.10	Wed Sep 18 11:05:50 2002
+++ Zope3/lib/python/Zope/App/OFS/Content/SQLScript/ISQLScript.py	Mon Oct  7 05:54:39 2002
@@ -50,19 +50,6 @@
         description=u"The source of the page template.",
         required=True)
 
-    maxCache = Zope.Schema.Int(
-        title=u"Maximum results to cache",
-        description=u"The size of the SQL script cache.",
-        min=0,
-        required=True)
-
-    cacheTime = Zope.Schema.Int(
-        title=u"Maximum time (sec) to cache",
-        description=u"The time in seconds that results are cached. "
-                    u"Setting to zero disables caching.",
-        min=0,
-        required=True)
-
     def setArguments(arguments):
         """Processes the arguments (which could be a dict, string or whatever)
         to arguments as they are needed for the rendering process."""
@@ -89,14 +76,3 @@
     def getConnectionName():
         """Get the connection name for this SQL Script."""
 
-    def setMaxCache(maxCache):
-        """Set the size of the SQL script cache."""
-
-    def getMaxCache():
-        """Get the size of the SQL script cache."""
-
-    def setCacheTime(cacheTime):
-        """Set the time in seconds that results are cached."""
-
-    def getCacheTime():
-        """Get the time in seconds that results are cached."""


=== Zope3/lib/python/Zope/App/OFS/Content/SQLScript/SQLScript.py 1.7 => 1.8 ===
--- Zope3/lib/python/Zope/App/OFS/Content/SQLScript/SQLScript.py:1.7	Thu Aug  8 11:05:59 2002
+++ Zope3/lib/python/Zope/App/OFS/Content/SQLScript/SQLScript.py	Mon Oct  7 05:54:39 2002
@@ -28,6 +28,9 @@
 from Zope.App.OFS.Content.IFileContent import IFileContent
 from Zope.App.OFS.Content.SQLScript.ISQLScript import ISQLScript
 from Zope.App.OFS.Content.SQLScript.Arguments import parseArguments
+from Zope.App.OFS.Annotation.IAttributeAnnotatable import IAttributeAnnotatable
+
+from Zope.App.Caching.Caching import getCacheForObj
 
 from DT_SQLVar import SQLVar
 from DT_SQLTest import SQLTest
@@ -35,9 +38,6 @@
 
 from time import time
 
-try: from Persistence.BTrees.IOBTree import IOBucket as Bucket
-except: Bucket = lambda:{}
-
 
 class SQLDTML(HTML):
     __name__ = 'SQLDTML'
@@ -55,16 +55,13 @@
 
 class SQLScript(SQLCommand, Persistent):
 
-    __implements__ = ISQLScript, IFileContent
+    __implements__ = ISQLScript, IFileContent, IAttributeAnnotatable
 
-    def __init__(self, connectionName='', source='', arguments='',
-                 maxCache=0, cacheTime=0):
+    def __init__(self, connectionName='', source='', arguments=''):
         self.template = SQLDTML(source)
         self.setConnectionName(connectionName)
         # In our case arguments should be a string that is parsed
         self.setArguments(arguments)
-        self.setMaxCache(maxCache)
-        self.setCacheTime(cacheTime)
 
     def setArguments(self, arguments):
         'See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript'
@@ -96,30 +93,14 @@
     def setConnectionName(self, name):
         'See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript'
         self._connectionName = name
-        self._clearCache()
+        cache = getCacheForObj(self)
+        if cache:
+            cache.invalidate(self)
 
     def getConnectionName(self):
         'See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript'
         return self._connectionName
 
-    def setMaxCache(self, maxCache):
-        'See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript'
-        self._maxCache = maxCache
-        self._clearCache()
-
-    def getMaxCache(self):
-        'See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript'
-        return self._maxCache
-
-    def setCacheTime(self, cacheTime):
-        'See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript'
-        self._cacheTime = cacheTime
-        self._clearCache()
-
-    def getCacheTime(self):
-        'See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript'
-        return self._cacheTime
-
     def getConnection(self):
         'See Zope.App.RDB.ISQLCommand.ISQLCommand'
         connection_service = getService(self, "Connections")
@@ -164,49 +145,19 @@
                 '%s is not connected to a database' %'foo')# self.id)
 
         query = apply(self.template, (), arg_values)
-
-        if self._maxCache > 0 and self._cacheTime > 0:
-            return self._cachedResult(connection, query)
-        else:
-            return queryForResults(connection, query)
-
-    __call__ = ContextMethod(__call__)
-
-
-    def _clearCache(self):
-        'Clear the cache'
-        self._v_cache = {}, Bucket()
-
-    def _cachedResult(self, connection, query):
-        'Try to fetch query result from cache'
-        if not hasattr(self, '_v_cache'):
-            self._clearCache()
-        cache, tcache = self._v_cache
-        max_cache = self._maxCache
-        now = time()
-        t = now - self._cacheTime
-        if len(cache) > max_cache / 2:
-            keys = tcache.keys()
-            keys.reverse()
-            while keys and (len(keys) > max_cache or keys[-1] < t):
-                key = keys[-1]
-                q = tcache[key]
-                del tcache[key]
-                if int(cache[q][0]) == key:
-                    del cache[q]
-                del keys[-1]
-
-        if cache.has_key(query):
-            k, r = cache[query]
-            if k > t: return r
-
+        cache = getCacheForObj(self)
+        if cache:
+            _marker = []
+            result = cache.query(self, keywords={'query': query}, default=_marker)
+            if result is not _marker:
+                return result
         result = queryForResults(connection, query)
-        if self._cacheTime > 0:
-            tcache[int(now)] = query
-            cache[query] = now, result
-
+        if cache:
+            cache.set(result, self, keywords={'query': query})
         return result
 
+    __call__ = ContextMethod(__call__)
+
 
     # See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript
     arguments = property(getArgumentsString, setArguments, None,
@@ -215,8 +166,4 @@
                       "Set the SQL template source.")
     connectionName = property(getConnectionName, setConnectionName, None,
                               "Connection Name for the SQL scripts.")
-    maxCache = property(getMaxCache, setMaxCache, None,
-                        "Set the size of the SQL Script cache.")
-    cacheTime = property(getCacheTime, setCacheTime, None,
-                         "Set the time in seconds that results are cached.")