MemoryDriver.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Cache\Driver;
  12. use Carbon\Carbon;
  13. use Hyperf\Cache\Collector\Memory;
  14. use Hyperf\Cache\Exception\InvalidArgumentException;
  15. use Hyperf\Cache\Exception\OverflowException;
  16. use Psr\Container\ContainerInterface;
  17. class MemoryDriver extends Driver implements DriverInterface
  18. {
  19. protected ?int $size = null;
  20. protected bool $throwWhenSizeExceeded = false;
  21. public function __construct(ContainerInterface $container, array $config)
  22. {
  23. parent::__construct($container, $config);
  24. if (isset($config['size'])) {
  25. $this->size = (int) $config['size'];
  26. }
  27. if (isset($config['throw_when_size_exceeded'])) {
  28. $this->throwWhenSizeExceeded = (bool) $config['throw_when_size_exceeded'];
  29. }
  30. }
  31. public function fetch($key, $default = null): array
  32. {
  33. return [
  34. $this->has($key),
  35. $this->get($key, $default),
  36. ];
  37. }
  38. public function has($key): bool
  39. {
  40. return $this->getCollector()->has($this->getCacheKey($key));
  41. }
  42. public function get($key, $default = null): mixed
  43. {
  44. return $this->getCollector()->get(
  45. $this->getCacheKey($key),
  46. $default
  47. );
  48. }
  49. public function set($key, $value, $ttl = null): bool
  50. {
  51. if (
  52. $this->size > 0
  53. && $this->getCollector()->size() >= $this->size
  54. ) {
  55. if ($this->throwWhenSizeExceeded) {
  56. throw new OverflowException('The memory cache is full!');
  57. }
  58. return false;
  59. }
  60. $seconds = $this->secondsUntil($ttl);
  61. return $this->getCollector()->set(
  62. $this->getCacheKey($key),
  63. $value,
  64. $seconds <= 0 ? null : Carbon::now()->addSeconds($seconds)
  65. );
  66. }
  67. public function delete($key): bool
  68. {
  69. return $this->getCollector()->delete($this->getCacheKey($key));
  70. }
  71. public function clear(): bool
  72. {
  73. return $this->getCollector()->clear();
  74. }
  75. public function getMultiple($keys, $default = null): iterable
  76. {
  77. $result = [];
  78. foreach ($keys as $key) {
  79. $result[$key] = $this->get($key, $default);
  80. }
  81. return $result;
  82. }
  83. public function setMultiple($values, $ttl = null): bool
  84. {
  85. if (! is_array($values)) {
  86. throw new InvalidArgumentException('The values is invalid!');
  87. }
  88. foreach ($values as $key => $value) {
  89. $this->set($key, $value, $ttl);
  90. }
  91. return true;
  92. }
  93. public function deleteMultiple($keys): bool
  94. {
  95. foreach ($keys as $key) {
  96. $this->delete($key);
  97. }
  98. return true;
  99. }
  100. public function clearPrefix(string $prefix): bool
  101. {
  102. return $this->getCollector()->clearPrefix($this->getCacheKey($prefix));
  103. }
  104. public function getConnection(): mixed
  105. {
  106. return $this;
  107. }
  108. protected function getCollector(): Memory
  109. {
  110. return Memory::instance();
  111. }
  112. }