[Erp5-report] r34441 nicolas - in /erp5/trunk/products/ERP5Type: CachePlugins/ Tool/

nobody at svn.erp5.org nobody at svn.erp5.org
Fri Apr 9 17:48:40 CEST 2010


Author: nicolas
Date: Fri Apr  9 17:48:40 2010
New Revision: 34441

URL: http://svn.erp5.org?rev=34441&view=rev
Log:
Fix the way to access memcached.Client instances stored on connection_pool.

If the configuration of memcached.Client instances are different
to each other inside the same thread, the first created one is allways returned.
So this patch store the instances inside a intermediate 
dictionary with keys build with discriminatory criterions.

Modified:
    erp5/trunk/products/ERP5Type/CachePlugins/DistributedRamCache.py
    erp5/trunk/products/ERP5Type/Tool/MemcachedTool.py

Modified: erp5/trunk/products/ERP5Type/CachePlugins/DistributedRamCache.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/CachePlugins/DistributedRamCache.py?rev=34441&r1=34440&r2=34441&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/CachePlugins/DistributedRamCache.py [utf8] (original)
+++ erp5/trunk/products/ERP5Type/CachePlugins/DistributedRamCache.py [utf8] Fri Apr  9 17:48:40 2010
@@ -39,12 +39,11 @@
 from base64 import encodestring
 
 try:
-  import memcache
   from Products.ERP5Type.Tool.MemcachedTool import MemcachedDict, SharedDict
 except ImportError:
   LOG('DistributedRamCache', 0, 'unable to import memcache')
 
-## global ditionary containing connection objects
+## global dictionary containing connection objects
 connection_pool = local()
 
 _MARKER = []
@@ -69,21 +68,29 @@
     ## cache storage is a memcached server and no need to init it
     pass
 
+  def _getMemcachedDict(self):
+    """return a threading safe MemcachedDict instance
+    """
+    configuration_key = (self._servers, self._server_max_key_length,
+                         self._server_max_value_length,
+                         self._debug_level, self._key_prefix)
+    try:
+      local_dict = connection_pool.local_dict
+    except AttributeError:
+      local_dict = connection_pool.local_dict = {}
+    try:
+      dictionary = local_dict[configuration_key]
+    except KeyError:
+      dictionary = MemcachedDict(self._servers.split('\n'),
+                      server_max_key_length=self._server_max_key_length,
+                      server_max_value_length=self._server_max_value_length)
+      local_dict[configuration_key] = dictionary
+    return dictionary
+
   def getCacheStorage(self, **kw):
-    ## if we use one connection object this causes 
-    ## "MemCached: while expecting 'STORED', got unexpected response 'END'"
-    ## messages in log files and can sometimes can block the thread. 
-    ## For the moment we create a new conn object for every thread.
-    try:
-      dictionary = connection_pool.memcached_dict
-    except AttributeError:
-      dictionary = SharedDict(
-        MemcachedDict(self._servers.split('\n'),
-                      server_max_key_length=self._server_max_key_length,
-                      server_max_value_length=self._server_max_value_length),
-        prefix=self._key_prefix)
-      connection_pool.memcached_dict = dictionary
-    return dictionary
+    """Follow MemcachedTool.getMemcachedDict implementation
+    """
+    return SharedDict(self._getMemcachedDict(), prefix=self._key_prefix)
 
   def _getCacheId(self, cache_id, scope):
     return '%s_%s' % (scope, cache_id)

Modified: erp5/trunk/products/ERP5Type/Tool/MemcachedTool.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Type/Tool/MemcachedTool.py?rev=34441&r1=34440&r2=34441&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Type/Tool/MemcachedTool.py [utf8] (original)
+++ erp5/trunk/products/ERP5Type/Tool/MemcachedTool.py [utf8] Fri Apr  9 17:48:40 2010
@@ -279,15 +279,19 @@
         Create it if does not exist.
       """
       try:
-        dictionary = memcached_dict_pool.memcached_dict
+        local_dict = memcached_dict_pool.local_dict
       except AttributeError:
+        local_dict = memcached_dict_pool.local_dict = {}
+      try:
+        dictionary = local_dict[plugin_path]
+      except KeyError:
         memcached_plugin = self.restrictedTraverse(plugin_path, None)
         if memcached_plugin is None:
           raise ValueError, 'Memcached Plugin does not exists: %r' % (plugin_path,)
         dictionary = MemcachedDict((memcached_plugin.getUrlString(''),),
                    server_max_key_length=memcached_plugin.getServerMaxKeyLength(),
                    server_max_value_length=memcached_plugin.getServerMaxValueLength())
-        memcached_dict_pool.memcached_dict = dictionary
+        local_dict[plugin_path] = dictionary
       return dictionary
 
     security.declareProtected(Permissions.AccessContentsInformation, 'getMemcachedDict')




More information about the Erp5-report mailing list