[Solar-svn] Revision 2809

pmjones at solarphp.com pmjones at solarphp.com
Sat Oct 6 09:22:07 CDT 2007


Solar_Cache_Adapter_Var: [NEW] A weak cache controller that stores cache entries to an object property.  Of course, when the script ends, the cache goes away.

also, whitespace and comment changes


Modified: trunk/Solar/Cache/Adapter/Apc.php
===================================================================
--- trunk/Solar/Cache/Adapter/Apc.php	2007-10-06 14:20:23 UTC (rev 2808)
+++ trunk/Solar/Cache/Adapter/Apc.php	2007-10-06 14:22:07 UTC (rev 2809)
@@ -1,45 +1,45 @@
 <?php
 /**
- *
+ * 
  * APC cache controller.
- *
+ * 
  * @category Solar
- *
+ * 
  * @package Solar_Cache
- *
+ * 
  * @author Rodrigo Moraes <rodrigo.moraes at gmail.com>
- *
+ * 
  * @license http://opensource.org/licenses/bsd-license.php BSD
- *
+ * 
  * @version $Id$
- *
+ * 
  */
 
 /**
- *
+ * 
  * APC cache controller.
- *
+ * 
  * The Alternative PHP Cache (APC) is a free and open opcode cache for PHP.
  * It was conceived of to provide a free, open, and robust framework for
  * caching and optimizing PHP intermediate code.
- *
+ * 
  * The APC extension is not bundled with PHP; you will need to install it
  * on your server before you can use it.
  * More info on the [APC homepage](http://pecl.php.net/package/apc).
- *
+ * 
  * @category Solar
- *
+ * 
  * @package Solar_Cache
- *
+ * 
  */
 class Solar_Cache_Adapter_Apc extends Solar_Cache_Adapter {
-
+    
     /**
-     *
+     * 
      * Constructor.
-     *
+     * 
      * @param array $config User-provided configuration values.
-     *
+     * 
      */
     public function __construct($config = null)
     {
@@ -50,21 +50,21 @@
                 array('extension' => 'apc')
             );
         }
-
+        
         // we're ok
         parent::__construct($config);
     }
-
+    
     /**
-     *
+     * 
      * 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)
     {
@@ -74,7 +74,7 @@
         
         return apc_store($key, $data, $this->_life);
     }
-
+    
     /**
      * 
      * Inserts cache entry data, but only if the entry does not already exist.
@@ -104,13 +104,13 @@
     }
     
     /**
-     *
+     * 
      * Gets cache entry data.
-     *
+     * 
      * @param string $key The entry ID.
-     *
+     * 
      * @return mixed Boolean false on failure, cache data on success.
-     *
+     * 
      */
     public function fetch($key)
     {
@@ -120,15 +120,15 @@
         
         return apc_fetch($key);
     }
-
+    
     /**
-     *
+     * 
      * Deletes a cache entry.
-     *
+     * 
      * @param string $key The entry ID.
-     *
+     * 
      * @return void
-     *
+     * 
      */
     public function delete($key)
     {
@@ -138,16 +138,16 @@
         
         apc_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()
     {
@@ -157,15 +157,15 @@
         
         apc_clear_cache('user');
     }
-
+    
     /**
-     *
+     * 
      * 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/Eaccelerator.php
===================================================================
--- trunk/Solar/Cache/Adapter/Eaccelerator.php	2007-10-06 14:20:23 UTC (rev 2808)
+++ trunk/Solar/Cache/Adapter/Eaccelerator.php	2007-10-06 14:22:07 UTC (rev 2809)
@@ -1,29 +1,29 @@
 <?php
 /**
- *
+ * 
  * eAccelerator cache controller.
- *
+ * 
  * @category Solar
- *
+ * 
  * @package Solar_Cache
- *
+ * 
  * @author Rodrigo Moraes <rodrigo.moraes at gmail.com>
- *
+ * 
  * @license http://opensource.org/licenses/bsd-license.php BSD
- *
+ * 
  * @version $Id$
- *
+ * 
  */
 
 /**
- *
+ * 
  * eAccellerator cache controller.
- *
+ * 
  * eAccelerator is a free open-source PHP accelerator, optimizer,
  * encoder and dynamic content cache. It increases the performance of
  * PHP scripts by caching them in their compiled state, so that the
  * overhead of compiling is almost completely eliminated.
- *
+ * 
  * eAccelerator is not bundled with PHP; you will need to install it
  * on your server before you can use it.  More info on the
  * [eAccelerator homepage](http://eaccelerator.net/).
@@ -37,13 +37,13 @@
  * 
  */
 class Solar_Cache_Adapter_Eaccelerator extends Solar_Cache_Adapter {
-
+    
     /**
-     *
+     * 
      * Constructor.
-     *
+     * 
      * @param array $config User-provided configuration values.
-     *
+     * 
      */
     public function __construct($config = null)
     {
@@ -54,22 +54,22 @@
                 array('extension' => 'eaccelerator')
             );
         }
-
+        
         // we're ok
         parent::__construct($config);
     }
-
+    
     /**
-     *
+     * 
      * Sets cache entry data. eAccelerator doesn't serialize object, so
      * you need to do it yourself or php will segfault on object retrieval.
-     *
+     * 
      * @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)
     {
@@ -79,7 +79,7 @@
         
         return eaccelerator_put($key, $data, $this->_life);
     }
-
+    
     /**
      * 
      * Inserts cache entry data, but only if the entry does not already exist.
@@ -105,13 +105,13 @@
     }
     
     /**
-     *
+     * 
      * Gets cache entry data.
-     *
+     * 
      * @param string $key The entry ID.
-     *
+     * 
      * @return mixed Boolean false on failure, string on success.
-     *
+     * 
      */
     public function fetch($key)
     {
@@ -121,15 +121,15 @@
         
         return eaccelerator_get($key);
     }
-
+    
     /**
-     *
+     * 
      * Deletes a cache entry.
-     *
+     * 
      * @param string $key The entry ID.
-     *
+     * 
      * @return void
-     *
+     * 
      */
     public function delete($key)
     {
@@ -139,13 +139,13 @@
         
         eaccelerator_rm($key);
     }
-
+    
     /**
-     *
+     * 
      * Removes all cache entries.
-     *
+     * 
      * @return void
-     *
+     * 
      */
     public function deleteAll()
     {
@@ -155,15 +155,15 @@
         
         eaccelerator_clean();
     }
-
+    
     /**
-     *
+     * 
      * 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/Memcache.php
===================================================================
--- trunk/Solar/Cache/Adapter/Memcache.php	2007-10-06 14:20:23 UTC (rev 2808)
+++ trunk/Solar/Cache/Adapter/Memcache.php	2007-10-06 14:22:07 UTC (rev 2809)
@@ -339,7 +339,7 @@
             if ($result === true) {
                 $connection_count++;
             }
-
+        
         }
         
         // make sure we connected to at least one

Copied: trunk/Solar/Cache/Adapter/Var.php (from rev 2801, branches/orm/Solar/Cache/Adapter/Var.php)
===================================================================
--- trunk/Solar/Cache/Adapter/Var.php	                        (rev 0)
+++ trunk/Solar/Cache/Adapter/Var.php	2007-10-06 14:22:07 UTC (rev 2809)
@@ -0,0 +1,187 @@
+<?php
+/**
+ * 
+ * Variable (in-memory) cache controller.
+ * 
+ * @category Solar
+ * 
+ * @package Solar_Cache
+ * 
+ * @author Paul M. Jones <pmjones at solarphp.com>
+ * 
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ * 
+ * @version $Id$
+ * 
+ */
+
+/**
+ * 
+ * Variable (in-memory) cache controller.
+ * 
+ * Stores cache entries to an object variable.  This means that entries are
+ * available for the duration of the script, but are cleared out at the end
+ * of the script.
+ * 
+ * @category Solar
+ * 
+ * @package Solar_Cache
+ * 
+ */
+class Solar_Cache_Adapter_Var extends Solar_Cache_Adapter {
+    
+    /**
+     * 
+     * Cache entries.
+     * 
+     * @var array
+     * 
+     */
+    protected $_entry = array();
+    
+    /**
+     * 
+     * Expiration timestamps for each cache entry.
+     * 
+     * @var array
+     * 
+     */
+    protected $_expires = array();
+    
+    /**
+     * 
+     * Constructor.
+     * 
+     * @param array $config User-provided configuration values.
+     * 
+     */
+    public function __construct($config = null)
+    {
+        parent::__construct($config);
+    }
+    
+    /**
+     * 
+     * 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)
+    {
+        if (! $this->_active) {
+            return;
+        }
+        
+        $this->_entry[$key] = $data;
+        $this->_expires[$key] = time() + $this->_life;
+        return true;
+    }
+    
+    /**
+     * 
+     * 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)
+    {
+        if (! $this->_active) {
+            return;
+        }
+        
+        if (empty($this->_entry[$key])) {
+            return $this->save($key, $data);
+        } else {
+            return false;
+        }
+    }
+    
+    /**
+     * 
+     * Gets cache entry data.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @return mixed Boolean false on failure, cache data on success.
+     * 
+     */
+    public function fetch($key)
+    {
+        if (! $this->_active) {
+            return;
+        }
+        
+        if (! empty($this->_entry[$key]) && $this->_expires[$key] <= time()) {
+            // exists, and is within its lifetime
+            return $this->_entry[$key];
+        } else {
+            // clear the entry
+            unset($this->_entry[$key]);
+            unset($this->_expires[$key]);
+            return false;
+        }
+    }
+    
+    /**
+     * 
+     * Deletes a cache entry.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @return void
+     * 
+     */
+    public function delete($key)
+    {
+        if (! $this->_active) {
+            return;
+        }
+        
+        unset($this->_entry[$key]);
+        unset($this->_expires[$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()
+    {
+        if (! $this->_active) {
+            return;
+        }
+        
+        $this->_entry = array();
+        $this->_expires = array();
+    }
+    
+    /**
+     * 
+     * Returns the name for the entry key.
+     * 
+     * @param string $key The entry ID.
+     * 
+     * @return string The cache entry name.
+     * 
+     */
+    public function entry($key)
+    {
+        return $key;
+    }
+}

Modified: trunk/Solar/Cache/Adapter/Xcache.php
===================================================================
--- trunk/Solar/Cache/Adapter/Xcache.php	2007-10-06 14:20:23 UTC (rev 2808)
+++ trunk/Solar/Cache/Adapter/Xcache.php	2007-10-06 14:22:07 UTC (rev 2809)
@@ -1,33 +1,33 @@
 <?php
 /**
- *
+ * 
  * Xcache cache controller.
- *
+ * 
  * @category Solar
- *
+ * 
  * @package Solar_Cache
- *
+ * 
  * @author Rodrigo Moraes <rodrigo.moraes at gmail.com>
- *
+ * 
  * @license http://opensource.org/licenses/bsd-license.php BSD
- *
+ * 
  * @version $Id$
- *
+ * 
  */
 
 /**
- *
+ * 
  * XCache cache controller.
- *
+ * 
  * XCache is a fast, stable PHP opcode cacher tested and supported on
  * all of the latest PHP cvs branches.
- *
+ * 
  * The XCache extension is not bundled with PHP; you will need to
  * install it on your server before you can use it. More info on the
  * [XCache homepage](http://trac.lighttpd.net/xcache/wiki/).
- *
+ * 
  * @category Solar
- *
+ * 
  * @package Solar_Cache
  * 
  * @todo Does not work with objects.  Need to add custom support for them.
@@ -35,41 +35,41 @@
  * 
  */
 class Solar_Cache_Adapter_Xcache extends Solar_Cache_Adapter {
-
+    
     /**
-     *
+     * 
      * User-provided configuration.
-     *
+     * 
      * Keys are ...
-     *
+     * 
      * `life`
      * : (int) The cache entry lifetime in seconds, default `0`
      * (never expires).
-     *
+     * 
      * `user`
      * : (string) Admin user name for Xcache, as set in php.ini. This login
      *   and the corresponding password are required _only_ for the deleteAll()
      *   method. Defaults to `null`.
-     *
+     * 
      * `pass`
      * : (string) Plaintext password that matches the MD5-encrypted password
      *   in php.ini. This password and the corresponding login are required
      *   _only_ for the deleteAll() method. Defaults to `null`.
-     *
+     * 
      * @var array
-     *
+     * 
      */
     protected $_Solar_Cache_Adapter_Xcache = array(
         'user' => null,
         'pass' => null
     );
-
+    
     /**
-     *
+     * 
      * Constructor.
-     *
+     * 
      * @param array $config User-provided configuration values.
-     *
+     * 
      */
     public function __construct($config = null)
     {
@@ -80,21 +80,21 @@
                 array('extension' => 'xcache')
             );
         }
-
+        
         // we're ok
         parent::__construct($config);
     }
-
+    
     /**
-     *
+     * 
      * 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)
     {
@@ -104,7 +104,7 @@
         
         return xcache_set($key, $data, $this->_life);
     }
-
+    
     /**
      * 
      * Inserts cache entry data, but only if the entry does not already exist.
@@ -130,13 +130,13 @@
     }
     
     /**
-     *
+     * 
      * Gets cache entry data.
-     *
+     * 
      * @param string $key The entry ID.
-     *
+     * 
      * @return mixed NULL on failure, cache data on success.
-     *
+     * 
      */
     public function fetch($key)
     {
@@ -146,15 +146,15 @@
         
         return xcache_get($key);
     }
-
+    
     /**
-     *
+     * 
      * Deletes a cache entry.
-     *
+     * 
      * @param string $key The entry ID.
-     *
+     * 
      * @return bool true on successful deletion, false on failure
-     *
+     * 
      */
     public function delete($key)
     {
@@ -164,16 +164,16 @@
         
         return xcache_unset($key);
     }
-
+    
     /**
-     *
+     * 
      * Removes all cache entries.
      * 
      * Note that Xcache makes a distinction between "user" entries and
      * "system" or "script" entries; this deletes only "user" entries.
      * 
      * @return bool true on success, false on failure
-     *
+     * 
      */
     public function deleteAll()
     {
@@ -199,7 +199,7 @@
         // force credentials to the configured values
         $_SERVER['PHP_AUTH_USER'] = $this->_config['user'];
         $_SERVER['PHP_AUTH_PW'] = $this->_config['pass'];
-
+        
         // clear user cache
         $vcnt = xcache_count(XC_TYPE_VAR);
         for ($i = 0; $i < $vcnt; $i++) {
@@ -207,7 +207,7 @@
                 return false;
             }
         }
-
+        
         // Restore creds to prior state
         if ($olduser !== null) {
             $_SERVER['PHP_AUTH_USER'] = $olduser;
@@ -220,18 +220,18 @@
         } else {
             $_SERVER['PHP_AUTH_PW'] = null;
         }
-
+        
         return true;
     }
-
+    
     /**
-     *
+     * 
      * Returns the name for the entry key.
-     *
+     * 
      * @param string $key The entry ID.
-     *
+     * 
      * @return string The cache entry name.
-     *
+     * 
      */
     public function entry($key)
     {




More information about the Solar-svn mailing list