silverstripe /
silverstripe-textextraction
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverStripe\TextExtraction\Cache\FileTextCache; |
||
| 4 | |||
| 5 | use Psr\SimpleCache\CacheInterface; |
||
| 6 | use SilverStripe\Assets\File; |
||
| 7 | use SilverStripe\Core\Config\Configurable; |
||
| 8 | use SilverStripe\Core\Flushable; |
||
| 9 | use SilverStripe\Core\Injector\Injector; |
||
| 10 | use SilverStripe\TextExtraction\Cache\FileTextCache; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Uses SS_Cache with a lifetime to cache extracted content |
||
| 14 | */ |
||
| 15 | class Cache implements FileTextCache, Flushable |
||
| 16 | { |
||
| 17 | use Configurable; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Lifetime of cache in seconds |
||
| 21 | * Null is indefinite |
||
| 22 | * |
||
| 23 | * @var int|null |
||
| 24 | * @config |
||
| 25 | */ |
||
| 26 | private static $lifetime = null; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 27 | |||
| 28 | /** |
||
| 29 | * @return CacheInterface |
||
| 30 | */ |
||
| 31 | protected static function get_cache() |
||
| 32 | { |
||
| 33 | $for = sprintf('%s.%s', CacheInterface::class, 'FileTextCache_Cache'); |
||
| 34 | |||
| 35 | return Injector::inst()->get($for); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * |
||
| 40 | * @param File $file |
||
| 41 | * @return string |
||
| 42 | */ |
||
| 43 | protected function getKey(File $file) |
||
| 44 | { |
||
| 45 | return md5($file->getFilename()); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * |
||
| 50 | * @param File $file |
||
| 51 | * @return mixed |
||
| 52 | */ |
||
| 53 | public function load(File $file) |
||
| 54 | { |
||
| 55 | $key = $this->getKey($file); |
||
| 56 | $cache = self::get_cache(); |
||
| 57 | |||
| 58 | return $cache->get($key); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param File $file |
||
| 63 | * @param string $content |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function save(File $file, $content) |
||
| 67 | { |
||
| 68 | $lifetime = $this->config()->get('lifetime') ?: 3600; |
||
| 69 | $key = $this->getKey($file); |
||
| 70 | $cache = self::get_cache(); |
||
| 71 | |||
| 72 | return $cache->set($key, $content, $lifetime); |
||
|
0 ignored issues
–
show
|
|||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return void |
||
| 77 | */ |
||
| 78 | public static function flush() |
||
| 79 | { |
||
| 80 | $cache = self::get_cache(); |
||
| 81 | $cache->clear(); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Alias for $this->flush() |
||
| 86 | * |
||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | public static function clear() |
||
| 90 | { |
||
| 91 | $cache = self::get_cache(); |
||
| 92 | $cache->clear(); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * |
||
| 97 | * @param File $file |
||
| 98 | * @return bool |
||
| 99 | */ |
||
| 100 | public function invalidate(File $file) |
||
| 101 | { |
||
| 102 | $key = $this->getKey($file); |
||
| 103 | $cache = self::get_cache(); |
||
| 104 | |||
| 105 | return $cache->delete($key); |
||
| 106 | } |
||
| 107 | } |
||
| 108 |