ArrayStore.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace Illuminate\Cache;
  3. use Illuminate\Contracts\Cache\LockProvider;
  4. use Illuminate\Support\Carbon;
  5. use Illuminate\Support\InteractsWithTime;
  6. class ArrayStore extends TaggableStore implements LockProvider
  7. {
  8. use InteractsWithTime, RetrievesMultipleKeys;
  9. /**
  10. * The array of stored values.
  11. *
  12. * @var array
  13. */
  14. protected $storage = [];
  15. /**
  16. * The array of locks.
  17. *
  18. * @var array
  19. */
  20. public $locks = [];
  21. /**
  22. * Indicates if values are serialized within the store.
  23. *
  24. * @var bool
  25. */
  26. protected $serializesValues;
  27. /**
  28. * Create a new Array store.
  29. *
  30. * @param bool $serializesValues
  31. * @return void
  32. */
  33. public function __construct($serializesValues = false)
  34. {
  35. $this->serializesValues = $serializesValues;
  36. }
  37. /**
  38. * Retrieve an item from the cache by key.
  39. *
  40. * @param string|array $key
  41. * @return mixed
  42. */
  43. public function get($key)
  44. {
  45. if (! isset($this->storage[$key])) {
  46. return;
  47. }
  48. $item = $this->storage[$key];
  49. $expiresAt = $item['expiresAt'] ?? 0;
  50. if ($expiresAt !== 0 && (Carbon::now()->getPreciseTimestamp(3) / 1000) >= $expiresAt) {
  51. $this->forget($key);
  52. return;
  53. }
  54. return $this->serializesValues ? unserialize($item['value']) : $item['value'];
  55. }
  56. /**
  57. * Store an item in the cache for a given number of seconds.
  58. *
  59. * @param string $key
  60. * @param mixed $value
  61. * @param int $seconds
  62. * @return bool
  63. */
  64. public function put($key, $value, $seconds)
  65. {
  66. $this->storage[$key] = [
  67. 'value' => $this->serializesValues ? serialize($value) : $value,
  68. 'expiresAt' => $this->calculateExpiration($seconds),
  69. ];
  70. return true;
  71. }
  72. /**
  73. * Increment the value of an item in the cache.
  74. *
  75. * @param string $key
  76. * @param mixed $value
  77. * @return int
  78. */
  79. public function increment($key, $value = 1)
  80. {
  81. if (! is_null($existing = $this->get($key))) {
  82. return tap(((int) $existing) + $value, function ($incremented) use ($key) {
  83. $value = $this->serializesValues ? serialize($incremented) : $incremented;
  84. $this->storage[$key]['value'] = $value;
  85. });
  86. }
  87. $this->forever($key, $value);
  88. return $value;
  89. }
  90. /**
  91. * Decrement the value of an item in the cache.
  92. *
  93. * @param string $key
  94. * @param mixed $value
  95. * @return int
  96. */
  97. public function decrement($key, $value = 1)
  98. {
  99. return $this->increment($key, $value * -1);
  100. }
  101. /**
  102. * Store an item in the cache indefinitely.
  103. *
  104. * @param string $key
  105. * @param mixed $value
  106. * @return bool
  107. */
  108. public function forever($key, $value)
  109. {
  110. return $this->put($key, $value, 0);
  111. }
  112. /**
  113. * Remove an item from the cache.
  114. *
  115. * @param string $key
  116. * @return bool
  117. */
  118. public function forget($key)
  119. {
  120. if (array_key_exists($key, $this->storage)) {
  121. unset($this->storage[$key]);
  122. return true;
  123. }
  124. return false;
  125. }
  126. /**
  127. * Remove all items from the cache.
  128. *
  129. * @return bool
  130. */
  131. public function flush()
  132. {
  133. $this->storage = [];
  134. return true;
  135. }
  136. /**
  137. * Get the cache key prefix.
  138. *
  139. * @return string
  140. */
  141. public function getPrefix()
  142. {
  143. return '';
  144. }
  145. /**
  146. * Get the expiration time of the key.
  147. *
  148. * @param int $seconds
  149. * @return float
  150. */
  151. protected function calculateExpiration($seconds)
  152. {
  153. return $this->toTimestamp($seconds);
  154. }
  155. /**
  156. * Get the UNIX timestamp, with milliseconds, for the given number of seconds in the future.
  157. *
  158. * @param int $seconds
  159. * @return float
  160. */
  161. protected function toTimestamp($seconds)
  162. {
  163. return $seconds > 0 ? (Carbon::now()->getPreciseTimestamp(3) / 1000) + $seconds : 0;
  164. }
  165. /**
  166. * Get a lock instance.
  167. *
  168. * @param string $name
  169. * @param int $seconds
  170. * @param string|null $owner
  171. * @return \Illuminate\Contracts\Cache\Lock
  172. */
  173. public function lock($name, $seconds = 0, $owner = null)
  174. {
  175. return new ArrayLock($this, $name, $seconds, $owner);
  176. }
  177. /**
  178. * Restore a lock instance using the owner identifier.
  179. *
  180. * @param string $name
  181. * @param string $owner
  182. * @return \Illuminate\Contracts\Cache\Lock
  183. */
  184. public function restoreLock($name, $owner)
  185. {
  186. return $this->lock($name, 0, $owner);
  187. }
  188. }