PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Cache/ArrayStore.php

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