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:
except ImportError:
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:
# Real memcache tool
import memcache
......@@ -103,9 +114,9 @@ if memcache is not None:
self.scheduled_action_dict[key] = UPDATE_ACTION
for key, action in self.scheduled_action_dict.iteritems():
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:
self.memcached_connection.delete(key, 0)
self.memcached_connection.delete(encodeKey(key), 0)
except:
LOG('MemcachedDict', 0, 'An exception occured during _finish : %s' % (traceback.format_exc(), ))
self.scheduled_action_dict.clear()
......@@ -125,15 +136,12 @@ if memcache is not None:
# We need to register in this function too to be able to flush cache at
# transaction end.
self._register()
# Memcached refuses characters which are below ' ' (inclued) in
# 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])
encoded_key = encodeKey(key)
result = self.local_cache.get(key, MARKER)
if result is MARKER:
result = self.memcached_connection.get(stripped_key)
result = self.memcached_connection.get(encoded_key)
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
return result
......
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