1 <?php 2 /* Copyright (c) 2012, Geert Bergman (geert@scrivo.nl) 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of "Scrivo" nor the names of its contributors may be 14 * used to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $Id: LocalCache.php 631 2013-05-21 15:14:11Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\LocalCache class. 34 */ 35 36 namespace Scrivo; 37 38 /** 39 * The LocalCache is the cache that Scrivo uses to store already instantiated 40 * objects. The local cache map through to a persisten cache (if defined). So 41 * when an object is not found the local cache the persistent cache will be 42 * checked next. 43 * 44 * So when you instantiate a Scrivo Page for instance first will be tried to 45 * load it from the local cache, if not it is tried to retrieve it from the 46 * persistent cache (apc or filecache). 47 */ 48 class LocalCache implements \ArrayAccess { 49 50 /** 51 * The cache array. 52 * @var object[] 53 */ 54 private static $cache; 55 56 /** 57 * The persitent cache to use. 58 * @var \Scrivo\Cache 59 */ 60 private static $persistentCache; 61 62 /** 63 * Construct a local cache. 64 * 65 * @param \Scrivo\Cache $persistentCache The persistent cache to use behind 66 * the local cache. 67 */ 68 function __construct(\Scrivo\Cache $persistentCache=null) { 69 self::$cache = array(); 70 if ($persistentCache) { 71 self::$persistentCache = $persistentCache; 72 } else { 73 self::$persistentCache = new \Scrivo\Cache\NoCache(); 74 } 75 } 76 77 /** 78 * Get an object from the cache. 79 * 80 * Note this is part of the implementation of ArrayAccess and should be 81 * invoked as array access: 82 * 83 * $obj = $myLocalCache["key"]; 84 * 85 * @param int|string $key The key used to store the value. 86 * 87 * @return object The requested object or null if not found. 88 */ 89 public function offsetGet($key) { 90 if (isset(self::$cache[$key])) { 91 //error_log("Local cache hit $key"); 92 return self::$cache[$key]; 93 } 94 $res = self::$persistentCache->fetch(new \Scrivo\String($key)); 95 if ($res) { 96 //error_log("Persistent cache hit $key"); 97 self::$cache[$key] = $res; 98 } 99 return $res; 100 } 101 102 /** 103 * Set an object in the cache. 104 * 105 * Note this is part of the implementation of ArrayAccess and should be 106 * invoked as array access: 107 * 108 * $myLocalCache["key"] = $obj; 109 * 110 * @param int|string $key The key used to store the value. 111 * @param mixed $value The object to set in the cache. 112 */ 113 public function offsetSet($key, $value) { 114 //error_log("cache set $key"); 115 self::$cache[$key] = $value; 116 self::$persistentCache->overwrite(new \Scrivo\String($key), $value); 117 } 118 119 /** 120 * Test if an object exists in the cache. 121 * 122 * Note this is part of the implementation of ArrayAccess and should be 123 * invoked as array access: 124 * 125 * isset($myLocalCache["key"]); 126 * 127 * @param int|string $key The key used to store the value. 128 * 129 * @return boolean True if an object exists in the cache at the 130 * entry specified by the key. 131 */ 132 public function offsetExists($key) { 133 if (isset(self::$cache[$key])) { 134 //error_log("Local cache hit $key"); 135 return true; 136 } 137 $t = self::$persistentCache->fetch(new \Scrivo\String($key)); 138 if ($t) { 139 //error_log("Persistent cache hit $key"); 140 self::$cache[$key] = $t; 141 } 142 return $t?true:false; 143 } 144 145 /** 146 * Delete an object from the cache. 147 * 148 * Note this is part of the implementation of ArrayAccess and should be 149 * invoked as array access: 150 * 151 * usset($myLocalCache["key"]); 152 * 153 * @param int|string $key The key used to store the value. 154 */ 155 public function offsetUnset($key) { 156 unset(self::$cache[$key]); 157 self::$persistentCache->delete(new \Scrivo\String($key)); 158 } 159 160 } 161 162 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013