[Solar-svn] Revision 3160

pmjones at solarphp.com pmjones at solarphp.com
Fri May 16 18:57:27 CDT 2008


Solar_Cache: adding an increment() method, and one fix to the file adapter.


Solar_Cache_Adapter*
--------------------

* [ADD] Method increment() on all adapters, to increment a value in the cache.


Solar_Cache_Adapter_File
------------------------

* [FIX] Method add() now checks _isExpired().


Solar_Cacahe_Adapter_None
-------------------------

* [NEW] Dummy no-cache for when a cache injection is needed but not really used.



Modified: trunk/Solar/Cache/Adapter/Apc.php
===================================================================
--- trunk/Solar/Cache/Adapter/Apc.php	2008-05-15 19:25:23 UTC (rev 3159)
+++ trunk/Solar/Cache/Adapter/Apc.php	2008-05-16 23:57:27 UTC (rev 3160)
@@ -107,6 +107,42 @@
     
     /**
      * 
+     * Increments a cache entry value by the specified amount.  If the entry
+     * does not exist, creates it at zero, then increments it.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @param string $amt The amount to increment by (default +1).  Using
+     * negative values is effectively a decrement.
+     * 
+     * @return int The new value of the cache entry.
+     * 
+     */
+    public function increment($key, $amt = 1)
+    {
+        if (! $this->_active) {
+            return;
+        }
+        
+        // make sure we have a key to increment
+        $this->add($key, 0, null, $this->_life);
+        
+        // fetch the current value
+        $val = $this->fetch($key);
+        
+        // increment and save
+        $val += $amt;
+        $this->save($key, $val);
+        
+        // re-fetch in case someone else incremented in the interim
+        $val = $this->fetch($key);
+        
+        // done
+        return $val;
+    }
+    
+    /**
+     * 
      * Deletes a cache entry.
      * 
      * @param string $key The entry ID.

Modified: trunk/Solar/Cache/Adapter/Eaccelerator.php
===================================================================
--- trunk/Solar/Cache/Adapter/Eaccelerator.php	2008-05-15 19:25:23 UTC (rev 3159)
+++ trunk/Solar/Cache/Adapter/Eaccelerator.php	2008-05-16 23:57:27 UTC (rev 3160)
@@ -114,6 +114,42 @@
     
     /**
      * 
+     * Increments a cache entry value by the specified amount.  If the entry
+     * does not exist, creates it at zero, then increments it.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @param string $amt The amount to increment by (default +1).  Using
+     * negative values is effectively a decrement.
+     * 
+     * @return int The new value of the cache entry.
+     * 
+     */
+    public function increment($key, $amt = 1)
+    {
+        if (! $this->_active) {
+            return;
+        }
+        
+        // make sure we have a key to increment
+        $this->add($key, 0, null, $this->_life);
+        
+        // fetch the current value
+        $val = $this->fetch($key);
+        
+        // increment and save
+        $val += $amt;
+        $this->save($key, $val);
+        
+        // re-fetch in case someone else incremented in the interim
+        $val = $this->fetch($key);
+        
+        // done
+        return $val;
+    }
+    
+    /**
+     * 
      * Deletes a cache entry.
      * 
      * @param string $key The entry ID.

Modified: trunk/Solar/Cache/Adapter/File.php
===================================================================
--- trunk/Solar/Cache/Adapter/File.php	2008-05-15 19:25:23 UTC (rev 3159)
+++ trunk/Solar/Cache/Adapter/File.php	2008-05-16 23:57:27 UTC (rev 3160)
@@ -211,7 +211,6 @@
         flock($fp, LOCK_UN);
         fclose($fp);
         return true;
-        
     }
     
     /**
@@ -234,14 +233,12 @@
         // what file should we look for?
         $file = $this->entry($key);
         
-        // if the file does not exists or is unreadable, key is available
-        if (! file_exists($file) || ! is_readable($file)) {
-            return $this->save($key, $data);
-        }
+        // is the key available for adding?
+        $available = ! file_exists($file) ||
+                     ! is_readable($file) ||
+                     $this->_isExpired($file);
         
-        // if the file has expired, key is available
-        $expire_time = filemtime($file) + $this->_config['life'];
-        if (time() > $expire_time) {
+        if ($available) {
             return $this->save($key, $data);
         }
         
@@ -341,6 +338,62 @@
     
     /**
      * 
+     * Increments a cache entry value by the specified amount.  If the entry
+     * does not exist, creates it at zero, then increments it.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @param string $amt The amount to increment by (default +1).  Using
+     * negative values is effectively a decrement.
+     * 
+     * @return int The new value of the cache entry.
+     * 
+     */
+    public function increment($key, $amt = 1)
+    {
+        if (! $this->_active) {
+            return;
+        }
+        
+        // make sure we have a key to increment
+        $this->add($key, '0', null, $this->_life);
+        
+        // what file should we write to?
+        $file = $this->entry($key);
+        
+        // open the file for read/write.
+        $fp = fopen($file, 'r+b', false, $this->_context);
+        
+        // was it opened?
+        if (! $fp) {
+            return false;
+        }
+        
+        // set exclusive lock for read/write.
+        flock($fp, LOCK_EX);
+        
+        // PHP caches file lengths; clear that out so we get
+        // an accurate file length.
+        clearstatcache();
+        $len = filesize($file);
+        
+        // get the current value and increment it
+        $val = fread($fp, $len);
+        $val += $amt;
+        
+        // clear the file, rewind the pointer, and write the new value
+        ftruncate($fp, 0);
+        rewind($fp);
+        fwrite($fp, $val);
+        
+        // unlock and close, then done.
+        flock($fp, LOCK_UN);
+        fclose($fp);
+        return $val;
+    }
+    
+    /**
+     * 
      * Deletes an entry from the cache.
      * 
      * @param string $key The entry ID.

Modified: trunk/Solar/Cache/Adapter/Memcache.php
===================================================================
--- trunk/Solar/Cache/Adapter/Memcache.php	2008-05-15 19:25:23 UTC (rev 3159)
+++ trunk/Solar/Cache/Adapter/Memcache.php	2008-05-16 23:57:27 UTC (rev 3160)
@@ -240,6 +240,35 @@
     
     /**
      * 
+     * Increments a cache entry value by the specified amount.  If the entry
+     * does not exist, creates it at zero, then increments it.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @param string $amt The amount to increment by (default +1).  Using
+     * negative values is effectively a decrement.
+     * 
+     * @return int The new value of the cache entry.
+     * 
+     */
+    public function increment($key, $amt = 1)
+    {
+        if (! $this->_active) {
+            return;
+        }
+        
+        // make sure we have a key to increment
+        $this->add($key, 0, null, $this->_life);
+        
+        // let memcache do the increment and retain its value
+        $val = $this->memcache->increment($key, $amt);
+        
+        // done
+        return $val;
+    }
+    
+    /**
+     * 
      * Deletes a cache entry.
      * 
      * @param string $key The entry ID.

Added: trunk/Solar/Cache/Adapter/None.php
===================================================================
--- trunk/Solar/Cache/Adapter/None.php	                        (rev 0)
+++ trunk/Solar/Cache/Adapter/None.php	2008-05-16 23:57:27 UTC (rev 3160)
@@ -0,0 +1,118 @@
+<?php
+/**
+ * 
+ * The cache of no-cache.
+ * 
+ * @category Solar
+ * 
+ * @package Solar_Cache
+ * 
+ * @author Paul M. Jones <pmjones at solarphp.com>
+ * 
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ * 
+ * @version $Id: Var.php 3153 2008-05-05 23:14:16Z pmjones $
+ * 
+ */
+class Solar_Cache_Adapter_None extends Solar_Cache_Adapter
+{
+    /**
+     * 
+     * Sets cache entry data.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @param mixed $data The data to write into the entry.
+     * 
+     * @return bool True on success, false on failure.
+     * 
+     */
+    public function save($key, $data)
+    {
+    }
+    
+    /**
+     * 
+     * Inserts cache entry data, but only if the entry does not already exist.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @param mixed $data The data to write into the entry.
+     * 
+     * @return bool True on success, false on failure.
+     * 
+     */
+    public function add($key, $data)
+    {
+    }
+    
+    /**
+     * 
+     * Gets cache entry data.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @return mixed Boolean false on failure, cache data on success.
+     * 
+     */
+    public function fetch($key)
+    {
+    }
+    
+    /**
+     * 
+     * Increments a cache entry value by the specified amount.  If the entry
+     * does not exist, creates it at zero, then increments it.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @param string $amt The amount to increment by (default +1).  Using
+     * negative values is effectively a decrement.
+     * 
+     * @return int The new value of the cache entry.
+     * 
+     */
+    public function increment($key, $amt = 1)
+    {
+    }
+    
+    /**
+     * 
+     * Deletes a cache entry.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @return void
+     * 
+     */
+    public function delete($key)
+    {
+    }
+    
+    /**
+     * 
+     * Removes all cache entries.
+     * 
+     * Note that APC makes a distinction between "user" entries and
+     * "system" entries; this only deletes the "user" entries.
+     * 
+     * @return void
+     * 
+     */
+    public function deleteAll()
+    {
+    }
+    
+    /**
+     * 
+     * Returns the name for the entry key.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @return string The cache entry name.
+     * 
+     */
+    public function entry($key)
+    {
+    }
+}

Modified: trunk/Solar/Cache/Adapter/Var.php
===================================================================
--- trunk/Solar/Cache/Adapter/Var.php	2008-05-15 19:25:23 UTC (rev 3159)
+++ trunk/Solar/Cache/Adapter/Var.php	2008-05-16 23:57:27 UTC (rev 3160)
@@ -124,6 +124,35 @@
     
     /**
      * 
+     * Increments a cache entry value by the specified amount.  If the entry
+     * does not exist, creates it at zero, then increments it.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @param string $amt The amount to increment by (default +1).  Using
+     * negative values is effectively a decrement.
+     * 
+     * @return int The new value of the cache entry.
+     * 
+     */
+    public function increment($key, $amt = 1)
+    {
+        if (! $this->_active) {
+            return;
+        }
+        
+        // make sure we have a key to increment
+        $this->add($key, 0, null, $this->_life);
+        
+        // increment it
+        $this->_entry[$key] += $amt;
+        
+        // done!
+        return $this->_entry[$key];
+    }
+    
+    /**
+     * 
      * Deletes a cache entry.
      * 
      * @param string $key The entry ID.

Modified: trunk/Solar/Cache/Adapter/Xcache.php
===================================================================
--- trunk/Solar/Cache/Adapter/Xcache.php	2008-05-15 19:25:23 UTC (rev 3159)
+++ trunk/Solar/Cache/Adapter/Xcache.php	2008-05-16 23:57:27 UTC (rev 3160)
@@ -139,6 +139,35 @@
     
     /**
      * 
+     * Increments a cache entry value by the specified amount.  If the entry
+     * does not exist, creates it at zero, then increments it.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @param string $amt The amount to increment by (default +1).  Using
+     * negative values is effectively a decrement.
+     * 
+     * @return int The new value of the cache entry.
+     * 
+     */
+    public function increment($key, $amt = 1)
+    {
+        if (! $this->_active) {
+            return;
+        }
+        
+        // make sure we have a key to increment
+        $this->add($key, 0, null, $this->_life);
+        
+        // let xcache do the increment and retain its value
+        $val = xcache_inc($key, $amt, $this->_life);
+        
+        // done
+        return $val;
+    }
+    
+    /**
+     * 
      * Deletes a cache entry.
      * 
      * @param string $key The entry ID.

Modified: trunk/Solar/Cache/Adapter.php
===================================================================
--- trunk/Solar/Cache/Adapter.php	2008-05-15 19:25:23 UTC (rev 3159)
+++ trunk/Solar/Cache/Adapter.php	2008-05-16 23:57:27 UTC (rev 3160)
@@ -246,6 +246,21 @@
     
     /**
      * 
+     * Increments a cache entry value by the specified amount.  If the entry
+     * does not exist, creates it at zero, then increments it.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @param string $amt The amount to increment by (default +1).  Using
+     * negative values is effectively a decrement.
+     * 
+     * @return int The new value of the cache entry.
+     * 
+     */
+    abstract public function increment($key, $amt = 1);
+    
+    /**
+     * 
      * Deletes a cache entry.
      * 
      * {{code: php




More information about the Solar-svn mailing list