Guest User

Untitled

a guest
Jun 10th, 2010
303
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // $Id: apc.php,v 1.1.2.19 2010/02/04 16:11:42 andypost Exp $
  3.  
  4. /**
  5. * @file
  6. * APC engine class.
  7. */
  8.  
  9. class apcCacheRouterEngine extends CacheRouterEngine {
  10. /**
  11. * page_fast_cache
  12. * This tells CacheRouter to use page_fast_cache.
  13. *
  14. * @return bool TRUE
  15. */
  16. function page_fast_cache() {
  17. return $this->fast_cache;
  18. }
  19.  
  20. /**
  21. * get()
  22. * Return item from cache if it is available.
  23. *
  24. * @param string $key
  25. * The key to fetch.
  26. * @return mixed object|bool
  27. * Returns either the cache object or FALSE on failure
  28. */
  29. function get($key) {
  30. $cache = parent::get($this->key($key));
  31. if (isset($cache)) {
  32. return $cache;
  33. }
  34.  
  35. $cache = apc_fetch($this->key($key));
  36. if (is_object($cache) && $cache->serialized) {
  37. $cache->data = unserialize($cache->data);
  38. }
  39. parent::set($this->key($key), $cache);
  40. return $cache;
  41. }
  42.  
  43. /**
  44. * set()
  45. * Add item into cache.
  46. *
  47. * @param string $key
  48. * The key to set.
  49. * @param string $value
  50. * The data to store in the cache.
  51. * @param string $expire
  52. * The time to expire in seconds.
  53. * @param string $headers
  54. * The page headers.
  55. * @return bool
  56. * Returns TRUE on success or FALSE on failure
  57. */
  58. function set($key, $value, $expire = CACHE_PERMANENT, $headers = NULL) {
  59. // Create new cache object.
  60. $cache = new stdClass;
  61. $cache->cid = $key;
  62. $cache->created = time();
  63. $cache->expire = $expire;
  64. $cache->headers = $headers;
  65.  
  66. if ($expire != CACHE_PERMANENT && $expire != CACHE_TEMPORARY) {
  67. // Convert Drupal $expire, which is a timestamp, to a TTL for APC
  68. $ttl = $expire - time();
  69. }
  70. else {
  71. $ttl = 0;
  72. }
  73.  
  74. if (!is_string($value)) {
  75. $cache->serialized = TRUE;
  76. $cache->data = serialize($value);
  77. }
  78. else {
  79. $cache->serialized = FALSE;
  80. $cache->data = $value;
  81. }
  82.  
  83. $return = FALSE;
  84. if (!empty($key) && $this->lock()) {
  85. // Get lookup table to be able to keep track of bins
  86. $lookup = $this->getLookup();
  87.  
  88. // If the lookup table is empty, initialize table
  89. if (!is_array($lookup)) {
  90. $lookup = array();
  91. }
  92.  
  93. $lookup[$this->key($key)] = $expire;
  94.  
  95. // Attempt to store full key and value
  96. if (!apc_store($this->key($key), $cache, $ttl)) {
  97. unset($lookup[$this->key($key)]);
  98. $return = FALSE;
  99. }
  100. else {
  101. // Update static cache
  102. parent::set($this->key($key), $cache);
  103. $return = TRUE;
  104. }
  105.  
  106. // Resave the lookup table (even on failure)
  107. $this->setLookup($lookup);
  108.  
  109. // Remove lock.
  110. $this->unlock();
  111. }
  112.  
  113. return $return;
  114. }
  115.  
  116. /**
  117. * delete()
  118. * Remove item from cache.
  119. *
  120. * @param string $key
  121. * The key to set.
  122. * @return mixed object|bool
  123. * Returns either the cache object or FALSE on failure
  124. */
  125. function delete($key) {
  126. // Remove from static array cache.
  127. parent::flush();
  128.  
  129. if (substr($key, strlen($key) - 1, 1) == '*') {
  130. $key = $this->key(substr($key, 0, strlen($key) - 1));
  131. $lookup = $this->getLookup();
  132. if (is_array($lookup) && !empty($lookup)) {
  133. foreach ($lookup as $k => $v) {
  134. if (substr($k, 0, strlen($key)) == $key) {
  135. apc_delete($k);
  136. unset($lookup[$k]);
  137. }
  138. }
  139. }
  140. else {
  141. $lookup = array();
  142. }
  143. if ($this->lock()) {
  144. $this->setLookup($lookup);
  145. $this->unlock();
  146. }
  147. }
  148. else {
  149. if (!empty($key)) {
  150. if (!apc_delete($this->key($key))) {
  151. return FALSE;
  152. }
  153. }
  154. }
  155. return TRUE;
  156. }
  157.  
  158. /**
  159. * flush()
  160. * Flush the entire cache.
  161. *
  162. * @param none
  163. * @return mixed bool
  164. * Returns TRUE
  165. */
  166. function flush() {
  167. parent::flush();
  168. if ($this->lock()) {
  169. // Get lookup table to be able to keep track of bins
  170. $lookup = $this->getLookup();
  171.  
  172. // If the lookup table is empty, remove lock and return
  173. if (!is_array($lookup) || empty($lookup)) {
  174. $this->unlock();
  175. return TRUE;
  176. }
  177.  
  178. // Cycle through keys and remove each entry from the cache
  179. foreach ($lookup as $k => $expire) {
  180. if ($expire != CACHE_PERMANENT && $expire <= time()) {
  181. apc_delete($k);
  182. unset($lookup[$k]);
  183. }
  184. }
  185.  
  186. // Resave the lookup table (even on failure)
  187. $this->setLookup($lookup);
  188.  
  189. // Remove lock
  190. $this->unlock();
  191. }
  192.  
  193. return TRUE;
  194. }
  195.  
  196. function getLookup() {
  197. return apc_fetch($this->lookup);
  198. }
  199.  
  200. function setLookup($lookup = array()) {
  201. apc_store($this->lookup, $lookup, 0);
  202. }
  203.  
  204. function stats() {
  205. $apc_stats = apc_cache_info('user', TRUE);
  206. $apc_stats['uptime'] = time() - $apc_stats['start_time'];
  207. $stats = array(
  208. 'uptime' => $apc_stats['uptime'],
  209. 'bytes_used' => $apc_stats['mem_size'],
  210. 'bytes_total' => ini_get('apc.shm_size') * 1024 * 1024,
  211. 'gets' => $apc_stats['num_hits'],
  212. 'sets' => $apc_stats['num_inserts'],
  213. 'hits' => $apc_stats['num_hits'] + $apc_stats['num_misses'],
  214. 'misses' => $apc_stats['num_misses'],
  215. 'req_rate' => (($apc_stats['num_hits'] + $apc_stats['num_misses']) / $apc_stats['uptime']),
  216. 'hit_rate' => ($apc_stats['num_hits'] / $apc_stats['uptime']),
  217. 'miss_rate' => ($apc_stats['num_misses'] / $apc_stats['uptime']),
  218. 'set_rate' => ($apc_stats['num_inserts'] / $apc_stats['uptime']),
  219. );
  220. return $stats;
  221. }
  222. }
RAW Paste Data