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

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