Commit 273e017d authored by Jean-Paul Smets's avatar Jean-Paul Smets

Encode keys before trying to access the memcache tool. This encoding is just a...

Encode keys before trying to access the memcache tool. This encoding is just a temporary version. It is not bijective. A fully bijective encoding is required.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@16731 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 030f4522
...@@ -38,6 +38,17 @@ try: ...@@ -38,6 +38,17 @@ try:
except ImportError: except ImportError:
memcache = None memcache = None
def encodeKey(key):
"""
Encode the key. The current encoding is not very good
since it is not bijective. Implementing a bijective
encoding is required.
"""
# 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])
if memcache is not None: if memcache is not None:
# Real memcache tool # Real memcache tool
import memcache import memcache
...@@ -103,9 +114,9 @@ if memcache is not None: ...@@ -103,9 +114,9 @@ if memcache is not None:
self.scheduled_action_dict[key] = UPDATE_ACTION self.scheduled_action_dict[key] = UPDATE_ACTION
for key, action in self.scheduled_action_dict.iteritems(): for key, action in self.scheduled_action_dict.iteritems():
if action is UPDATE_ACTION: if action is UPDATE_ACTION:
self.memcached_connection.set(key, self.local_cache[key], 0) self.memcached_connection.set(encodeKey(key), self.local_cache[key], 0)
elif action is DELETE_ACTION: elif action is DELETE_ACTION:
self.memcached_connection.delete(key, 0) self.memcached_connection.delete(encodeKey(key), 0)
except: except:
LOG('MemcachedDict', 0, 'An exception occured during _finish : %s' % (traceback.format_exc(), )) LOG('MemcachedDict', 0, 'An exception occured during _finish : %s' % (traceback.format_exc(), ))
self.scheduled_action_dict.clear() self.scheduled_action_dict.clear()
...@@ -125,15 +136,12 @@ if memcache is not None: ...@@ -125,15 +136,12 @@ if memcache is not None:
# We need to register in this function too to be able to flush cache at # We need to register in this function too to be able to flush cache at
# transaction end. # transaction end.
self._register() self._register()
# Memcached refuses characters which are below ' ' (inclued) in encoded_key = encodeKey(key)
# ascii table. Just strip them here to avoid the raise.
stripped_key = ''.join([x for x in key if ord(x) > \
MEMCACHED_MINIMUM_KEY_CHAR_ORD])
result = self.local_cache.get(key, MARKER) result = self.local_cache.get(key, MARKER)
if result is MARKER: if result is MARKER:
result = self.memcached_connection.get(stripped_key) result = self.memcached_connection.get(encoded_key)
if result is None: if result is None:
raise KeyError, 'Key %s (was %s) not found.' % (stripped_key, key) raise KeyError, 'Key %s (was %s) not found.' % (encoded_key, key)
self.local_cache[key] = result self.local_cache[key] = result
return result return result
...@@ -172,7 +180,7 @@ if memcache is not None: ...@@ -172,7 +180,7 @@ if memcache is not None:
return self.__getitem__(key) return self.__getitem__(key)
except KeyError: except KeyError:
return default return default
class SharedDict: class SharedDict:
""" """
Class to make possible for multiple "users" to store data in the same Class to make possible for multiple "users" to store data in the same
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment