1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | <?php /** * Cache to manage objects in process memory * * @author Christopher Han <xiphux@gmail.com> * @copyright Copyright (c) 2012 Christopher Han * @package GitPHP * @subpackage Cache */ class GitPHP_MemoryCache { /** * Stores the objects in this cache * * @var array */ protected $objects = array(); /** * Whether the cache will automatically manage the number of items * * @var boolean */ protected $autoManaged = true; /** * Stores the last project that stored into this cache * * @var string */ protected $lastProject; /** * Size of cache * * @var int */ protected $size; /** * Class constructor * * @param int $size size of cache */ public function __construct($size = 0) { $this->size = $size; } /** * Gets the size of this cache * * @return int size */ public function GetSize() { return $this->size; } /** * Sets the size of this cache * * @param int $size size */ public function SetSize($size) { if ($this->size != $size) { $oldSize = $this->size; $this->size = $size; if (($size > 0) && ($size < $oldSize)) { $this->Evict(); } } } /** * Gets whether this cache is auto managing its size * * @return bool true if automanaged */ public function GetAutoManaged() { return $this->autoManaged; } /** * Sets whether this cache should auto manage its size * * @param bool $autoManaged true if cache should automanage */ public function SetAutoManaged($autoManaged) { if (!$this->autoManaged && $autoManaged && (count($this->objects) > 0)) { end($this->objects); $lastKey = key($this->objects); if ($lastKey) { $this->lastProject = $this->ExtractProject($lastKey); } } $this->autoManaged = $autoManaged; } /** * Gets an object from the cache * * @param string $key cache key * @return mixed object from cache if found */ public function Get($key) { if (empty($key)) return null; if (!isset($this->objects[$key])) return null; $object = $this->objects[$key]; /* * unset and reset to move to end of associative array * (indicate as most recently used) */ unset($this->objects[$key]); $this->objects[$key] = $object; return $object; } /** * Sets an object into the cache * * @param string $key cache key * @param mixed $object object to cache */ public function Set($key, $object) { if (empty($key)) return; if ($this->autoManaged) { $project = $this->ExtractProject($key); if (empty($this->lastProject) || ($this->lastProject != $project)) { if (count($this->objects) > 0) { $this->Clear(); } $this->lastProject = $project; } } if (isset($this->objects[$key])) { /* * unset so resetting will move to end of associative array * (indicate as most recently used) */ unset($this->objects[$key]); } else { $this->Evict(); } $this->objects[$key] = $object; } /** * Gets the count of items in this cache * * @return int count */ public function GetCount() { return count($this->objects); } /** * Clear the cache */ public function Clear() { $this->objects = array(); $this->lastProject = ''; } /** * Evicts items from the cache down to the size limit */ private function Evict() { if ($this->size < 1) { return; } while (count($this->objects) >= $this->size) { array_shift($this->objects); } } /** * Extracts the project from a key * * @param string $key cache key */ private function ExtractProject($key) { if (empty($key)) return ''; if (strncmp($key, 'project|', 8) != 0) { return ''; } strtok($key, '|'); $project = strtok('|'); return $project; } } |