Index: cacherouter/engines/memcache.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cacherouter/engines/memcache.php,v
retrieving revision 1.1
diff -u -r1.1 memcache.php
--- cacherouter/engines/memcache.php	31 Mar 2008 08:40:50 -0000	1.1
+++ cacherouter/engines/memcache.php	30 May 2008 01:47:33 -0000
@@ -34,10 +34,21 @@
     if (isset($cache)) {
       return $cache;
     }
-    
+ 
+    // Save invalidation info
+    static $expiry = array();
+    if (!isset($expiry[$this->name])) {
+      $expiry[$this->name] = $this->memcache->get($this->key('invalidation_' . $this->name));
+    }
+
     // Get from memcache
     $cache = $this->memcache->get($this->key($key));
-    
+
+    // Check whether key is still valid
+    if ((FALSE !== $expiry[$this->name]) && $cache->created != '' &&  $expiry[$this->name] > $cache->created) {
+      return FALSE;
+    }
+
     // Update static cache 
     parent::set($key, $cache);
     
@@ -58,42 +69,8 @@
     $cache->data = $value;
     
     if (!empty($key) && !empty($value)) {
-      if ($this->settings['shared']) {
-        $this->lock();
-
-        // Get lookup table to be able to keep track of bins
-        $lookup = $this->memcache->get('lookup_' . $this->name);
-
-        // If the lookup table is empty, initialize table
-        if (empty($lookup)) {
-          $lookup = array();
-        }
-
-        // Get full key for storage and set it to 1 so we can keep track of the bin
-        $full_key = $this->key($key);
-        $lookup[$full_key] = 1;
-
-        // Attempt to store full key and value
-        if (!$this->memcache->set($full_key, $cache, $this->settings['compress'], $expire)) {
-          unset($lookup[$full_key]);
-          $return = FALSE;
-        }
-        else {
-          // Update static cache
-          parent::set($key, $cache);
-          $return = TRUE;
-        }
-
-        // Resave the lookup table (even on failure)
-        $this->memcache->set('lookup_' . $this->name, $lookup, $this->settings['compress'], $expire);  
-
-        // Remove lock.
-        $this->unlock();
-      }
-      else {
-        // Update memcache
-        return $this->memcache->set($this->key($key), $cache, $this->settings['compress'], $expire);
-      }
+      // Update memcache
+      return $this->memcache->set($this->key($key), $cache, $this->settings['compress'], $expire);
     }
   }
   
@@ -110,32 +87,9 @@
     // Flush static cache
     parent::flush();
     
-    // If this is a shared cache, we need to cycle through the lookup table and remove individual
-    // items directly
+    // If this is a shared cache, we mark the time in memcache so that we know keys are invalid on future gets
     if ($this->settings['shared']) {
-      $this->lock();
-
-      // Get lookup table to be able to keep track of bins
-      $lookup = $this->memcache->get('lookup_' . $this->name);
-
-      // If the lookup table is empty, remove lock and return
-      if (empty($lookup)) {
-        $this->unlock();
-        return TRUE;
-      }
-
-      // Cycle through keys and remove each entry from the cache
-      foreach ($lookup as $k => $v) {
-        if ($this->memcache->delete($k)) {
-          unset($lookup[$k]);
-        }
-      }
-
-      // Resave the lookup table (even on failure)
-      $this->memcache->set('lookup_' . $this->name, $lookup, $this->settings['compress'], 0);
-
-      // Remove lock
-      $this->unlock();
+      $this->memcache->set($this->key('invalidation_' . $this->name), time());
     }
     else {
       // Flush memcache
@@ -143,25 +97,6 @@
     }
   }
   
-  function lock() {
-    // Lock once by trying to add lock file, if we can't get the lock, we will loop
-    // for 3 seconds attempting to get lock.  If we still can't get it at that point,
-    // then we give up and return FALSE.
-    if ($this->memcache->add('lock_' . $this->name, $this->settings['compress'], 0) === FALSE) {
-      $time = time();
-      while ($this->memcache->add('lock_' . $this->name, $this->settings['compress'], 0) === FALSE) {
-        if (time() - $time >= 3) {
-          return FALSE;
-        }
-      }
-    }
-    return TRUE;
-  }
-  
-  function unlock() {
-    return $this->memcache->delete('lock_' . $this->name);   
-  }
-  
   function connect() {
     $this->memcache =& new Memcache;
     foreach ($this->settings['servers'] as $server) {
@@ -175,4 +110,4 @@
   function close() {
     $this->memcache->close();
   }
-}
\ No newline at end of file
+}
