[Checkins] SVN: relstorage/trunk/relstorage/ added the cache-prefix option.
Shane Hathaway
shane at hathawaymix.org
Mon Oct 12 12:26:32 EDT 2009
Log message for revision 105027:
added the cache-prefix option.
Changed:
U relstorage/trunk/relstorage/adapters/oidallocator.py
U relstorage/trunk/relstorage/component.xml
U relstorage/trunk/relstorage/options.py
U relstorage/trunk/relstorage/storage.py
U relstorage/trunk/relstorage/tests/reltestbase.py
-=-
Modified: relstorage/trunk/relstorage/adapters/oidallocator.py
===================================================================
--- relstorage/trunk/relstorage/adapters/oidallocator.py 2009-10-12 16:25:01 UTC (rev 105026)
+++ relstorage/trunk/relstorage/adapters/oidallocator.py 2009-10-12 16:26:32 UTC (rev 105027)
@@ -15,7 +15,8 @@
"""
# All of these allocators allocate 16 OIDs at a time. In the sequence
-# or table, value 1 represents OID block 1-16, 2 represents OID block 17-32,
+# or table, value (n) represents (n * 16 - 15) through (n * 16). So,
+# value 1 represents OID block 1-16, 2 represents OID block 17-32,
# and so on.
from relstorage.adapters.interfaces import IOIDAllocator
Modified: relstorage/trunk/relstorage/component.xml
===================================================================
--- relstorage/trunk/relstorage/component.xml 2009-10-12 16:25:01 UTC (rev 105026)
+++ relstorage/trunk/relstorage/component.xml 2009-10-12 16:26:32 UTC (rev 105027)
@@ -171,6 +171,13 @@
"cmemcache". This setting has no effect unless cache-servers is set.
</description>
</key>
+ <key name="cache-prefix" datatype="string" required="no">
+ <description>
+ The prefix for all keys in the cache. All clients using a
+ database should use the same cache-prefix. Use this if you use
+ a single cache for multiple databases.
+ </description>
+ </key>
</sectiontype>
<sectiontype name="postgresql" implements="relstorage.adapter"
Modified: relstorage/trunk/relstorage/options.py
===================================================================
--- relstorage/trunk/relstorage/options.py 2009-10-12 16:25:01 UTC (rev 105026)
+++ relstorage/trunk/relstorage/options.py 2009-10-12 16:26:32 UTC (rev 105027)
@@ -38,6 +38,7 @@
self.pack_max_delay = 20.0
self.cache_servers = () # ['127.0.0.1:11211']
self.cache_module_name = 'memcache'
+ self.cache_prefix = ''
for key, value in kwoptions.iteritems():
if key in self.__dict__:
Modified: relstorage/trunk/relstorage/storage.py
===================================================================
--- relstorage/trunk/relstorage/storage.py 2009-10-12 16:25:01 UTC (rev 105026)
+++ relstorage/trunk/relstorage/storage.py 2009-10-12 16:26:32 UTC (rev 105027)
@@ -145,8 +145,10 @@
if isinstance(servers, basestring):
servers = servers.split()
self._cache_client = module.Client(servers)
+ self._cache_prefix = options.cache_prefix or ''
else:
self._cache_client = None
+ self._cache_prefix = ''
# _prev_polled_tid contains the tid at the previous poll
self._prev_polled_tid = None
@@ -369,10 +371,11 @@
else:
# try to load from cache
- state_key = 'state:%d' % oid_int
+ prefix = self._cache_prefix
+ state_key = '%s:state:%d' % (prefix, oid_int)
my_tid = self._prev_polled_tid
if my_tid:
- backptr_key = 'back:%d:%d' % (my_tid, oid_int)
+ backptr_key = '%s:back:%d:%d' % (prefix, my_tid, oid_int)
v = cache.get_multi([state_key, backptr_key])
if v is not None:
cache_data = v.get(state_key)
@@ -762,13 +765,14 @@
self._adapter.locker.release_commit_lock(self._store_cursor)
cache = self._cache_client
if cache is not None:
- if cache.incr('commit_count') is None:
+ cachekey = '%s:commit_count' % self._cache_prefix
+ if cache.incr(cachekey) is None:
# Use the current time as an initial commit_count value.
- cache.add('commit_count', int(time.time()))
+ cache.add(cachekey, int(time.time()))
# A concurrent committer could have won the race to set the
# initial commit_count. Increment commit_count so that it
# doesn't matter who won.
- cache.incr('commit_count')
+ cache.incr(cachekey)
self._ltid = self._tid
#if self._txn_blobs and not self._adapter.keep_history:
@@ -1117,7 +1121,8 @@
cache = self._cache_client
if cache is not None:
- new_commit_count = cache.get('commit_count')
+ new_commit_count = cache.get(
+ '%s:commit_count' % self._cache_prefix)
if new_commit_count != self._polled_commit_count:
# There is new data ready to poll
self._polled_commit_count = new_commit_count
Modified: relstorage/trunk/relstorage/tests/reltestbase.py
===================================================================
--- relstorage/trunk/relstorage/tests/reltestbase.py 2009-10-12 16:25:01 UTC (rev 105026)
+++ relstorage/trunk/relstorage/tests/reltestbase.py 2009-10-12 16:26:32 UTC (rev 105027)
@@ -228,6 +228,7 @@
# Store an object, cache it, then retrieve it from the cache
self._storage._options.cache_servers = 'x:1 y:2'
self._storage._options.cache_module_name = fakecache.__name__
+ self._storage._options.cache_prefix = 'zzz'
db = DB(self._storage)
try:
@@ -236,16 +237,16 @@
fakecache.data.clear()
r1 = c1.root()
# the root state should now be cached
- self.assertEqual(fakecache.data.keys(), ['state:0'])
+ self.assertEqual(fakecache.data.keys(), ['zzz:state:0'])
r1['alpha'] = PersistentMapping()
- self.assertFalse('commit_count' in fakecache.data)
+ self.assertFalse('zzz:commit_count' in fakecache.data)
transaction.commit()
- self.assertTrue('commit_count' in fakecache.data)
+ self.assertTrue('zzz:commit_count' in fakecache.data)
self.assertEqual(sorted(fakecache.data.keys()),
- ['commit_count', 'state:0'])
+ ['zzz:commit_count', 'zzz:state:0'])
oid = r1['alpha']._p_oid
self.assertEqual(sorted(fakecache.data.keys()),
- ['commit_count', 'state:0'])
+ ['zzz:commit_count', 'zzz:state:0'])
got, serial = c1._storage.load(oid, '')
# another state should now be cached
More information about the checkins
mailing list