From 09e7dfa6ab707f1614531fc80215465db099db6c Mon Sep 17 00:00:00 2001
From: Ivan Tyagov <ivan@nexedi.com>
Date: Wed, 8 Nov 2006 13:29:41 +0000
Subject: [PATCH] Added initCacheStorage method which will init (if needed)
 cache backend storage when cache is updated

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@11204 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../CachePlugins/DistributedRamCache.py       |  5 +++++
 product/ERP5Type/CachePlugins/RamCache.py     |  7 ++++++-
 product/ERP5Type/CachePlugins/SQLCache.py     | 19 ++++++++++++++++---
 product/ERP5Type/Tool/CacheTool.py            |  6 ++++--
 4 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/product/ERP5Type/CachePlugins/DistributedRamCache.py b/product/ERP5Type/CachePlugins/DistributedRamCache.py
index 4e20f9c33c..3c23bdca02 100644
--- a/product/ERP5Type/CachePlugins/DistributedRamCache.py
+++ b/product/ERP5Type/CachePlugins/DistributedRamCache.py
@@ -52,6 +52,11 @@ class DistributedRamCache(BaseCache):
     self._debugLevel = params.get('debugLevel', 7)
     self._last_cache_conn_creation_time = time()
     BaseCache.__init__(self)
+    
+  def initCacheStorage(self):
+    """ Init cache storage """
+    ## cache storage is a memcached server and no need to init it
+    pass
         
   def getCacheStorage(self):
     ## if we use one connection object this causes 
diff --git a/product/ERP5Type/CachePlugins/RamCache.py b/product/ERP5Type/CachePlugins/RamCache.py
index 7f276c85e1..e4138c909d 100644
--- a/product/ERP5Type/CachePlugins/RamCache.py
+++ b/product/ERP5Type/CachePlugins/RamCache.py
@@ -42,7 +42,12 @@ class RamCache(BaseCache):
     
   def __init__(self, params={}):
     BaseCache.__init__(self)
-            
+ 
+  def initCacheStorage(self):
+    """ Init cache storage """
+    ## cache storage is a RAM based dictionary
+    pass
+    
   def getCacheStorage(self):
     return self._cache_dict
     
diff --git a/product/ERP5Type/CachePlugins/SQLCache.py b/product/ERP5Type/CachePlugins/SQLCache.py
index 95341b0333..d8875bedb4 100644
--- a/product/ERP5Type/CachePlugins/SQLCache.py
+++ b/product/ERP5Type/CachePlugins/SQLCache.py
@@ -105,7 +105,9 @@ class SQLCache(BaseCache):
                                 FROM %s 
                                 WHERE scope="%s"
                             '''
-
+                            
+  find_table_by_name_sql = """SHOW TABLES LIKE '%s' """
+  
   def __init__(self, params):
     BaseCache.__init__(self)
     self._dbConn = None
@@ -117,7 +119,19 @@ class SQLCache(BaseCache):
     
     ## since SQL cache is persistent check for expired objects
     #self.expireOldCacheEntries(forceCheck=True)
-    
+  
+  def initCacheStorage(self):
+    """ Init cache backedn storage by creating needed cache table in RDBMS """
+    sql_query = self.find_table_by_name_sql %self._db_cache_table_name
+    cursor =  self.execSQLQuery(sql_query)
+    result = cursor.fetchall()
+    if 0 < len(result):
+      ## we have such table
+      pass
+    else:
+      ## no such table create it
+      self.execSQLQuery(self.create_table_sql %self._db_cache_table_name) 
+  
   def getCacheStorage(self):
     """ 
     Return current DB connection or create a new one for this thread.
@@ -139,7 +153,6 @@ class SQLCache(BaseCache):
     else:
       ## we have already dbConn for this thread 
       return dbConn
-
     
   def get(self, cache_id, scope, default=None):
     sql_query = self.get_key_sql %(self._db_cache_table_name, cache_id, scope)
diff --git a/product/ERP5Type/Tool/CacheTool.py b/product/ERP5Type/Tool/CacheTool.py
index 91565632ee..780837bcfa 100644
--- a/product/ERP5Type/Tool/CacheTool.py
+++ b/product/ERP5Type/Tool/CacheTool.py
@@ -160,14 +160,16 @@ class CacheTool(BaseTool):
   security.declareProtected(Permissions.ModifyPortalContent, 'updateCache')
   def updateCache(self, REQUEST=None):
     """ Clear and update cache structure """
-    #erp5_site_id = self.getPortalObject().getId()
     for cf in CachingMethod.factories:
-      for cp in  CachingMethod.factories[cf].getCachePluginList():
+      for cp in CachingMethod.factories[cf].getCachePluginList():
         del cp
     CachingMethod.factories = {}
     ## read configuration from ZODB
     for key,item in self.getCacheFactoryList().items():
       if len(item['cache_plugins'])!=0:
+        ## init cache backend storages
+        for cp in item["cache_plugins"]:
+          cp.initCacheStorage()
         CachingMethod.factories[key] = CacheFactory(item['cache_plugins'], item['cache_params'])    
     if REQUEST is not None:
       self.REQUEST.RESPONSE.redirect('cache_tool_configure?portal_status_message=Cache updated.')
-- 
2.30.9