[Checkins] SVN: lovely.memcached/trunk/ see CHANGES.txt

Bernd Dorn bernd.dorn at lovelysystems.com
Tue Jul 3 08:46:03 EDT 2007


Log message for revision 77334:
  see CHANGES.txt

Changed:
  U   lovely.memcached/trunk/CHANGES.txt
  U   lovely.memcached/trunk/bootstrap.py
  U   lovely.memcached/trunk/setup.py
  U   lovely.memcached/trunk/src/lovely/memcached/README.txt
  U   lovely.memcached/trunk/src/lovely/memcached/utility.py

-=-
Modified: lovely.memcached/trunk/CHANGES.txt
===================================================================
--- lovely.memcached/trunk/CHANGES.txt	2007-07-03 09:41:50 UTC (rev 77333)
+++ lovely.memcached/trunk/CHANGES.txt	2007-07-03 12:46:02 UTC (rev 77334)
@@ -2,6 +2,13 @@
 Changes in lovely.memcached
 ===========================
 
+After 0.1.1
+===========
+
+- use a specific client uid for key tracking
+
+- more logging
+
 2007/06/19 0.1.1
 ================
 

Modified: lovely.memcached/trunk/bootstrap.py
===================================================================
--- lovely.memcached/trunk/bootstrap.py	2007-07-03 09:41:50 UTC (rev 77333)
+++ lovely.memcached/trunk/bootstrap.py	2007-07-03 12:46:02 UTC (rev 77334)
@@ -24,12 +24,15 @@
 
 tmpeggs = tempfile.mkdtemp()
 
-ez = {}
-exec urllib2.urlopen('http://peak.telecommunity.com/dist/ez_setup.py'
-                     ).read() in ez
-ez['use_setuptools'](to_dir=tmpeggs, download_delay=0)
+try:
+    import pkg_resources
+except ImportError:
+    ez = {}
+    exec urllib2.urlopen('http://peak.telecommunity.com/dist/ez_setup.py'
+                         ).read() in ez
+    ez['use_setuptools'](to_dir=tmpeggs, download_delay=0)
 
-import pkg_resources
+    import pkg_resources
 
 cmd = 'from setuptools.command.easy_install import main; main()'
 if sys.platform == 'win32':
@@ -50,3 +53,4 @@
 import zc.buildout.buildout
 zc.buildout.buildout.main(sys.argv[1:] + ['bootstrap'])
 shutil.rmtree(tmpeggs)
+

Modified: lovely.memcached/trunk/setup.py
===================================================================
--- lovely.memcached/trunk/setup.py	2007-07-03 09:41:50 UTC (rev 77333)
+++ lovely.memcached/trunk/setup.py	2007-07-03 12:46:02 UTC (rev 77334)
@@ -3,7 +3,7 @@
 
 setup (
     name='lovely.memcached',
-    version='0.1.1',
+    version='0.1.2',
     author = "Lovely Systems",
     author_email = "office at lovelysystems.com",
     description = "A memcached client utiltiy for zope 3",

Modified: lovely.memcached/trunk/src/lovely/memcached/README.txt
===================================================================
--- lovely.memcached/trunk/src/lovely/memcached/README.txt	2007-07-03 09:41:50 UTC (rev 77333)
+++ lovely.memcached/trunk/src/lovely/memcached/README.txt	2007-07-03 12:46:02 UTC (rev 77334)
@@ -104,18 +104,24 @@
   >>> import threading
   >>> log = []
 
-Each thread has a different thread.
+Each thread has a different connection and uid.
 
   >>> def differentConn():
   ...     util3.set(3,3)
-  ...     log.append(sorted(util3.keys()))
+  ...     log.append((sorted(util3.keys()), util3.storage.uid))
   ...
   >>> thread = threading.Thread(target=differentConn)
   >>> thread.start()
   >>> thread.join()
   >>> log
-  [[1, 2, 3]]
+  [([1, 2, 3], '...-...-...')]
 
+Each key aware utility has its own uid per thread.
+
+  >>> util4 = MemcachedClient(trackKeys=True)
+  >>> util4.storage.uid != log[0][1]
+  True
+
 Keys expire too
 
   >>> k = util3.set(4, 4, lifetime=1)
@@ -160,8 +166,6 @@
 be done by setting the raw keyword argument to True on the set
 and query methods.
 
-  >>> util4 = MemcachedClient()
-
 If raw is used, the key must be a string.
 
   >>> k = util.set(u'value of a', u'a', raw=True)

Modified: lovely.memcached/trunk/src/lovely/memcached/utility.py
===================================================================
--- lovely.memcached/trunk/src/lovely/memcached/utility.py	2007-07-03 09:41:50 UTC (rev 77333)
+++ lovely.memcached/trunk/src/lovely/memcached/utility.py	2007-07-03 12:46:02 UTC (rev 77334)
@@ -24,9 +24,10 @@
 import logging
 import memcache
 import cPickle
-from threading import local
+import threading
 import persistent
-
+import os
+import socket
 from zope.schema.fieldproperty import FieldProperty
 from zope import interface
 from interfaces import IMemcachedClient
@@ -210,23 +211,34 @@
         # we use a thread local storage to have a memcache client for every
         # thread.
         if not hasattr(self, '_v_storage'):
-            self._v_storage = local()
+            log.info('Creating new local storage')
+            self._v_storage = threading.local()
         if self.trackKeys and not hasattr(self._v_storage, 'keys'):
-            self._keysInit(self._v_storage)
+            tName = threading.currentThread().getName()
+            pid = os.getpid()
+            hostName = socket.gethostname()
+            uid = '%s-%s-%s' % (hostName,
+                                pid,
+                                tName)
+            self._keysInit(self._v_storage, uid)
         return self._v_storage
 
     def _instantiateClient(self, debug):
         return memcache.Client(self.servers, debug=debug)
 
-    def _keysInit(self, storage):
+    def _keysInit(self, storage, uid, clients=None):
+        log.info('Init of keytracking uid: %r' % uid)
         storage.keys = {}
-        storage.uid = random.randint(0, sys.maxint)
+        storage.uid = uid
         storage.dirtyKeys = set()
         storage.lastUpdates = {}
-        clients = self._getClients()
+        if clients is None:
+            clients = self._getClients()
         if not storage.uid in clients:
+            log.info('Adding new client uid: %r' % storage.uid)
             clients.add(storage.uid)
             self.set(clients, 'clients', lifetime=0, ns=NS)
+        return clients
 
     def _keysSet(self, key, ns, lifetime):
         """track a key"""
@@ -250,15 +262,18 @@
         res = set()
         s = self.storage
         t = time.time()
+        clients = self._getClients()
+        changed = False
+        if not s.uid in clients:
+            clients = self._keysInit(s, s.uid, clients)
         localKeys = s.keys.get(ns, set())
-        for client in self._getClients():
+        for client in clients:
             if client == s.uid:
                 v = localKeys
             else:
                 v = self.query((client, ns), default=set(), ns=NS)
             res.update(v)
         # look at the timestamps
-        changed = False
         for k in list(res):
             uid = self.query((ns, k), ns=STAMP_NS)
             if uid is None:
@@ -274,8 +289,8 @@
             s.dirtyKeys.add(ns)
             self._keysUpdate(localKeys, ns)
         return res
-        
 
+
     def _keysUpdate(self, keys, ns):
         # updates the key set of this thread on server
         s = self.storage



More information about the Checkins mailing list