Apc.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\storage\cache\adapter;
  9. /**
  10. * An Alternative PHP Cache (APC) cache adapter implementation.
  11. *
  12. * The APC cache adapter is meant to be used through the `Cache` interface,
  13. * which abstracts away key generation, adapter instantiation and filter
  14. * implementation.
  15. *
  16. * A simple configuration of this adapter can be accomplished in `config/bootstrap/cache.php`
  17. * as follows:
  18. *
  19. * {{{
  20. * Cache::config(array(
  21. * 'cache-config-name' => array('adapter' => 'Apc')
  22. * ));
  23. * }}}
  24. *
  25. * This APC adapter provides basic support for `write`, `read`, `delete`
  26. * and `clear` cache functionality, as well as allowing the first four
  27. * methods to be filtered as per the Lithium filtering system. Additionally,
  28. * This adapter defines several methods that are _not_ implemented in other
  29. * adapters, and are thus non-portable - see the documentation for `Cache`
  30. * as to how these methods should be accessed.
  31. *
  32. * This adapter supports multi-key `write`, `read` and `delete` operations.
  33. *
  34. * Learn more about APC in the [PHP APC manual](http://php.net/manual/en/book.apc.php).
  35. *
  36. * @see lithium\storage\Cache::key()
  37. */
  38. class Apc extends \lithium\core\Object {
  39. /**
  40. * Class constructor.
  41. *
  42. * @param array $config
  43. */
  44. public function __construct(array $config = array()) {
  45. $defaults = array(
  46. 'prefix' => '',
  47. 'expiry' => '+1 hour'
  48. );
  49. parent::__construct($config + $defaults);
  50. }
  51. /**
  52. * Write value(s) to the cache.
  53. *
  54. * This adapter method supports multi-key write. By specifying `$key` as an
  55. * associative array of key/value pairs, `$data` is ignored and all keys that
  56. * are cached will receive an expiration time of `$expiry`.
  57. *
  58. * @param string|array $key The key to uniquely identify the cached item.
  59. * @param mixed $data The value to be cached.
  60. * @param null|string $expiry A strtotime() compatible cache time. If no expiry time is set,
  61. * then the default cache expiration time set with the cache configuration will be used.
  62. * @return closure Function returning boolean `true` on successful write, `false` otherwise.
  63. */
  64. public function write($key, $data, $expiry = null) {
  65. $expiry = ($expiry) ?: $this->_config['expiry'];
  66. return function($self, $params) use ($expiry) {
  67. $cachetime = (is_int($expiry) ? $expiry : strtotime($expiry)) - time();
  68. $key = $params['key'];
  69. if (is_array($key)) {
  70. return apc_store($key, $cachetime);
  71. }
  72. return apc_store($params['key'], $params['data'], $cachetime);
  73. };
  74. }
  75. /**
  76. * Read value(s) from the cache.
  77. *
  78. * This adapter method supports multi-key reads. By specifying `$key` as an
  79. * array of key names, this adapter will attempt to return an array of data
  80. * containing key/value pairs of the requested data.
  81. *
  82. * @param string|array $key The key to uniquely identify the cached item.
  83. * @return closure Function returning cached value on successful read, `false` otherwise.
  84. */
  85. public function read($key) {
  86. return function($self, $params) {
  87. return apc_fetch($params['key']);
  88. };
  89. }
  90. /**
  91. * Delete value from the cache.
  92. *
  93. * This adapter method supports multi-key deletes. By specifynig `$key` as an
  94. * array of key names, this adapter method will attempt to remove these keys
  95. * from the user space cache.
  96. *
  97. * @param string|array $key The key to uniquely identify the cached item.
  98. * @return closure Function returning `true` on successful delete, `false` otherwise.
  99. */
  100. public function delete($key) {
  101. return function($self, $params) {
  102. return apc_delete($params['key']);
  103. };
  104. }
  105. /**
  106. * Performs an atomic decrement operation on specified numeric cache item.
  107. *
  108. * Note that, as per the APC specification:
  109. * If the item's value is not numeric, the decrement operation has no effect
  110. * on the key - it retains it's original non-integer value.
  111. *
  112. * @param string $key Key of numeric cache item to decrement
  113. * @param integer $offset Offset to decrement - defaults to 1.
  114. * @return closure Function returning item's new value on successful decrement, else `false`
  115. */
  116. public function decrement($key, $offset = 1) {
  117. return function($self, $params) use ($offset) {
  118. return apc_dec($params['key'], $offset);
  119. };
  120. }
  121. /**
  122. * Performs an atomic increment operation on specified numeric cache item.
  123. *
  124. * Note that, as per the APC specification:
  125. * If the item's value is not numeric, the increment operation has no effect
  126. * on the key - it retains it's original non-integer value.
  127. *
  128. * @param string $key Key of numeric cache item to increment
  129. * @param integer $offset Offset to increment - defaults to 1.
  130. * @return closure Function returning item's new value on successful increment, else `false`
  131. */
  132. public function increment($key, $offset = 1) {
  133. return function($self, $params) use ($offset) {
  134. return apc_inc($params['key'], $offset);
  135. };
  136. }
  137. /**
  138. * Clears user-space cache
  139. *
  140. * @return mixed True on successful clear, false otherwise
  141. */
  142. public function clear() {
  143. return apc_clear_cache('user');
  144. }
  145. /**
  146. * Determines if the APC extension has been installed and
  147. * if the userspace cache is available.
  148. *
  149. * @return boolean `true` if enabled, `false` otherwise
  150. */
  151. public static function enabled() {
  152. $loaded = extension_loaded('apc');
  153. $isCli = (php_sapi_name() === 'cli');
  154. $enabled = (!$isCli && ini_get('apc.enabled')) || ($isCli && ini_get('apc.enable_cli'));
  155. return ($loaded && $enabled);
  156. }
  157. }
  158. ?>