FileSystemDriver.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 Hyperf\Cache\Collector\FileStorage;
  13. use Hyperf\Cache\Exception\CacheException;
  14. use Hyperf\Cache\Exception\InvalidArgumentException;
  15. use Hyperf\Support\Filesystem\Filesystem;
  16. use Psr\Container\ContainerInterface;
  17. class FileSystemDriver extends Driver
  18. {
  19. /**
  20. * @var string
  21. */
  22. protected $storePath = BASE_PATH . '/runtime/caches';
  23. /**
  24. * @var Filesystem
  25. */
  26. private $filesystem;
  27. public function __construct(ContainerInterface $container, array $config)
  28. {
  29. parent::__construct($container, $config);
  30. if (! is_dir($this->storePath)) {
  31. $results = mkdir($this->storePath, 0777, true);
  32. if (! $results) {
  33. throw new CacheException('Has no permission to create cache directory!');
  34. }
  35. }
  36. $this->filesystem = $container->get(Filesystem::class);
  37. }
  38. public function getCacheKey(string $key)
  39. {
  40. return $this->getPrefix() . $key . '.cache';
  41. }
  42. public function get($key, $default = null): mixed
  43. {
  44. $file = $this->getCacheKey($key);
  45. if (! file_exists($file)) {
  46. return $default;
  47. }
  48. /** @var FileStorage $obj */
  49. $obj = $this->packer->unpack($this->filesystem->get($file));
  50. if ($obj->isExpired()) {
  51. return $default;
  52. }
  53. return $obj->getData();
  54. }
  55. public function fetch(string $key, $default = null): array
  56. {
  57. $file = $this->getCacheKey($key);
  58. if (! file_exists($file)) {
  59. return [false, $default];
  60. }
  61. /** @var FileStorage $obj */
  62. $obj = $this->packer->unpack($this->filesystem->get($file));
  63. if ($obj->isExpired()) {
  64. return [false, $default];
  65. }
  66. return [true, $obj->getData()];
  67. }
  68. public function set($key, $value, $ttl = null): bool
  69. {
  70. $seconds = $this->secondsUntil($ttl);
  71. $file = $this->getCacheKey($key);
  72. $content = $this->packer->pack(new FileStorage($value, $seconds));
  73. $result = $this->filesystem->put($file, $content);
  74. return (bool) $result;
  75. }
  76. public function delete($key): bool
  77. {
  78. $file = $this->getCacheKey($key);
  79. if (file_exists($file)) {
  80. if (! is_writable($file)) {
  81. return false;
  82. }
  83. unlink($file);
  84. }
  85. return true;
  86. }
  87. public function clear(): bool
  88. {
  89. return $this->clearPrefix('');
  90. }
  91. public function getMultiple($keys, $default = null): iterable
  92. {
  93. if (! is_array($keys)) {
  94. throw new InvalidArgumentException('The keys is invalid!');
  95. }
  96. $result = [];
  97. foreach ($keys as $key) {
  98. $result[$key] = $this->get($key, $default);
  99. }
  100. return $result;
  101. }
  102. public function setMultiple($values, $ttl = null): bool
  103. {
  104. if (! is_array($values)) {
  105. throw new InvalidArgumentException('The values is invalid!');
  106. }
  107. $seconds = $this->secondsUntil($ttl);
  108. foreach ($values as $key => $value) {
  109. $this->set($key, $value, $seconds);
  110. }
  111. return true;
  112. }
  113. public function deleteMultiple($keys): bool
  114. {
  115. if (! is_array($keys)) {
  116. throw new InvalidArgumentException('The keys is invalid!');
  117. }
  118. foreach ($keys as $key) {
  119. $this->delete($key);
  120. }
  121. return true;
  122. }
  123. public function has($key): bool
  124. {
  125. $file = $this->getCacheKey($key);
  126. return file_exists($file);
  127. }
  128. public function clearPrefix(string $prefix): bool
  129. {
  130. $files = glob($this->getPrefix() . $prefix . '*');
  131. foreach ($files as $file) {
  132. if (is_dir($file)) {
  133. continue;
  134. }
  135. unlink($file);
  136. }
  137. return true;
  138. }
  139. protected function getPrefix()
  140. {
  141. return $this->storePath . DIRECTORY_SEPARATOR . $this->prefix;
  142. }
  143. }