Source for file LinkedCacheSet.php

Documentation is available at LinkedCacheSet.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__) . '/CacheSet.php');
  40.  
  41. /**
  42.  * Quasi-equivalent (superior in some repects) to Java's LinkedHashSet.  Uses
  43.  * memcached as a storage mechanism for persisting set data beyond the scope of
  44.  * the current request.
  45.  */
  46. class LinkedCacheSet extends CacheSet {
  47.     
  48.     /**
  49.      * Class constructor
  50.      * 
  51.      * @param string $name 
  52.      */
  53.     public function __construct($name) {
  54.         parent::__construct($name);
  55.         $this->last();
  56.     }
  57.     
  58.     /**
  59.      * Class destructor
  60.      */
  61.     public function __destruct() {}
  62.     
  63.     /**
  64.      * Adds the specified element to the end of the set.  The internal pointer
  65.      * is updated to the position of the element (last in the set).
  66.      * 
  67.      * @param mixed $key 
  68.      * @param mixed $value 
  69.      */
  70.     public function add($value) {
  71.         parent::add($value);
  72.         $this->last();
  73.     }
  74.     
  75.     /**
  76.      * Returns the element referenced by the internal pointer, or null if there
  77.      * is no element at the current position.
  78.      * 
  79.      * @return mixed 
  80.      */
  81.     public function current() {
  82.         return current($this->set);
  83.     }
  84.     
  85.     /**
  86.      * Deletes and returns the specified element from the set, or returns null
  87.      * if the element does not exist.  The internal pointer is updated to the
  88.      * first element in the set.
  89.      * 
  90.      * @param mixed $key 
  91.      * @return mixed 
  92.      */
  93.     public function delete($key) {
  94.         $value = parent::delete($key);
  95.         $this->first();
  96.         return $value;
  97.     }
  98.     
  99.     /**
  100.      * Updates the internal pointer to the first element in the set and returns
  101.      * that element, or false if the set is empty.
  102.      * 
  103.      * @return mixed 
  104.      */
  105.     public function first() {
  106.         return reset($this->set);
  107.     }
  108.     
  109.     /**
  110.      * Updates the internal pointer to the last element in the set and returns
  111.      * that element, or false if the set is empty.
  112.      * 
  113.      * @return mixed 
  114.      */
  115.     public function last() {
  116.         if (!$this->isEmpty()) {
  117.             return end($this->set);
  118.         }
  119.         return false;
  120.     }
  121.     
  122.     /**
  123.      * Moves the internal pointer by a magnitude of $delta.  The default is to
  124.      * move the pointer in a forward direction.
  125.      * 
  126.      * @param int $delta 
  127.      * @param boolean $forward = true
  128.      */
  129.     protected function move($delta, $forward = true) {
  130.         for ($i = 0; $i < $delta; $i++) {
  131.             $forward ? $this->next() : $this->previous();
  132.         }
  133.     }
  134.     
  135.     /**
  136.      * Updates the internal pointer to the next element in the set and returns
  137.      * that element, or false if there is no element at that location.
  138.      * 
  139.      * @return mixed 
  140.      */
  141.     public function next() {
  142.         return next($this->set);
  143.     }
  144.     
  145.     /**
  146.      * Updates the internal pointer to the previous element in the set and
  147.      * returns that element, or false if there is no element at that location.
  148.      * 
  149.      * @return mixed 
  150.      */
  151.     public function previous() {
  152.         return prev($this->set);
  153.     }
  154.     
  155.     /**
  156.      * Updates the internal pointer to the specified element in the set and
  157.      * returns that element, or returns false if key does not exist.
  158.      * 
  159.      * @param mixed $key 
  160.      * @return mixed 
  161.      */
  162.     public function seek($key) {
  163.         $keyIndex = $this->indexOf($key);
  164.         if ($keyIndex > -1) {
  165.             $index = $this->indexOf(key($this->set));
  166.             if ($index < $keyIndex) {
  167.                 $this->move($keyIndex - $index);
  168.             }
  169.             else if ($index > $keyIndex) {
  170.                 $this->move($index - $keyIndex, false);
  171.             }
  172.             return $this->current();
  173.         }
  174.         return false;
  175.     }
  176.     
  177.     /**
  178.      * Updates the specified element's value within the set.  The internal
  179.      * pointer is updated to the position of the element.
  180.      * 
  181.      * @param mixed $key 
  182.      * @param mixed $value 
  183.      */
  184.     public function set($key, $value) {
  185.         parent::set($key, $value);
  186.         $this->seek($key);
  187.     }
  188. }
  189.  
  190. ?>

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