You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NullDriver.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php declare(strict_types=1);
  2. /**
  3. * Banker
  4. *
  5. * A Caching library implementing psr/cache
  6. *
  7. * PHP version 7.1
  8. *
  9. * @package Banker
  10. * @author Timothy J. Warren <tim@timshomepage.net>
  11. * @copyright 2016 - 2018 Timothy J. Warren
  12. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  13. * @version 2.0.0
  14. * @link https://git.timshomepage.net/timw4mail/banker
  15. */
  16. namespace Aviat\Banker\Driver;
  17. /**
  18. * Cache backend for use without a cache server. Only does transient
  19. * in-memory caching
  20. */
  21. class NullDriver extends AbstractDriver {
  22. /**
  23. * In memory store
  24. *
  25. * @var array
  26. */
  27. protected $store = [];
  28. /**
  29. * NullDriver constructor.
  30. *
  31. * @param array $config
  32. * @param array $options
  33. */
  34. public function __construct(array $config = [], array $options = [])
  35. {
  36. $this->store = [];
  37. }
  38. /**
  39. * Clean up nothing
  40. */
  41. public function __destruct()
  42. {
  43. //noop
  44. }
  45. /**
  46. * See if a key currently exists in the cache
  47. *
  48. * @param string $key
  49. * @return bool
  50. */
  51. public function exists(string $key): bool
  52. {
  53. return array_key_exists($key, $this->store);
  54. }
  55. /**
  56. * Get the value for the selected cache key
  57. *
  58. * @param string $key
  59. * @return mixed
  60. */
  61. public function get(string $key)
  62. {
  63. return $this->exists($key)
  64. ? $this->store[$key]
  65. : NULL;
  66. }
  67. /**
  68. * Set a cached value
  69. *
  70. * @param string $key
  71. * @param mixed $value
  72. * @param int $expires
  73. * @return DriverInterface
  74. */
  75. public function set(string $key, $value, int $expires = 0): DriverInterface
  76. {
  77. $this->store[$key] = $value;
  78. return $this;
  79. }
  80. /**
  81. * Remove an item from the cache
  82. *
  83. * @param string $key
  84. * @return boolean
  85. */
  86. public function delete(string $key): bool
  87. {
  88. unset($this->store[$key]);
  89. return ( ! array_key_exists($key, $this->store));
  90. }
  91. /**
  92. * Remove multiple items from the cache
  93. *
  94. * @param string[] $keys
  95. * @return boolean
  96. */
  97. public function deleteMultiple(array $keys = []): bool
  98. {
  99. $res = TRUE;
  100. foreach($keys as $key)
  101. {
  102. $res = $res && $this->delete($key);
  103. }
  104. return $res;
  105. }
  106. /**
  107. * Empty the cache
  108. *
  109. * @return boolean
  110. */
  111. public function flush(): bool
  112. {
  113. $this->store = [];
  114. return TRUE;
  115. }
  116. /**
  117. * Set the specified key to expire at the given time
  118. *
  119. * @param string $key
  120. * @param int $expires
  121. * @return boolean
  122. */
  123. public function expiresAt(string $key, int $expires): bool
  124. {
  125. //noop
  126. return array_key_exists($key, $this->store);
  127. }
  128. }