From a0eefb1359ca10d40ec2f95bfb314449d036d5ce Mon Sep 17 00:00:00 2001
From: Kazuhiko Shiozaki <kazuhiko@nexedi.com>
Date: Fri, 9 Oct 2009 13:50:00 +0000
Subject: [PATCH] * use Quoted Printable encode instead of 'removing invalid
 characters' to encode keys for memcached. * add a global prefix that makes it
 possible to share the same memcached with different ERP5 instances.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@29531 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Type/Tool/MemcachedTool.py | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/product/ERP5Type/Tool/MemcachedTool.py b/product/ERP5Type/Tool/MemcachedTool.py
index 11a09f2653..876d59081d 100644
--- a/product/ERP5Type/Tool/MemcachedTool.py
+++ b/product/ERP5Type/Tool/MemcachedTool.py
@@ -32,6 +32,7 @@ from Products.ERP5Type.Tool.BaseTool import BaseTool
 from Products.ERP5Type import Permissions, _dtmldir
 from AccessControl import ClassSecurityInfo
 from Globals import DTMLFile
+from quopri import encodestring
 
 MEMCACHED_TOOL_MODIFIED_FLAG_PROPERTY_ID = '_v_memcached_edited'
 
@@ -42,14 +43,12 @@ except ImportError:
 
 def encodeKey(key):
   """
-    Encode the key. The current encoding is not very good
-    since it is not bijective. Implementing a bijective
-    encoding is required.
+    Encode the key like 'Quoted Printable'.
   """
-  # Memcached refuses characters which are below ' ' (included) in
-  # ascii table. Just strip them here to avoid the raise.
-  return ''.join([x for x in key if ord(x) > \
-                              MEMCACHED_MINIMUM_KEY_CHAR_ORD])
+  # According to the memcached's protocol.txt, the key cannot contain
+  # control characters and white spaces.
+  return encodestring(key, True).replace('\n', '').replace('\r', '')
+
 memcached_dict_pool = local()
 if memcache is not None:
   # Real memcache tool
@@ -299,6 +298,9 @@ if memcache is not None:
         plugin_path
           relative_url of dedicated Memcached Plugin
       """
+      global_prefix = getattr(self, 'erp5_site_global_id', '')
+      if global_prefix:
+        key_prefix = '%s_%s' % (global_prefix, key_prefix)
       return SharedDict(self._getMemcachedDict(plugin_path), prefix=key_prefix)
 
 else:
-- 
2.30.9