ArrayStore.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Illuminate\Cache;
  3. use Illuminate\Contracts\Cache\Store;
  4. class ArrayStore extends TaggableStore implements Store
  5. {
  6. use RetrievesMultipleKeys;
  7. /**
  8. * The array of stored values.
  9. *
  10. * @var array
  11. */
  12. protected $storage = [];
  13. /**
  14. * Retrieve an item from the cache by key.
  15. *
  16. * @param string|array $key
  17. * @return mixed
  18. */
  19. public function get($key)
  20. {
  21. return $this->storage[$key] ?? null;
  22. }
  23. /**
  24. * Store an item in the cache for a given number of minutes.
  25. *
  26. * @param string $key
  27. * @param mixed $value
  28. * @param float|int $minutes
  29. * @return void
  30. */
  31. public function put($key, $value, $minutes)
  32. {
  33. $this->storage[$key] = $value;
  34. }
  35. /**
  36. * Increment the value of an item in the cache.
  37. *
  38. * @param string $key
  39. * @param mixed $value
  40. * @return int
  41. */
  42. public function increment($key, $value = 1)
  43. {
  44. $this->storage[$key] = ! isset($this->storage[$key])
  45. ? $value : ((int) $this->storage[$key]) + $value;
  46. return $this->storage[$key];
  47. }
  48. /**
  49. * Decrement the value of an item in the cache.
  50. *
  51. * @param string $key
  52. * @param mixed $value
  53. * @return int
  54. */
  55. public function decrement($key, $value = 1)
  56. {
  57. return $this->increment($key, $value * -1);
  58. }
  59. /**
  60. * Store an item in the cache indefinitely.
  61. *
  62. * @param string $key
  63. * @param mixed $value
  64. * @return void
  65. */
  66. public function forever($key, $value)
  67. {
  68. $this->put($key, $value, 0);
  69. }
  70. /**
  71. * Remove an item from the cache.
  72. *
  73. * @param string $key
  74. * @return bool
  75. */
  76. public function forget($key)
  77. {
  78. unset($this->storage[$key]);
  79. return true;
  80. }
  81. /**
  82. * Remove all items from the cache.
  83. *
  84. * @return bool
  85. */
  86. public function flush()
  87. {
  88. $this->storage = [];
  89. return true;
  90. }
  91. /**
  92. * Get the cache key prefix.
  93. *
  94. * @return string
  95. */
  96. public function getPrefix()
  97. {
  98. return '';
  99. }
  100. }