00001 <?php 00002 /*************************************************************************** 00003 * Copyright (C) 2006-2007 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 /* $Id: WatermarkedPeer.class.php 4687 2007-12-09 18:57:18Z voxus $ */ 00012 00018 final class WatermarkedPeer extends SelectivePeer 00019 { 00020 private $peer = null; 00021 private $watermark = null; 00022 00024 private $map = null; 00025 00029 public static function create( 00030 CachePeer $peer, 00031 $watermark = "Single onPHP's project" 00032 ) 00033 { 00034 return new self($peer, $watermark); 00035 } 00036 00037 public function __construct( 00038 CachePeer $peer, 00039 $watermark = "Single onPHP's project" 00040 ) 00041 { 00042 $this->peer = $peer; 00043 $this->setWatermark($watermark); 00044 } 00045 00046 public function setWatermark($watermark) 00047 { 00048 $this->watermark = md5($watermark.'::'); 00049 00050 return $this; 00051 } 00052 00053 public function getWatermark() 00054 { 00055 return $this->watermark; 00056 } 00057 00058 public function getActualWatermark() 00059 { 00060 if ( 00061 $this->className 00062 && isset($this->map[$this->className]) 00063 ) 00064 return $this->map[$this->className]; 00065 00066 return $this->watermark; 00067 } 00068 00074 public function setClassMap($map) 00075 { 00076 $this->map = array(); 00077 00078 foreach ($map as $className => $watermark) 00079 $this->map[$className] = md5($watermark.'::'); 00080 00081 return $this; 00082 } 00083 00087 public function mark($className) 00088 { 00089 $this->className = $className; 00090 00091 $this->peer->mark($this->getActualWatermark().$className); 00092 00093 return $this; 00094 } 00095 00096 public function get($key) 00097 { 00098 return $this->peer->get($this->getActualWatermark().$key); 00099 } 00100 00101 public function delete($key) 00102 { 00103 return $this->peer->delete($this->getActualWatermark().$key); 00104 } 00105 00109 public function clean() 00110 { 00111 $this->peer->clean(); 00112 00113 return parent::clean(); 00114 } 00115 00116 public function isAlive() 00117 { 00118 return $this->peer->isAlive(); 00119 } 00120 00121 protected function store( 00122 $action, $key, &$value, $expires = Cache::EXPIRES_MEDIUM 00123 ) 00124 { 00125 return 00126 $this->peer->$action( 00127 $this->getActualWatermark().$key, $value, $expires 00128 ); 00129 } 00130 } 00131 ?>