PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/class_cache.php

https://github.com/opensciencefund/gazelle
PHP | 295 lines | 227 code | 26 blank | 42 comment | 54 complexity | 91d6c348b1db0f0338c7d50a33eb9094 MD5 | raw file
  1. <?
  2. /*************************************************************************|
  3. |--------------- Caching class -------------------------------------------|
  4. |*************************************************************************|
  5. This class is a wrapper for the Memcache class, and it's been written in
  6. order to better handle the caching of full pages with bits of dynamic
  7. content that are different for every user.
  8. As this inherits memcache, all of the default memcache methods work -
  9. however, this class has page caching functions superior to those of
  10. memcache.
  11. Also, Memcache::get and Memcache::set have been wrapped by
  12. CACHE::get_value and CACHE::cache_value. get_value uses the same argument
  13. as get, but cache_value only takes the key, the value, and the duration
  14. (no zlib).
  15. //unix sockets
  16. memcached -d -m 5120 -s /var/run/memcached.sock -a 0777 -t16 -C -u root
  17. //tcp bind
  18. memcached -d -m 8192 -l 10.10.0.1 -t8 -C
  19. |*************************************************************************/
  20. if (!extension_loaded('memcache')) {
  21. error('Memcache Extension not loaded.');
  22. }
  23. class CACHE extends Memcache {
  24. public $CacheHits = array();
  25. public $MemcacheDBArray = array();
  26. public $MemcacheDBKey = '';
  27. protected $InTransaction = false;
  28. public $Time = 0;
  29. function __construct() {
  30. $this->pconnect(MEMCACHED_HOST, MEMCACHED_PORT);
  31. //$this->connect('localhost', 11211);
  32. }
  33. //---------- Caching functions ----------//
  34. // Allows us to set an expiration on otherwise perminantly cache'd values
  35. // Useful for disabled users, locked threads, basically reducing ram usage
  36. public function expire_value($Key, $Duration=2592000) {
  37. $StartTime=microtime(true);
  38. $this->set($Key, $this->get($Key), $Duration);
  39. $this->Time+=(microtime(true)-$StartTime)*1000;
  40. }
  41. // Wrapper for Memcache::set, with the zlib option removed and default duration of 1 hour
  42. public function cache_value($Key, $Value, $Duration=2592000) {
  43. $StartTime=microtime(true);
  44. if (empty($Key)) {
  45. trigger_error("Cache insert failed for empty key");
  46. }
  47. if (!$this->set($Key, $Value, 0, $Duration)) {
  48. trigger_error("Cache insert failed for key $Key");
  49. }
  50. $this->Time+=(microtime(true)-$StartTime)*1000;
  51. }
  52. public function replace_value($Key, $Value, $Duration=2592000) {
  53. $StartTime=microtime(true);
  54. $this->replace($Key, $Value, false, $Duration);
  55. $this->Time+=(microtime(true)-$StartTime)*1000;
  56. }
  57. public function get_value($Key, $NoCache=false) {
  58. $StartTime=microtime(true);
  59. if (empty($Key)) {
  60. trigger_error("Cache retrieval failed for empty key");
  61. }
  62. if (isset($_GET['clearcache']) && check_perms('admin_clear_cache')) {
  63. if ($_GET['clearcache'] == 1) {
  64. //Because check_perms isn't true until loggeduser is pulled from the cache, we have to remove the entries loaded before the loggeduser data
  65. //Because of this, not user cache data will require a secondary pageload following the clearcache to update
  66. if (count($this->CacheHits) > 0) {
  67. foreach ($this->CacheHits as $Key => $Entry) {
  68. $this->delete($Key);
  69. unset($this->CacheHits[$Key]);
  70. }
  71. }
  72. $this->delete($Key);
  73. $this->Time+=(microtime(true)-$StartTime)*1000;
  74. return false;
  75. } elseif ($_GET['clearcache'] == $Key) {
  76. $this->delete($Key);
  77. $this->Time+=(microtime(true)-$StartTime)*1000;
  78. return false;
  79. } elseif (in_array($_GET['clearcache'], $this->CacheHits)) {
  80. unset($this->CacheHits[$_GET['clearcache']]);
  81. $this->delete($_GET['clearcache']);
  82. }
  83. }
  84. //For cases like the forums, if a keys already loaded grab the existing pointer
  85. if (isset($this->CacheHits[$Key]) && !$NoCache) {
  86. $this->Time+=(microtime(true)-$StartTime)*1000;
  87. return $this->CacheHits[$Key];
  88. }
  89. $Return = $this->get($Key);
  90. if ($Return) {
  91. $this->CacheHits[$Key] = $Return;
  92. }
  93. $this->Time+=(microtime(true)-$StartTime)*1000;
  94. return $Return;
  95. }
  96. // Wrapper for Memcache::delete. For a reason, see above.
  97. public function delete_value($Key) {
  98. $StartTime=microtime(true);
  99. if (empty($Key)) {
  100. trigger_error("Cache retrieval failed for empty key");
  101. }
  102. if (!$this->delete($Key)) {
  103. //trigger_error("Cache delete failed for key $Key");
  104. }
  105. $this->Time+=(microtime(true)-$StartTime)*1000;
  106. }
  107. //---------- memcachedb functions ----------//
  108. public function begin_transaction($Key) {
  109. $Value = $this->get($Key);
  110. if (!is_array($Value)) {
  111. $this->InTransaction = false;
  112. $this->MemcacheDBKey = array();
  113. $this->MemcacheDBKey = '';
  114. return false;
  115. }
  116. $this->MemcacheDBArray = $Value;
  117. $this->MemcacheDBKey = $Key;
  118. $this->InTransaction = true;
  119. return true;
  120. }
  121. public function cancel_transaction() {
  122. $this->InTransaction = false;
  123. $this->MemcacheDBKey = array();
  124. $this->MemcacheDBKey = '';
  125. }
  126. public function commit_transaction($Time=2592000) {
  127. if (!$this->InTransaction) {
  128. return false;
  129. }
  130. $this->cache_value($this->MemcacheDBKey, $this->MemcacheDBArray, $Time);
  131. $this->InTransaction = false;
  132. }
  133. // Updates multiple rows in an array
  134. public function update_transaction($Rows, $Values) {
  135. if (!$this->InTransaction) {
  136. return false;
  137. }
  138. $Array = $this->MemcacheDBArray;
  139. if (is_array($Rows)) {
  140. $i = 0;
  141. $Keys = $Rows[0];
  142. $Property = $Rows[1];
  143. foreach ($Keys as $Row) {
  144. $Array[$Row][$Property] = $Values[$i];
  145. $i++;
  146. }
  147. } else {
  148. $Array[$Rows] = $Values;
  149. }
  150. $this->MemcacheDBArray = $Array;
  151. }
  152. // Updates multiple values in a single row in an array
  153. // $Values must be an associative array with key:value pairs like in the array we're updating
  154. public function update_row($Row, $Values) {
  155. if (!$this->InTransaction) {
  156. return false;
  157. }
  158. if ($Row === false) {
  159. $UpdateArray = $this->MemcacheDBArray;
  160. } else {
  161. $UpdateArray = $this->MemcacheDBArray[$Row];
  162. }
  163. foreach ($Values as $Key => $Value) {
  164. if (!array_key_exists($Key, $UpdateArray)) {
  165. trigger_error('Bad transaction key ('.$Key.') for cache '.$this->MemcacheDBKey);
  166. }
  167. if ($Value === '+1') {
  168. if (!is_number($UpdateArray[$Key])) {
  169. trigger_error('Tried to increment non-number ('.$Key.') for cache '.$this->MemcacheDBKey);
  170. }
  171. ++$UpdateArray[$Key]; // Increment value
  172. } elseif ($Value === '-1') {
  173. if (!is_number($UpdateArray[$Key])) {
  174. trigger_error('Tried to decrement non-number ('.$Key.') for cache '.$this->MemcacheDBKey);
  175. }
  176. --$UpdateArray[$Key]; // Decrement value
  177. } else {
  178. $UpdateArray[$Key] = $Value; // Otherwise, just alter value
  179. }
  180. }
  181. if ($Row === false) {
  182. $this->MemcacheDBArray = $UpdateArray;
  183. } else {
  184. $this->MemcacheDBArray[$Row] = $UpdateArray;
  185. }
  186. }
  187. // Increments multiple values in a single row in an array
  188. // $Values must be an associative array with key:value pairs like in the array we're updating
  189. public function increment_row($Row, $Values) {
  190. if (!$this->InTransaction) {
  191. return false;
  192. }
  193. if ($Row === false) {
  194. $UpdateArray = $this->MemcacheDBArray;
  195. } else {
  196. $UpdateArray = $this->MemcacheDBArray[$Row];
  197. }
  198. foreach ($Values as $Key => $Value) {
  199. if (!array_key_exists($Key, $UpdateArray)) {
  200. trigger_error('Bad transaction key ('.$Key.') for cache '.$this->MemcacheDBKey);
  201. }
  202. if (!is_number($Value)) {
  203. trigger_error('Tried to increment with non-number ('.$Key.') for cache '.$this->MemcacheDBKey);
  204. }
  205. $UpdateArray[$Key] += $Value; // Increment value
  206. }
  207. if ($Row === false) {
  208. $this->MemcacheDBArray = $UpdateArray;
  209. } else {
  210. $this->MemcacheDBArray[$Row] = $UpdateArray;
  211. }
  212. }
  213. // Insert a value at the beginning of the array
  214. public function insert_front($Key, $Value) {
  215. if (!$this->InTransaction) {
  216. return false;
  217. }
  218. if ($Key === '') {
  219. array_unshift($this->MemcacheDBArray, $Value);
  220. } else {
  221. $this->MemcacheDBArray = array($Key=>$Value) + $this->MemcacheDBArray;
  222. }
  223. }
  224. // Insert a value at the end of the array
  225. public function insert_back($Key, $Value) {
  226. if (!$this->InTransaction) {
  227. return false;
  228. }
  229. if ($Key === '') {
  230. array_push($this->MemcacheDBArray, $Value);
  231. } else {
  232. $this->MemcacheDBArray = $this->MemcacheDBArray + array($Key=>$Value);
  233. }
  234. }
  235. public function insert($Key, $Value) {
  236. if (!$this->InTransaction) {
  237. return false;
  238. }
  239. if ($Key === '') {
  240. $this->MemcacheDBArray[] = $Value;
  241. } else {
  242. $this->MemcacheDBArray[$Key] = $Value;
  243. }
  244. }
  245. public function delete_row($Row) {
  246. if (!$this->InTransaction) {
  247. return false;
  248. }
  249. if (!isset($this->MemcacheDBArray[$Row])) {
  250. trigger_error('Tried to delete non-existent row ('.$Row.') for cache '.$this->MemcacheDBKey);
  251. }
  252. unset($this->MemcacheDBArray[$Row]);
  253. }
  254. public function update($Key, $Rows, $Values, $Time=2592000) {
  255. if (!$this->InTransaction) {
  256. $this->begin_transaction($Key);
  257. $this->update_transaction($Rows, $Values);
  258. $this->commit_transaction($Time);
  259. } else {
  260. $this->update_transaction($Rows, $Values);
  261. }
  262. }
  263. }