ArrayEngine.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.7.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Cache\Engine;
  16. use Cake\Cache\CacheEngine;
  17. /**
  18. * Array storage engine for cache.
  19. *
  20. * Not actually a persistent cache engine. All data is only
  21. * stored in memory for the duration of a single process. While not
  22. * useful in production settings this engine can be useful in tests
  23. * or console tools where you don't want the overhead of interacting
  24. * with a cache servers, but want the work saving properties a cache
  25. * provides.
  26. */
  27. class ArrayEngine extends CacheEngine
  28. {
  29. /**
  30. * Cached data.
  31. *
  32. * Structured as [key => [exp => expiration, val => value]]
  33. *
  34. * @var array
  35. */
  36. protected $data = [];
  37. /**
  38. * Write data for key into cache
  39. *
  40. * @param string $key Identifier for the data
  41. * @param mixed $value Data to be cached
  42. * @return bool True if the data was successfully cached, false on failure
  43. */
  44. public function write($key, $value)
  45. {
  46. $key = $this->_key($key);
  47. $expires = time() + $this->_config['duration'];
  48. $this->data[$key] = ['exp' => $expires, 'val' => $value];
  49. return true;
  50. }
  51. /**
  52. * Read a key from the cache
  53. *
  54. * @param string $key Identifier for the data
  55. * @return mixed The cached data, or false if the data doesn't exist,
  56. * has expired, or if there was an error fetching it
  57. */
  58. public function read($key)
  59. {
  60. $key = $this->_key($key);
  61. if (!isset($this->data[$key])) {
  62. return false;
  63. }
  64. $data = $this->data[$key];
  65. // Check expiration
  66. $now = time();
  67. if ($data['exp'] <= $now) {
  68. unset($this->data[$key]);
  69. return false;
  70. }
  71. return $data['val'];
  72. }
  73. /**
  74. * Increments the value of an integer cached key
  75. *
  76. * @param string $key Identifier for the data
  77. * @param int $offset How much to increment
  78. * @return bool|int New incremented value, false otherwise
  79. */
  80. public function increment($key, $offset = 1)
  81. {
  82. if (!$this->read($key)) {
  83. $this->write($key, 0);
  84. }
  85. $key = $this->_key($key);
  86. $this->data[$key]['val'] += $offset;
  87. return $this->data[$key]['val'];
  88. }
  89. /**
  90. * Decrements the value of an integer cached key
  91. *
  92. * @param string $key Identifier for the data
  93. * @param int $offset How much to subtract
  94. * @return bool|int New decremented value, false otherwise
  95. */
  96. public function decrement($key, $offset = 1)
  97. {
  98. if (!$this->read($key)) {
  99. $this->write($key, 0);
  100. }
  101. $key = $this->_key($key);
  102. $this->data[$key]['val'] -= $offset;
  103. return $this->data[$key]['val'];
  104. }
  105. /**
  106. * Delete a key from the cache
  107. *
  108. * @param string $key Identifier for the data
  109. * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  110. */
  111. public function delete($key)
  112. {
  113. $key = $this->_key($key);
  114. unset($this->data[$key]);
  115. return true;
  116. }
  117. /**
  118. * Delete all keys from the cache. This will clear every cache config using APC.
  119. *
  120. * @param bool $check Unused argument required by interface.
  121. * @return bool True Returns true.
  122. */
  123. public function clear($check)
  124. {
  125. $this->data = [];
  126. return true;
  127. }
  128. /**
  129. * Returns the `group value` for each of the configured groups
  130. * If the group initial value was not found, then it initializes
  131. * the group accordingly.
  132. *
  133. * @return array
  134. */
  135. public function groups()
  136. {
  137. $result = [];
  138. foreach ($this->_config['groups'] as $group) {
  139. $key = $this->_config['prefix'] . $group;
  140. if (!isset($this->data[$key])) {
  141. $this->data[$key] = ['exp' => PHP_INT_MAX, 'val' => 1];
  142. }
  143. $value = $this->data[$key]['val'];
  144. $result[] = $group . $value;
  145. }
  146. return $result;
  147. }
  148. /**
  149. * Increments the group value to simulate deletion of all keys under a group
  150. * old values will remain in storage until they expire.
  151. *
  152. * @param string $group The group to clear.
  153. * @return bool success
  154. */
  155. public function clearGroup($group)
  156. {
  157. $key = $this->_config['prefix'] . $group;
  158. if (isset($this->data[$key])) {
  159. $this->data[$key]['val'] += 1;
  160. }
  161. return true;
  162. }
  163. }