Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cache.class.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. die('Memcache Extension not loaded.');
  22. }
  23. class CACHE extends Memcache {
  24. /**
  25. * Torrent Group cache version
  26. */
  27. const GROUP_VERSION = 5;
  28. public $CacheHits = [];
  29. public $MemcacheDBArray = [];
  30. public $MemcacheDBKey = '';
  31. protected $InTransaction = false;
  32. public $Time = 0;
  33. private $Servers = [];
  34. private $PersistentKeys = array(
  35. 'ajax_requests_*',
  36. 'query_lock_*',
  37. 'stats_*',
  38. 'top10tor_*',
  39. 'top10votes_*',
  40. 'users_snatched_*',
  41. // Cache-based features
  42. 'global_notification',
  43. 'notifications_one_reads_*',
  44. );
  45. private $ClearedKeys = [];
  46. public $CanClear = false;
  47. public $InternalCache = true;
  48. function __construct($Servers) {
  49. $this->Servers = $Servers;
  50. foreach ($Servers as $Server) {
  51. $this->addServer($Server['host'], $Server['port'], true, $Server['buckets']);
  52. }
  53. }
  54. //---------- Caching functions ----------//
  55. // Allows us to set an expiration on otherwise perminantly cache'd values
  56. // Useful for disabled users, locked threads, basically reducing ram usage
  57. public function expire_value($Key, $Duration = 2592000) {
  58. $StartTime = microtime(true);
  59. $this->set($Key, $this->get($Key), $Duration);
  60. $this->Time += (microtime(true) - $StartTime) * 1000;
  61. }
  62. // Wrapper for Memcache::set, with the zlib option removed and default duration of 30 days
  63. public function cache_value($Key, $Value, $Duration = 2592000) {
  64. $StartTime = microtime(true);
  65. if (empty($Key)) {
  66. trigger_error("Cache insert failed for empty key");
  67. }
  68. if (!$this->set($Key, $Value, 0, $Duration)) {
  69. trigger_error("Cache insert failed for key $Key");
  70. }
  71. if ($this->InternalCache && array_key_exists($Key, $this->CacheHits)) {
  72. $this->CacheHits[$Key] = $Value;
  73. }
  74. $this->Time += (microtime(true) - $StartTime) * 1000;
  75. }
  76. // Wrapper for Memcache::add, with the zlib option removed and default duration of 30 days
  77. public function add_value($Key, $Value, $Duration = 2592000) {
  78. $StartTime = microtime(true);
  79. $Added = $this->add($Key, $Value, 0, $Duration);
  80. $this->Time += (microtime(true) - $StartTime) * 1000;
  81. return $Added;
  82. }
  83. public function replace_value($Key, $Value, $Duration = 2592000) {
  84. $StartTime = microtime(true);
  85. $this->replace($Key, $Value, false, $Duration);
  86. if ($this->InternalCache && array_key_exists($Key, $this->CacheHits)) {
  87. $this->CacheHits[$Key] = $Value;
  88. }
  89. $this->Time += (microtime(true) - $StartTime) * 1000;
  90. }
  91. public function get_value($Key, $NoCache = false) {
  92. if (!$this->InternalCache) {
  93. $NoCache = true;
  94. }
  95. $StartTime = microtime(true);
  96. if (empty($Key)) {
  97. trigger_error('Cache retrieval failed for empty key');
  98. }
  99. if (!empty($_GET['clearcache']) && $this->CanClear && !isset($this->ClearedKeys[$Key]) && !Misc::in_array_partial($Key, $this->PersistentKeys)) {
  100. if ($_GET['clearcache'] === '1') {
  101. // Because check_perms() isn't true until LoggedUser is pulled from the cache, we have to remove the entries loaded before the LoggedUser data
  102. // Because of this, not user cache data will require a secondary pageload following the clearcache to update
  103. if (count($this->CacheHits) > 0) {
  104. foreach (array_keys($this->CacheHits) as $HitKey) {
  105. if (!isset($this->ClearedKeys[$HitKey]) && !Misc::in_array_partial($HitKey, $this->PersistentKeys)) {
  106. $this->delete($HitKey);
  107. unset($this->CacheHits[$HitKey]);
  108. $this->ClearedKeys[$HitKey] = true;
  109. }
  110. }
  111. }
  112. $this->delete($Key);
  113. $this->Time += (microtime(true) - $StartTime) * 1000;
  114. return false;
  115. } elseif ($_GET['clearcache'] == $Key) {
  116. $this->delete($Key);
  117. $this->Time += (microtime(true) - $StartTime) * 1000;
  118. return false;
  119. } elseif (substr($_GET['clearcache'], -1) === '*') {
  120. $Prefix = substr($_GET['clearcache'], 0, -1);
  121. if ($Prefix === '' || $Prefix === substr($Key, 0, strlen($Prefix))) {
  122. $this->delete($Key);
  123. $this->Time += (microtime(true) - $StartTime) * 1000;
  124. return false;
  125. }
  126. }
  127. $this->ClearedKeys[$Key] = true;
  128. }
  129. // For cases like the forums, if a key is already loaded, grab the existing pointer
  130. if (isset($this->CacheHits[$Key]) && !$NoCache) {
  131. $this->Time += (microtime(true) - $StartTime) * 1000;
  132. return $this->CacheHits[$Key];
  133. }
  134. $Return = $this->get($Key);
  135. if ($Return !== false) {
  136. $this->CacheHits[$Key] = $NoCache ? null : $Return;
  137. }
  138. $this->Time += (microtime(true) - $StartTime) * 1000;
  139. return $Return;
  140. }
  141. // Wrapper for Memcache::delete. For a reason, see above.
  142. public function delete_value($Key) {
  143. $StartTime = microtime(true);
  144. if (empty($Key)) {
  145. trigger_error('Cache deletion failed for empty key');
  146. }
  147. if (!$this->delete($Key)) {
  148. //trigger_error("Cache delete failed for key $Key");
  149. }
  150. unset($this->CacheHits[$Key]);
  151. $this->Time += (microtime(true) - $StartTime) * 1000;
  152. }
  153. public function increment_value($Key, $Value = 1) {
  154. $StartTime = microtime(true);
  155. $NewVal = $this->increment($Key, $Value);
  156. if (isset($this->CacheHits[$Key])) {
  157. $this->CacheHits[$Key] = $NewVal;
  158. }
  159. $this->Time += (microtime(true) - $StartTime) * 1000;
  160. }
  161. public function decrement_value($Key, $Value = 1) {
  162. $StartTime = microtime(true);
  163. $NewVal = $this->decrement($Key, $Value);
  164. if (isset($this->CacheHits[$Key])) {
  165. $this->CacheHits[$Key] = $NewVal;
  166. }
  167. $this->Time += (microtime(true) - $StartTime) * 1000;
  168. }
  169. //---------- memcachedb functions ----------//
  170. public function begin_transaction($Key) {
  171. $Value = $this->get($Key);
  172. if (!is_array($Value)) {
  173. $this->InTransaction = false;
  174. $this->MemcacheDBKey = [];
  175. $this->MemcacheDBKey = '';
  176. return false;
  177. }
  178. $this->MemcacheDBArray = $Value;
  179. $this->MemcacheDBKey = $Key;
  180. $this->InTransaction = true;
  181. return true;
  182. }
  183. public function cancel_transaction() {
  184. $this->InTransaction = false;
  185. $this->MemcacheDBKey = [];
  186. $this->MemcacheDBKey = '';
  187. }
  188. public function commit_transaction($Time = 2592000) {
  189. if (!$this->InTransaction) {
  190. return false;
  191. }
  192. $this->cache_value($this->MemcacheDBKey, $this->MemcacheDBArray, $Time);
  193. $this->InTransaction = false;
  194. }
  195. // Updates multiple rows in an array
  196. public function update_transaction($Rows, $Values) {
  197. if (!$this->InTransaction) {
  198. return false;
  199. }
  200. $Array = $this->MemcacheDBArray;
  201. if (is_array($Rows)) {
  202. $i = 0;
  203. $Keys = $Rows[0];
  204. $Property = $Rows[1];
  205. foreach ($Keys as $Row) {
  206. $Array[$Row][$Property] = $Values[$i];
  207. $i++;
  208. }
  209. } else {
  210. $Array[$Rows] = $Values;
  211. }
  212. $this->MemcacheDBArray = $Array;
  213. }
  214. // Updates multiple values in a single row in an array
  215. // $Values must be an associative array with key:value pairs like in the array we're updating
  216. public function update_row($Row, $Values) {
  217. if (!$this->InTransaction) {
  218. return false;
  219. }
  220. if ($Row === false) {
  221. $UpdateArray = $this->MemcacheDBArray;
  222. } else {
  223. $UpdateArray = $this->MemcacheDBArray[$Row];
  224. }
  225. foreach ($Values as $Key => $Value) {
  226. if (!array_key_exists($Key, $UpdateArray)) {
  227. trigger_error('Bad transaction key ('.$Key.') for cache '.$this->MemcacheDBKey);
  228. }
  229. if ($Value === '+1') {
  230. if (!is_number($UpdateArray[$Key])) {
  231. trigger_error('Tried to increment non-number ('.$Key.') for cache '.$this->MemcacheDBKey);
  232. }
  233. ++$UpdateArray[$Key]; // Increment value
  234. } elseif ($Value === '-1') {
  235. if (!is_number($UpdateArray[$Key])) {
  236. trigger_error('Tried to decrement non-number ('.$Key.') for cache '.$this->MemcacheDBKey);
  237. }
  238. --$UpdateArray[$Key]; // Decrement value
  239. } else {
  240. $UpdateArray[$Key] = $Value; // Otherwise, just alter value
  241. }
  242. }
  243. if ($Row === false) {
  244. $this->MemcacheDBArray = $UpdateArray;
  245. } else {
  246. $this->MemcacheDBArray[$Row] = $UpdateArray;
  247. }
  248. }
  249. // Increments multiple values in a single row in an array
  250. // $Values must be an associative array with key:value pairs like in the array we're updating
  251. public function increment_row($Row, $Values) {
  252. if (!$this->InTransaction) {
  253. return false;
  254. }
  255. if ($Row === false) {
  256. $UpdateArray = $this->MemcacheDBArray;
  257. } else {
  258. $UpdateArray = $this->MemcacheDBArray[$Row];
  259. }
  260. foreach ($Values as $Key => $Value) {
  261. if (!array_key_exists($Key, $UpdateArray)) {
  262. trigger_error("Bad transaction key ($Key) for cache ".$this->MemcacheDBKey);
  263. }
  264. if (!is_number($Value)) {
  265. trigger_error("Tried to increment with non-number ($Key) for cache ".$this->MemcacheDBKey);
  266. }
  267. $UpdateArray[$Key] += $Value; // Increment value
  268. }
  269. if ($Row === false) {
  270. $this->MemcacheDBArray = $UpdateArray;
  271. } else {
  272. $this->MemcacheDBArray[$Row] = $UpdateArray;
  273. }
  274. }
  275. // Insert a value at the beginning of the array
  276. public function insert_front($Key, $Value) {
  277. if (!$this->InTransaction) {
  278. return false;
  279. }
  280. if ($Key === '') {
  281. array_unshift($this->MemcacheDBArray, $Value);
  282. } else {
  283. $this->MemcacheDBArray = array($Key=>$Value) + $this->MemcacheDBArray;
  284. }
  285. }
  286. // Insert a value at the end of the array
  287. public function insert_back($Key, $Value) {
  288. if (!$this->InTransaction) {
  289. return false;
  290. }
  291. if ($Key === '') {
  292. array_push($this->MemcacheDBArray, $Value);
  293. } else {
  294. $this->MemcacheDBArray = $this->MemcacheDBArray + array($Key=>$Value);
  295. }
  296. }
  297. public function insert($Key, $Value) {
  298. if (!$this->InTransaction) {
  299. return false;
  300. }
  301. if ($Key === '') {
  302. $this->MemcacheDBArray[] = $Value;
  303. } else {
  304. $this->MemcacheDBArray[$Key] = $Value;
  305. }
  306. }
  307. public function delete_row($Row) {
  308. if (!$this->InTransaction) {
  309. return false;
  310. }
  311. if (!isset($this->MemcacheDBArray[$Row])) {
  312. trigger_error("Tried to delete non-existent row ($Row) for cache ".$this->MemcacheDBKey);
  313. }
  314. unset($this->MemcacheDBArray[$Row]);
  315. }
  316. public function update($Key, $Rows, $Values, $Time = 2592000) {
  317. if (!$this->InTransaction) {
  318. $this->begin_transaction($Key);
  319. $this->update_transaction($Rows, $Values);
  320. $this->commit_transaction($Time);
  321. } else {
  322. $this->update_transaction($Rows, $Values);
  323. }
  324. }
  325. /**
  326. * Tries to set a lock. Expiry time is one hour to avoid indefinite locks
  327. *
  328. * @param string $LockName name on the lock
  329. * @return true if lock was acquired
  330. */
  331. public function get_query_lock($LockName) {
  332. return $this->add_value('query_lock_'.$LockName, 1, 3600);
  333. }
  334. /**
  335. * Remove lock
  336. *
  337. * @param string $LockName name on the lock
  338. */
  339. public function clear_query_lock($LockName) {
  340. $this->delete_value('query_lock_'.$LockName);
  341. }
  342. /**
  343. * Get cache server status
  344. *
  345. * @return array (host => bool status, ...)
  346. */
  347. public function server_status() {
  348. $Status = [];
  349. foreach ($this->Servers as $Server) {
  350. $Status["$Server[host]:$Server[port]"] = $this->getServerStatus($Server['host'], $Server['port']);
  351. }
  352. return $Status;
  353. }
  354. }