|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of php-cache\filesystem-adapter package. |
| 5 | + * |
| 6 | + * (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> |
| 7 | + * |
| 8 | + * This source file is subject to the MIT license that is bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Cache\Adapter\Filesystem; |
| 13 | + |
| 14 | +use Cache\Adapter\Common\AbstractCachePool; |
| 15 | +use Cache\Adapter\Common\Exception\InvalidArgumentException; |
| 16 | +use League\Flysystem\FileNotFoundException; |
| 17 | +use League\Flysystem\Filesystem; |
| 18 | +use Psr\Cache\CacheItemInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Tobias Nyholm <[email protected]> |
| 22 | + */ |
| 23 | +class FilesystemCachePool extends AbstractCachePool |
| 24 | +{ |
| 25 | + const CACHE_PATH = 'cache'; |
| 26 | + /** |
| 27 | + * @type Filesystem |
| 28 | + */ |
| 29 | + private $filesystem; |
| 30 | + |
| 31 | + /** |
| 32 | + * @param Filesystem $filesystem |
| 33 | + */ |
| 34 | + public function __construct(Filesystem $filesystem) |
| 35 | + { |
| 36 | + $this->filesystem = $filesystem; |
| 37 | + $this->filesystem->createDir(self::CACHE_PATH); |
| 38 | + } |
| 39 | + |
| 40 | + protected function fetchObjectFromCache($key) |
| 41 | + { |
| 42 | + $file = $this->getFilePath($key); |
| 43 | + if (!$this->filesystem->has($file)) { |
| 44 | + return [false, null]; |
| 45 | + } |
| 46 | + |
| 47 | + $data = unserialize($this->filesystem->read($file)); |
| 48 | + if ($data[0] !== null && time() > $data[0]) { |
| 49 | + $this->clearOneObjectFromCache($key); |
| 50 | + |
| 51 | + return [false, null]; |
| 52 | + } |
| 53 | + |
| 54 | + return [true, $data[1]]; |
| 55 | + } |
| 56 | + |
| 57 | + protected function clearAllObjectsFromCache() |
| 58 | + { |
| 59 | + $this->filesystem->deleteDir(self::CACHE_PATH); |
| 60 | + $this->filesystem->createDir(self::CACHE_PATH); |
| 61 | + |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + protected function clearOneObjectFromCache($key) |
| 66 | + { |
| 67 | + try { |
| 68 | + return $this->filesystem->delete($this->getFilePath($key)); |
| 69 | + } catch (FileNotFoundException $e) { |
| 70 | + return true; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + protected function storeItemInCache($key, CacheItemInterface $item, $ttl) |
| 75 | + { |
| 76 | + $file = $this->getFilePath($key); |
| 77 | + if ($this->filesystem->has($file)) { |
| 78 | + $this->filesystem->delete($file); |
| 79 | + } |
| 80 | + |
| 81 | + return $this->filesystem->write($file, serialize([ |
| 82 | + ($ttl === null ? null : time() + $ttl), |
| 83 | + $item->get(), |
| 84 | + ])); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param string $key |
| 89 | + * |
| 90 | + * @throws InvalidArgumentException |
| 91 | + * |
| 92 | + * @return string |
| 93 | + */ |
| 94 | + private function getFilePath($key) |
| 95 | + { |
| 96 | + if (!preg_match('|^[a-zA-Z0-9_\.! ]+$|', $key)) { |
| 97 | + throw new InvalidArgumentException(sprintf('Invalid key "%s". Valid keys must match [a-zA-Z0-9_\.! ].', $key)); |
| 98 | + } |
| 99 | + |
| 100 | + return sprintf('%s/%s', self::CACHE_PATH, $key); |
| 101 | + } |
| 102 | +} |
0 commit comments