Filesystem.php
192 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Filesystem.php |
| 4 | * |
| 5 | * @package Embera |
| 6 | * @author Michael Pratt <yo@michael-pratt.com> |
| 7 | * @link http://www.michael-pratt.com/ |
| 8 | * |
| 9 | * For the full copyright and license information, please view the LICENSE |
| 10 | * file that was distributed with this source code. |
| 11 | */ |
| 12 | |
| 13 | namespace Embera\Cache; |
| 14 | |
| 15 | use DateTimeInterface; |
| 16 | |
| 17 | /** |
| 18 | * Simple Cache Class that stores information in the |
| 19 | * filesystem. |
| 20 | */ |
| 21 | class Filesystem extends CacheAdapter implements CacheInterface |
| 22 | { |
| 23 | /** @var string $path where the cache is being stored */ |
| 24 | protected $path; |
| 25 | |
| 26 | /** @var int $ttl Duration of the cache in seconds */ |
| 27 | protected $ttl = 0; |
| 28 | |
| 29 | /** @var bool Wether the cache is enabled or not */ |
| 30 | protected $enabled = true; |
| 31 | |
| 32 | /** |
| 33 | * Construct |
| 34 | * |
| 35 | * @param string $path |
| 36 | * @param int $ttl |
| 37 | * @return void |
| 38 | */ |
| 39 | public function __construct($path, $ttl) |
| 40 | { |
| 41 | $this->ttl = (int) $ttl; |
| 42 | if (!is_dir($path) || !is_writable($path)) { |
| 43 | $this->enabled = false; |
| 44 | } else { |
| 45 | $this->path = $path; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Enable this engine |
| 51 | * |
| 52 | * @return void |
| 53 | */ |
| 54 | public function enable() |
| 55 | { |
| 56 | $this->enabled = true; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Disable this engine |
| 61 | * |
| 62 | * @return void |
| 63 | */ |
| 64 | public function disable() |
| 65 | { |
| 66 | $this->enabled = false; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Disable this engine |
| 71 | * |
| 72 | * @return bool |
| 73 | */ |
| 74 | public function isEnabled() |
| 75 | { |
| 76 | return $this->enabled; |
| 77 | } |
| 78 | |
| 79 | /** inline {@inheritdoc} */ |
| 80 | public function get($key, $default = null) |
| 81 | { |
| 82 | if (!$this->enabled) { |
| 83 | return $default; |
| 84 | } |
| 85 | |
| 86 | $file = $this->getTargetFile($key); |
| 87 | if (file_exists($file)) { |
| 88 | $data = unserialize(file_get_contents($file)); |
| 89 | |
| 90 | if ($data['expire_time'] <= time()) { |
| 91 | $this->delete($key); |
| 92 | return $default; |
| 93 | } |
| 94 | |
| 95 | return $data['content']; |
| 96 | } |
| 97 | |
| 98 | return $default; |
| 99 | } |
| 100 | |
| 101 | /** inline {@inheritdoc} */ |
| 102 | public function set($key, $value, $ttl = null) |
| 103 | { |
| 104 | if (!$this->enabled) { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | $expiration = $this->calculateExpiration($ttl); |
| 109 | $createFile = file_put_contents($this->getTargetFile($key), serialize([ |
| 110 | 'expire_time' => (int) $expiration, |
| 111 | 'expire_time_date' => date('Y-m-d H:i:s', $expiration), |
| 112 | 'created' => date('Y-m-d H:i:s'), |
| 113 | 'content' => $value, |
| 114 | ]), LOCK_EX); |
| 115 | |
| 116 | return ($createFile !== false && $createFile > 0); |
| 117 | } |
| 118 | |
| 119 | /** inline {@inheritdoc} */ |
| 120 | public function delete($key) |
| 121 | { |
| 122 | if (!$this->enabled) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | $file = $this->getTargetFile($key); |
| 127 | if (file_exists($file)) { |
| 128 | @unlink($file); |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | /** inline {@inheritdoc} */ |
| 136 | public function clear() |
| 137 | { |
| 138 | if (!$this->enabled) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | $pattern = rtrim($this->path, '/') . '/embera-*.cache'; |
| 143 | if ($list = glob($pattern)) { |
| 144 | foreach ($list as $file) { |
| 145 | @unlink($file); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | /** inline {@inheritdoc} */ |
| 153 | public function has($key) |
| 154 | { |
| 155 | if (!$this->enabled) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | $file = $this->getTargetFile($key); |
| 160 | return (file_exists($file)); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Calculates the expiration date of a cache item, |
| 165 | * based on the given $ttl |
| 166 | * |
| 167 | * @param int|\DateTime $ttl |
| 168 | * @return int |
| 169 | */ |
| 170 | protected function calculateExpiration($ttl = null) |
| 171 | { |
| 172 | if (is_numeric($ttl)) |
| 173 | return (time() + $ttl); |
| 174 | else if ($ttl instanceof DateTimeInterface) |
| 175 | return $ttl->getTimestamp(); |
| 176 | else |
| 177 | return (time() + $this->ttl); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Generates the filename for the given $key |
| 182 | * |
| 183 | * @param string $key |
| 184 | * @return string |
| 185 | */ |
| 186 | protected function getTargetFile($key) |
| 187 | { |
| 188 | return rtrim($this->path, '/') . '/embera-' . md5($key) . '.cache'; |
| 189 | } |
| 190 | |
| 191 | } |
| 192 |