WatermarkedPeer.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2008 by 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 
00017     final class WatermarkedPeer extends SelectivePeer
00018     {
00019         private $peer       = null;
00020         private $watermark  = null;
00021         
00023         private $map        = null;
00024         
00028         public static function create(
00029             CachePeer $peer,
00030             $watermark = "Single onPHP's project"
00031         )
00032         {
00033             return new self($peer, $watermark);
00034         }
00035         
00036         public function __construct(
00037             CachePeer $peer,
00038             $watermark = "Single onPHP's project"
00039         )
00040         {
00041             $this->peer = $peer;
00042             $this->setWatermark($watermark);
00043         }
00044         
00045         public function setWatermark($watermark)
00046         {
00047             $this->watermark = md5($watermark.' ['.ONPHP_VERSION.']::');
00048             
00049             return $this;
00050         }
00051         
00052         public function getWatermark()
00053         {
00054             return $this->watermark;
00055         }
00056         
00057         public function getActualWatermark()
00058         {
00059             if (
00060                 $this->className
00061                 && isset($this->map[$this->className])
00062             )
00063                 return $this->map[$this->className];
00064             
00065             return $this->watermark;
00066         }
00067         
00073         public function setClassMap($map)
00074         {
00075             $this->map = array();
00076             
00077             foreach ($map as $className => $watermark)
00078                 $this->map[$className] = md5($watermark.' ['.ONPHP_VERSION.']::');
00079         
00080             return $this;
00081         }
00082         
00086         public function mark($className)
00087         {
00088             $this->className = $className;
00089             
00090             $this->peer->mark($this->getActualWatermark().$className);
00091             
00092             return $this;
00093         }
00094         
00095         public function increment($key, $value)
00096         {
00097             return $this->peer->increment(
00098                 $this->getActualWatermark().$key,
00099                 $value
00100             );
00101         }
00102         
00103         public function decrement($key, $value)
00104         {
00105             return $this->peer->decrement(
00106                 $this->getActualWatermark().$key,
00107                 $value
00108             );
00109         }
00110         
00111         public function getList($indexes)
00112         {
00113             foreach ($indexes as &$index)
00114                 $index = $this->getActualWatermark().$index;
00115             
00116             return $this->peer->getList($indexes);
00117         }
00118         
00119         public function get($key)
00120         {
00121             return $this->peer->get($this->getActualWatermark().$key);
00122         }
00123         
00124         public function delete($key)
00125         {
00126             return $this->peer->delete($this->getActualWatermark().$key);
00127         }
00128         
00132         public function clean()
00133         {
00134             $this->peer->clean();
00135             
00136             return parent::clean();
00137         }
00138         
00139         public function isAlive()
00140         {
00141             return $this->peer->isAlive();
00142         }
00143         
00144         public function append($key, $data)
00145         {
00146             return $this->peer->append($this->getActualWatermark().$key, $data);
00147         }
00148         
00149         protected function store(
00150             $action, $key, $value, $expires = Cache::EXPIRES_MEDIUM
00151         )
00152         {
00153             return
00154                 $this->peer->$action(
00155                     $this->getActualWatermark().$key, $value, $expires
00156                 );
00157         }
00158     }
00159 ?>