MemoryCache.class.php 4.1 KB

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