CachePeer.class.php

Go to the documentation of this file.
00001 <?php
00002 /****************************************************************************
00003  *   Copyright (C) 2005-2007 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 /* $Id: CachePeer.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00013 /*
00014     CachePeer:
00015     
00016         get from cache:
00017 
00018             abstract public function get($key)
00019             
00020         uncache:
00021         
00022             abstract public function delete($key)
00023 
00024         drop everything from cache:
00025         
00026             abstract public function clean()
00027     
00028         store this data:
00029         
00030             public function set(
00031                 $key, $value, $expires = Cache::EXPIRES_MEDIUM
00032             )
00033 
00034         store this data, but only if peer *doesn't* already
00035         hold data for this key:
00036         
00037             public function add(
00038                 $key, $value, $expires = Cache::EXPIRES_MEDIUM
00039             )
00040         
00041         store this data, but only if the server *does* already
00042         hold data for this key:
00043         
00044             public function replace(
00045                 $key, $value, $expires = Cache::EXPIRES_MEDIUM
00046             )
00047 
00048         drop object from cache:
00049 
00050             abstract public function delete($key)
00051         
00052         check if cache alive:
00053         
00054             abstract public function isAlive()
00055 
00056     Memcached <- CachePeer:
00057     
00058         public function __construct(
00059             $host = Memcached::DEFAULT_PORT,
00060             $port = Memcached::DEFAULT_HOST,
00061             $buffer = Memcached::DEFAULT_BUFFER
00062         )
00063     
00064     PeclMemcached <- CachePeer
00065     
00066         public function __construct(
00067             $host = Memcached::DEFAULT_PORT,
00068             $port = Memcached::DEFAULT_HOST
00069         )
00070     
00071     RubberFileSystem <- CachePeer:
00072     
00073         very simple fileSystem cache
00074     
00075         public function __construct(
00076             $directory = '/tmp/onPHP/'
00077         )
00078     
00079     RuntimeMemory <- CachePeer:
00080     
00081         useful for cache fallback, when all other's peers are dead
00082         
00083         public function __construct()
00084     
00085     SharedMemory <- CachePeer:
00086     
00087         Sys-V shared memory, for memcachedless installations.
00088         
00089         public function __construct(
00090             $defaultSize = self::DEFAULT_SEGMENT_SIZE,
00091             $customSized = array() // 'className' => sizeInBytes
00092         )
00093 */
00094 
00100     abstract class CachePeer
00101     {
00102         const TIME_SWITCH       = 2592000; // 60 * 60 * 24 * 30
00103 
00104         protected $alive        = false;
00105         protected $compress     = false;
00106 
00107         abstract public function get($key);
00108         abstract public function delete($key);
00109         
00113         public function clean()
00114         {
00115             foreach (Singleton::getAllInstances() as $object)
00116                 if ($object instanceof GenericDAO)
00117                     $object->dropIdentityMap();
00118             
00119             return $this;
00120         }
00121         
00122         abstract protected function store(
00123             $action, $key, &$value, $expires = Cache::EXPIRES_MEDIUM
00124         );
00125         
00126         final public function set($key, &$value, $expires = Cache::EXPIRES_MEDIUM)
00127         {
00128             return $this->store('set', $key, $value, $expires);
00129         }
00130         
00131         final public function add($key, &$value, $expires = Cache::EXPIRES_MEDIUM)
00132         {
00133             return $this->store('add', $key, $value, $expires);
00134         }
00135         
00136         final public function replace($key, &$value, $expires = Cache::EXPIRES_MEDIUM)
00137         {
00138             return $this->store('replace', $key, $value, $expires);
00139         }
00140 
00141         public function isAlive()
00142         {
00143             return $this->alive;
00144         }
00145         
00149         public function mark($className)
00150         {
00151             return $this;
00152         }
00153         
00157         public function enableCompression()
00158         {
00159             $this->compress = true;
00160             return $this;
00161         }
00162 
00166         public function disableCompression()
00167         {
00168             $this->compress = false;
00169             return $this;
00170         }
00171 
00172         protected function prepareData(&$value)
00173         {
00174             if ($this->compress)
00175                 return gzcompress(serialize($value));
00176             else
00177                 return serialize($value);
00178         }
00179         
00180         protected function restoreData(&$value)
00181         {
00182             if ($this->compress)
00183                 return unserialize(gzuncompress($value));
00184             else
00185                 return unserialize($value);
00186         }
00187     }
00188 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4