PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/system/classes/apccache.php

https://github.com/HabariMag/habarimag-old
PHP | 235 lines | 148 code | 29 blank | 58 comment | 22 complexity | 68907113fca15b5f116ef902457124d0 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * @package Habari
  4. *
  5. */
  6. /**
  7. *
  8. * Contains the APCCache class
  9. */
  10. class APCCache extends Cache
  11. {
  12. protected $enabled = false;
  13. protected $cache_data = array();
  14. protected $prefix;
  15. /**
  16. * Constructor for APCCache
  17. */
  18. public function __construct()
  19. {
  20. $this->prefix = Options::get( 'GUID' );
  21. $this->enabled = extension_loaded( 'apc' );
  22. if ( !$this->enabled ) {
  23. Session::error( _t( "The APC Cache PHP module is not loaded - the cache is disabled.", "apccache" ), 'filecache' );
  24. EventLog::log( _t( "The APC Cache PHP module is not loaded - the cache is disabled.", "apccache" ), 'notice', 'cache', 'apccache' );
  25. }
  26. }
  27. /**
  28. * Is record with $name in the cache?
  29. *
  30. * @param string $name name of the cached item
  31. * @return boolean true if item is cached, false if not
  32. */
  33. protected function _has( $name, $group )
  34. {
  35. if ( !$this->enabled ) {
  36. return false;
  37. }
  38. return apc_fetch( implode( ':', array( $this->prefix, $group, $name ) ) ) !== false;
  39. }
  40. /**
  41. * Returns the named value from the cache.
  42. *
  43. * @param string $name The name of the cached item
  44. * @return mixed The item value or null if it doesn't exist in cache
  45. */
  46. protected function _get( $name, $group )
  47. {
  48. if ( !$this->enabled ) {
  49. return null;
  50. }
  51. return apc_fetch( implode( ':', array( $this->prefix, $group, $name ) ) );
  52. }
  53. /**
  54. * Returns the named values from a group of cache.
  55. *
  56. * @param string $name The name of the cached item
  57. * @return array The cache records of the group
  58. */
  59. protected function _get_group( $group )
  60. {
  61. if ( !$this->enabled ) {
  62. return null;
  63. }
  64. $cache_info = apc_cache_info( 'user' );
  65. $group_cache = array();
  66. foreach ( $cache_info['cache_list'] as $cache_item ) {
  67. if ( strpos( $cache_item['info'], $this->prefix . ":$group:" ) === 0 ) {
  68. $name = substr( $cache_item['info'], strlen( $this->prefix . ":$group:" ) );
  69. $group_cache[$name] = apc_fetch( $cache_item['info'] );
  70. }
  71. }
  72. return $group_cache;
  73. }
  74. /**
  75. * Is group named $group in the cache?
  76. *
  77. * @param string $name name of the cached item
  78. * @return boolean true if group is cached, false if not
  79. */
  80. protected function _has_group( $group )
  81. {
  82. if ( !$this->enabled ) {
  83. return null;
  84. }
  85. $cache_info = apc_cache_info( 'user' );
  86. $group_cache = array();
  87. foreach ( $cache_info['cache_list'] as $cache_item ) {
  88. if ( strpos( $cache_item['info'], $this->prefix . ":$group:" ) === 0 ) {
  89. $group_cache[$cache_item['info']] = apc_fetch( $cache_item['info'] );
  90. }
  91. }
  92. return $this->_get_group( $group ) ? true : false;
  93. }
  94. protected function _set( $name, $value, $expiry, $group, $keep )
  95. {
  96. if ( !$this->enabled ) {
  97. return null;
  98. }
  99. Plugins::act( 'cache_set_before', $name, $group, $value, $expiry );
  100. $this->cache_data[$group][$name] = $value;
  101. if ( $keep ) {
  102. $keepcache = apc_fetch( $this->prefix . ':keepcache' );
  103. if ( !is_array( $keepcache ) ) {
  104. $keepcache = array();
  105. }
  106. $keepcache[$group][$name] = intval( $expiry );
  107. apc_store( $this->prefix . ':keepcache', $keepcache );
  108. $expiry = 0;
  109. }
  110. apc_store( implode( ':', array( $this->prefix, $group, $name ) ), $value, intval( $expiry ) );
  111. Plugins::act( 'cache_set_after', $name, $group, $value, $expiry );
  112. return true;
  113. }
  114. /**
  115. * Expires the named value from the cache.
  116. *
  117. * @param string $name The name of the cached item
  118. * @param string $match_mode (optional) how to match bucket names ('strict', 'regex', 'glob') (default 'strict')
  119. */
  120. protected function _expire( $name, $group, $match_mode = 'strict' )
  121. {
  122. if ( !$this->enabled ) {
  123. return null;
  124. }
  125. $keys = array();
  126. switch ( strtolower( $match_mode ) ) {
  127. case 'glob':
  128. if ( array_key_exists( $group, $this->cache_data ) ) {
  129. $keys = preg_grep( Utils::glob_to_regex( $name ), array_keys( $this->cache_data[$group] ) );
  130. }
  131. break;
  132. case 'regex':
  133. if ( array_key_exists( $group, $this->cache_data ) ) {
  134. $keys = preg_grep( $name, array_keys( $this->cache_data[$group] ) );
  135. }
  136. break;
  137. case 'strict':
  138. default:
  139. $keys = array( $name );
  140. break;
  141. }
  142. foreach ( $keys as $key ) {
  143. Plugins::act( 'cache_expire_before', $name, $group );
  144. apc_delete( implode( ':', array( $this->prefix, $group, $key ) ) );
  145. Plugins::act( 'cache_expire_after', $name, $group );
  146. }
  147. }
  148. /**
  149. * Return whether a named cache value has expired
  150. *
  151. * @param string $name The name of the cached item
  152. * @param string $group The group of the cached item
  153. * @return boolean true if the stored value has expired
  154. */
  155. protected function _expired( $name, $group )
  156. {
  157. if ( !$this->enabled ) {
  158. return null;
  159. }
  160. // Do not check cached data, since we can return (and cache in this object) data if the cache is set to 'keep'
  161. $keepcache = apc_fetch( $this->prefix . ':keepcache' );
  162. if ( !self::_has( $name, $group ) || !isset( $keepcache[$group][$name] ) || $keepcache[$group][$name] < time() ) {
  163. return true;
  164. }
  165. else {
  166. return false;
  167. }
  168. }
  169. /**
  170. * Extend the expiration of the named cached value.
  171. *
  172. * @param string $name The name of the cached item
  173. * @param integer $expiry The duration in seconds to extend the cache expiration by
  174. */
  175. protected function _extend( $name, $expiry, $group )
  176. {
  177. if ( !$this->enabled ) {
  178. return null;
  179. }
  180. Plugins::act( 'cache_extend_before', $name, $group, $expiry );
  181. if ( $this->_has( $name, $group ) ) {
  182. $cache_data = $this->_get( $name, $group );
  183. $this->_set( implode( ':', array( $this->prefix, $group, $name ) ), $cache_data, time() + $expiry, $group );
  184. }
  185. Plugins::act( 'cache_extend_after', $name, $group, $expiry );
  186. }
  187. /**
  188. * Remove all cached items
  189. */
  190. protected function _purge()
  191. {
  192. Plugins::act( 'cache_purge_before' );
  193. $cache_info = apc_cache_info( 'user' );
  194. $delete = array();
  195. foreach ( $cache_info['cache_list'] as $cache_item ) {
  196. if ( strpos( $cache_item['info'], $this->prefix . ":" ) === 0 ) {
  197. $delete[] = $cache_item['info'];
  198. }
  199. }
  200. foreach ( $delete as $item ) {
  201. apc_delete( $item );
  202. }
  203. Plugins::act( 'cache_purge_after' );
  204. }
  205. }
  206. ?>