CachePeer.class.php

Go to the documentation of this file.
00001 <?php
00002 /****************************************************************************
00003  *   Copyright (C) 2005-2008 by Anton E. Lebedevich, Konstantin V. Arkhipov *
00004  *                                                                          *
00005  *   This program is free software; you can redistribute it and/or modify   *
00006  *   it under the terms of the GNU Lesser General Public License as         *
00007  *   published by the Free Software Foundation; either version 3 of the     *
00008  *   License, or (at your option) any later version.                        *
00009  *                                                                          *
00010  ****************************************************************************/
00011 
00012 /*
00013     CachePeer:
00014     
00015         get from cache:
00016         
00017             abstract public function get($key)
00018         
00019         multi-get from cache:
00020         
00021             abstract public function getList($keys)
00022         
00023         increment integer value:
00024         
00025             abstract public function increment($key, $value)
00026         
00027         decrement integer value:
00028         
00029             abstract public function decrement($key, $value)
00030         
00031         uncache:
00032         
00033             abstract public function delete($key)
00034         
00035         drop everything from cache:
00036         
00037             abstract public function clean()
00038         
00039         store this data:
00040         
00041             public function set(
00042                 $key, $value, $expires = Cache::EXPIRES_MEDIUM
00043             )
00044         
00045         store this data, but only if peer *doesn't* already
00046         hold data for this key:
00047         
00048             public function add(
00049                 $key, $value, $expires = Cache::EXPIRES_MEDIUM
00050             )
00051         
00052         store this data, but only if the server *does* already
00053         hold data for this key:
00054         
00055             public function replace(
00056                 $key, $value, $expires = Cache::EXPIRES_MEDIUM
00057             )
00058         
00059         add this data to an existing key after existing data:
00060         
00061             public function append($key, $data)
00062         
00063         drop object from cache:
00064         
00065             abstract public function delete($key)
00066         
00067         check if cache alive:
00068         
00069             abstract public function isAlive()
00070     
00071     Memcached <- CachePeer:
00072     
00073         public function __construct(
00074             $host = Memcached::DEFAULT_PORT,
00075             $port = Memcached::DEFAULT_HOST,
00076             $buffer = Memcached::DEFAULT_BUFFER
00077         )
00078     
00079     PeclMemcached <- CachePeer
00080     
00081         public function __construct(
00082             $host = Memcached::DEFAULT_PORT,
00083             $port = Memcached::DEFAULT_HOST
00084         )
00085     
00086     RubberFileSystem <- CachePeer:
00087     
00088         very simple fileSystem cache
00089     
00090         public function __construct(
00091             $directory = '/tmp/onPHP/'
00092         )
00093     
00094     RuntimeMemory <- CachePeer:
00095     
00096         useful for cache fallback, when all other's peers are dead
00097         
00098         public function __construct()
00099     
00100     SharedMemory <- CachePeer:
00101     
00102         Sys-V shared memory, for memcachedless installations.
00103         
00104         public function __construct(
00105             $defaultSize = self::DEFAULT_SEGMENT_SIZE,
00106             $customSized = array() // 'className' => sizeInBytes
00107         )
00108 */
00109 
00115     abstract class CachePeer
00116     {
00117         const TIME_SWITCH       = 2592000; // 60 * 60 * 24 * 30
00118 
00119         protected $alive        = false;
00120         protected $compress     = false;
00121 
00122         abstract public function get($key);
00123         abstract public function delete($key);
00124         
00125         abstract public function increment($key, $value);
00126         abstract public function decrement($key, $value);
00127         
00128         abstract protected function store(
00129             $action, $key, $value, $expires = Cache::EXPIRES_MEDIUM
00130         );
00131         
00132         abstract public function append($key, $data);
00133         
00137         public function clean()
00138         {
00139             foreach (Singleton::getAllInstances() as $object)
00140                 if ($object instanceof GenericDAO)
00141                     $object->dropIdentityMap();
00142             
00143             return $this;
00144         }
00145         
00146         public function getList($indexes)
00147         {
00148             // intentially not array
00149             $out = null;
00150             
00151             foreach ($indexes as $key)
00152                 if (null !== ($value = $this->get($key)))
00153                     $out[] = $value;
00154             
00155             return $out;
00156         }
00157         
00158         final public function set($key, $value, $expires = Cache::EXPIRES_MEDIUM)
00159         {
00160             return $this->store('set', $key, $value, $expires);
00161         }
00162         
00163         final public function add($key, $value, $expires = Cache::EXPIRES_MEDIUM)
00164         {
00165             return $this->store('add', $key, $value, $expires);
00166         }
00167         
00168         final public function replace($key, $value, $expires = Cache::EXPIRES_MEDIUM)
00169         {
00170             return $this->store('replace', $key, $value, $expires);
00171         }
00172 
00173         public function isAlive()
00174         {
00175             return $this->alive;
00176         }
00177         
00181         public function mark($className)
00182         {
00183             return $this;
00184         }
00185         
00189         public function enableCompression()
00190         {
00191             $this->compress = true;
00192             return $this;
00193         }
00194 
00198         public function disableCompression()
00199         {
00200             $this->compress = false;
00201             return $this;
00202         }
00203 
00204         protected function prepareData($value)
00205         {
00206             if ($this->compress)
00207                 return gzcompress(serialize($value));
00208             else
00209                 return serialize($value);
00210         }
00211         
00212         protected function restoreData($value)
00213         {
00214             if ($this->compress)
00215                 return unserialize(gzuncompress($value));
00216             else
00217                 return unserialize($value);
00218         }
00219     }
00220 ?>