Source for file Cache.php

Documentation is available at Cache.php

  1. <?php
  2. /**
  3.  * Data Structures for PHP similar to Java's HashSet and LinkedHashSet.  Uses
  4.  * memcached as a storage mechanism for persisting set data beyond the scope of
  5.  * the current request.
  6.  * 
  7.  * Requires: {@link http://www.php.net/ PHP 5},
  8.  * {@link http://www.danga.com/memcached/ memcached} and
  9.  * {@link http://pecl.php.net/memcache pecl_memcache}
  10.  * 
  11.  * License:
  12.  * 
  13.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  14.  * of this software and associated documentation files (the "Software"), to
  15.  * deal in the Software without restriction, including without limitation the
  16.  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  17.  * sell copies of the Software, and to permit persons to whom the Software is
  18.  * furnished to do so, subject to the following conditions:
  19.  * 
  20.  * The above copyright notice and this permission notice shall be included in
  21.  * all copies or substantial portions of the Software.
  22.  * 
  23.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  29.  * IN THE SOFTWARE.
  30.  * 
  31.  * @category   HighPerformance
  32.  * @package    CacheSets
  33.  * @author     Jeff Trudeau
  34.  * @copyright  Coypright (c) 2008 Jeff Trudeau
  35.  * @license    http://www.opensource.org/licenses/mit-license.php MIT License
  36.  * @filesource
  37.  */
  38.  
  39. require_once(dirname(__FILE__) . '/properties.php');
  40.  
  41. /**
  42.  * Wraps the major functionality of the Memcache class provided by the
  43.  * pecl_memcache extension.  An instance of this class should be retrieved
  44.  * using CacheFactory, which maintains a static reference throughout the
  45.  * request.
  46.  */
  47. class Cache {
  48.     
  49.     /**
  50.      * @var resource 
  51.      */
  52.     private $connection;
  53.     
  54.     /**
  55.      * @var boolean 
  56.      */
  57.     private $isConnected;
  58.     
  59.     /**
  60.      * Class constructor
  61.      */
  62.     public function __construct() {
  63.         global $cache_servers;
  64.         $this->isConnected = false;
  65.         
  66.         try {
  67.             $this->connection = new Memcache();
  68.             foreach($cache_servers as $server) {
  69.                 $this->connection->addServer($server['host'], $server['port']);
  70.             }
  71.             if ($this->connection->getStats()) {
  72.                 $this->isConnected = true;
  73.             }
  74.         }
  75.         catch (Exception $e) {
  76.             print $e->getMessage();
  77.         }
  78.     }
  79.     
  80.     /**
  81.      * Class destructor
  82.      */
  83.     public function __destruct() {
  84.         if ($this->isConnected) {
  85.             $this->connection->close();
  86.             $this->isConnected = false;
  87.         }
  88.     }
  89.     
  90.     /**
  91.      * Deletes the specified element from the cache.
  92.      * 
  93.      * @param mixed $key 
  94.      * @param int $expiration = 0
  95.      */
  96.     public function delete($key, $expiration = 0) {
  97.         if ($this->isConnected) {
  98.             $this->connection->delete($key, $expiration);
  99.         }
  100.     }
  101.     
  102.     /**
  103.      * Flushes the entire cache.
  104.      */
  105.     public function flush() {
  106.         if ($this->isConnected) {
  107.             $this->connection->flush();
  108.         }
  109.     }
  110.     
  111.     /**
  112.      * Retrieves the specified element from the cache, or null if the element
  113.      * does not exist.
  114.      * 
  115.      * @param mixed $key 
  116.      * @return mixed 
  117.      */
  118.     public function &get($key) {
  119.         $value = null;
  120.         if ($this->isConnected) {
  121.             $value = $this->connection->get($key);
  122.         }
  123.         return $value;
  124.     }
  125.     
  126.     /**
  127.      * Returns true if the cache is connected, false otherwise.
  128.      * 
  129.      * @return boolean 
  130.      */
  131.     public function isConnected() {
  132.         return $this->isConnected;
  133.     }
  134.     
  135.     /**
  136.      * Inserts/updates the specified element in the cache.
  137.      * 
  138.      * @param mixed $key 
  139.      * @param mixed $value 
  140.      * @param boolean $useCompression = false
  141.      * @param int $expiration = 0
  142.      */
  143.     public function set($key,
  144.                         &$value,
  145.                         $useCompression = false,
  146.                         $expiration = 0) {
  147.         if ($this->isConnected) {
  148.             $this->connection->set($key, $value, $useCompression, $expiration);
  149.         }
  150.     }
  151. }
  152.  
  153. ?>

Documentation generated on Thu, 14 Feb 2008 09:14:26 -0500 by phpDocumentor 1.4.1