Source for file CacheSet.php

Documentation is available at CacheSet.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__) . '/CacheFactory.php');
  40. require_once(dirname(__FILE__) . '/properties.php');
  41.  
  42. /**
  43.  * Quasi-equivalent (superior in some repects) to Java's HashSet.  Uses
  44.  * memcached as a storage mechanism for persisting set data beyond the scope of
  45.  * the current request.
  46.  */
  47. class CacheSet {
  48.     
  49.     /**
  50.      * @var Cache 
  51.      */
  52.     protected $cache;
  53.     
  54.     /**
  55.      * @var string 
  56.      */
  57.     protected $name;
  58.     
  59.     /**
  60.      * @var array 
  61.      */
  62.     protected $set;
  63.     
  64.     /**
  65.      * Class constructor
  66.      * 
  67.      * @param string $name 
  68.      */
  69.     public function __construct($name) {
  70.         $this->cache = CacheFactory::getInstance();
  71.         $this->name = $name;
  72.         $this->load();
  73.     }
  74.     
  75.     /**
  76.      * Class destructor
  77.      */
  78.     public function __destruct() {}
  79.     
  80.     /**
  81.      * Adds the specified element to the end of the set.
  82.      * 
  83.      * @param mixed $value 
  84.      */
  85.     public function add($value) {
  86.         $this->set[] = $value;
  87.         $this->save();
  88.     }
  89.     
  90.     /**
  91.      * Returns true if the specified element is contained within the set, or
  92.      * false otherwise.
  93.      * 
  94.      * @param mixed $key 
  95.      * @return boolean 
  96.      */
  97.     public function contains($key) {
  98.         if ($this->indexOf($key) > -1) {
  99.             return true;
  100.         }
  101.         return false;
  102.     }
  103.     
  104.     /**
  105.      * Creates a copy (clone) of the internal set.  As changes to the copy are
  106.      * not persisted in the cache, this is used mainly for performing read/sort
  107.      * operations externally on the set.
  108.      * 
  109.      * @return array 
  110.      */
  111.     public function &copy() {
  112.         return $this->set;
  113.     }
  114.     
  115.     /**
  116.      * Deletes and returns the specified element from the set, or returns null
  117.      * if the element does not exist.
  118.      * 
  119.      * @param mixed $key 
  120.      * @return mixed 
  121.      */
  122.     public function delete($key) {
  123.         $value = $this->valueOf($key);
  124.         if ($value) {
  125.             $keys = array_keys($this->set);
  126.             unset($this->set[$keys[$this->indexOf($key)]]);
  127.             $this->save();
  128.         }
  129.         return $value;
  130.     }
  131.     
  132.     /**
  133.      * Returns the specified element from the set, or null if the element does
  134.      * not exist.
  135.      * 
  136.      * @param mixed $key 
  137.      * @return mixed 
  138.      */
  139.     public function get($key) {
  140.         return $this->valueOf($key);
  141.     }
  142.     
  143.     /**
  144.      * Returns the index of the element referenced by the specified key from
  145.      * the set, or -1 if the key does not exist.
  146.      * 
  147.      * @param mixed $key 
  148.      * @return int 
  149.      */
  150.     public function indexOf($key) {
  151.         $keys = array_keys($this->set);
  152.         for ($i = 0; $i < $this->size(); $i++) {
  153.             if (strval($key) == strval($i)
  154.                 || strval($key) == strval($keys[$i])) {
  155.                 return $i;
  156.             }
  157.         }
  158.         return -1;
  159.     }
  160.     
  161.     /**
  162.      * Returns whether or not the set is empty.
  163.      * 
  164.      * @return boolean 
  165.      */
  166.     public function isEmpty() {
  167.         return empty($this->set) || $this->size() == 0;
  168.     }
  169.     
  170.     /**
  171.      * Retrieves the internal set from the cache.
  172.      */
  173.     protected function load() {
  174.         $this->set = unserialize($this->cache->get($this->name));
  175.     }
  176.     
  177.     /**
  178.      * Deletes the internal set from the cache.
  179.      */
  180.     public function purge() {
  181.         $this->cache->delete($this->name);
  182.         $this->load();
  183.     }
  184.     
  185.     /**
  186.      * Persists the internal set to the cache.
  187.      */
  188.     protected function save() {
  189.         $this->cache->set($this->name,
  190.                           serialize($this->set),
  191.                           CACHE_COMPRESSION,
  192.                           CACHE_EXPIRATION);
  193.     }
  194.     
  195.     /**
  196.      * Updates the specified element's value within the set.
  197.      * 
  198.      * @param mixed $key 
  199.      * @param mixed $value 
  200.      */
  201.     public function set($key, $value) {
  202.         $this->set[$key] = $value;
  203.         $this->save();
  204.     }
  205.     
  206.     /**
  207.      * Returns the number of elements in the set.
  208.      * 
  209.      * @return int 
  210.      */
  211.     public function size() {
  212.         return count($this->set);
  213.     }
  214.     
  215.     /**
  216.      * CacheSet's internal sorting facility, which uses PHP's array value
  217.      * sorting functions.  The default assumes a non-associative set, and sorts
  218.      * in ascending order of values.
  219.      * 
  220.      * @param boolean $keepAssoc = false
  221.      * @param boolean $reverseOrder = false
  222.      * @return boolean 
  223.      */
  224.     public function sort($keepAssoc = false, $reverseOrder = false) {
  225.         $func = null;
  226.         if ($keepAssoc && $reverseOrder) {
  227.             $func = 'arsort';
  228.         }
  229.         else if ($keepAssoc) {
  230.             $func = 'asort';
  231.         }
  232.         else if ($reverseOrder) {
  233.             $func = 'rsort';
  234.         }
  235.         else {
  236.             $func = 'sort';
  237.         }
  238.         if (call_user_func($func, $this->set)) {
  239.             $this->save();
  240.             return true;
  241.         }
  242.         return false;
  243.     }
  244.     
  245.     /**
  246.      * CacheSet's internal sorting facility, which uses PHP's array key sorting
  247.      * functions.  The default sorts in ascending order of keys.
  248.      * 
  249.      * @param boolean $reverseOrder = false
  250.      * @return boolean 
  251.      */
  252.     public function sortByKey($reverseOrder = false) {
  253.         $func = $reverseOrder ? 'krsort' : 'ksort';
  254.         if (call_user_func($func, $this->set)) {
  255.             $this->save();
  256.             return true;
  257.         }
  258.         return false;
  259.     }
  260.     
  261.     /**
  262.      * Updates the internal set.  Useful in conjunction with copy() if advanced
  263.      * sorting operations are required on the set - simply use copy() to create
  264.      * a local clone of the set, perform the necessary operations on the clone,
  265.      * then use update() to refresh the structure and contents of the set.
  266.      * 
  267.      * @param mixed $set 
  268.      */
  269.     public function update(&$set) {
  270.         $this->set = $set;
  271.         $this->save();
  272.     }
  273.     
  274.     /**
  275.      * Returns the value of the element referenced by the specified key from
  276.      * the set, or null if the key does not exist.
  277.      * 
  278.      * @param mixed $key 
  279.      * @return mixed 
  280.      */
  281.     protected function valueOf($key) {
  282.         $index = $this->indexOf($key);
  283.         if ($index > -1) {
  284.             $keys = array_keys($this->set);
  285.             return $this->set[$keys[$index]];
  286.         }
  287.         return null;
  288.     }
  289. }
  290.  
  291. ?>

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