bytic /
cache
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Nip\Cache; |
||
| 4 | |||
| 5 | use Nip\Utility\Traits\SingletonTrait; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Class Manager |
||
| 9 | * @package Nip\Cache |
||
| 10 | */ |
||
| 11 | class Manager |
||
| 12 | { |
||
| 13 | use SingletonTrait; |
||
| 14 | |||
| 15 | protected $active = true; |
||
| 16 | |||
| 17 | protected $ttl = 180; |
||
| 18 | |||
| 19 | protected $data; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @param $cacheId |
||
| 23 | * |
||
| 24 | * @return mixed |
||
| 25 | */ |
||
| 26 | public function get($cacheId) |
||
| 27 | { |
||
| 28 | if (!$this->valid($cacheId)) { |
||
| 29 | return null; |
||
| 30 | } |
||
| 31 | |||
| 32 | return $this->getData($cacheId); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param $cacheId |
||
| 37 | * |
||
| 38 | * @return bool |
||
| 39 | */ |
||
| 40 | public function valid($cacheId) |
||
| 41 | { |
||
| 42 | if ($this->isActive() && $this->exists($cacheId)) { |
||
| 43 | $fmtime = filemtime($this->filePath($cacheId)); |
||
| 44 | if (($fmtime + $this->ttl) > time()) { |
||
| 45 | return true; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | return false; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return bool |
||
| 54 | */ |
||
| 55 | public function isActive() |
||
| 56 | { |
||
| 57 | return $this->active; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param $active |
||
| 62 | * |
||
| 63 | * @return $this |
||
| 64 | */ |
||
| 65 | public function setActive($active) |
||
| 66 | { |
||
| 67 | $this->active = $active; |
||
| 68 | |||
| 69 | return $this; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param $cacheId |
||
| 74 | * |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | public function exists($cacheId) |
||
| 78 | { |
||
| 79 | return file_exists($this->filePath($cacheId)); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param $cacheId |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function filePath($cacheId) |
||
| 88 | { |
||
| 89 | $cacheId = DIRECTORY_SEPARATOR . trim($cacheId, DIRECTORY_SEPARATOR); |
||
| 90 | return $this->cachePath() . $cacheId . '.php'; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function cachePath() |
||
| 97 | { |
||
| 98 | return \cache_path(); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param $cacheId |
||
| 103 | * |
||
| 104 | * @return mixed |
||
| 105 | */ |
||
| 106 | public function getData($cacheId) |
||
| 107 | { |
||
| 108 | if (!isset($this->data[$cacheId])) { |
||
| 109 | $this->data[$cacheId] = $this->loadData($cacheId); |
||
| 110 | } |
||
| 111 | |||
| 112 | return $this->data[$cacheId]; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param $cacheId |
||
| 117 | * @param bool $retry |
||
| 118 | * |
||
| 119 | * @return bool|mixed |
||
| 120 | */ |
||
| 121 | public function loadData($cacheId, $retry = true) |
||
| 122 | { |
||
| 123 | $file = $this->filePath($cacheId); |
||
| 124 | $content = file_get_contents($file); |
||
| 125 | $data = unserialize($content); |
||
| 126 | |||
| 127 | if (!$data) { |
||
| 128 | if ($retry === false) { |
||
| 129 | return false; |
||
| 130 | } |
||
| 131 | $this->reload($cacheId); |
||
| 132 | |||
| 133 | return $this->loadData($cacheId, false); |
||
| 134 | } |
||
| 135 | |||
| 136 | return $data; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param $cacheId |
||
| 141 | */ |
||
| 142 | public function reload($cacheId) |
||
|
0 ignored issues
–
show
|
|||
| 143 | { |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param $cacheId |
||
| 148 | * @param $data |
||
| 149 | * |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | public function set($cacheId, $data) |
||
| 153 | { |
||
| 154 | $this->data[$cacheId] = $data; |
||
| 155 | |||
| 156 | return $this; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function put($cacheId, $data) |
||
| 160 | { |
||
| 161 | $this->set($cacheId, $data); |
||
| 162 | $this->saveData($cacheId, $data); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param $cacheId |
||
| 167 | * @param $data |
||
| 168 | * |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | public function saveData($cacheId, $data) |
||
| 172 | { |
||
| 173 | $file = $this->filePath($cacheId); |
||
| 174 | $content = serialize($data); |
||
| 175 | |||
| 176 | return $this->save($file, $content); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param $file |
||
| 181 | * @param $content |
||
| 182 | * |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public function save($file, $content) |
||
| 186 | { |
||
| 187 | $dir = dirname($file); |
||
| 188 | |||
| 189 | if (!is_dir($dir)) { |
||
| 190 | mkdir($dir, 0777); |
||
| 191 | } |
||
| 192 | |||
| 193 | if (file_put_contents($file, $content)) { |
||
| 194 | chmod($file, 0777); |
||
| 195 | |||
| 196 | return true; |
||
| 197 | } else { |
||
| 198 | $message = "Cannot open cache file for writing: "; |
||
| 199 | // if (!Nip_Staging::instance()->isPublic()) { |
||
| 200 | // $message .= " [ ".$this->cache_file." ] "; |
||
| 201 | // } |
||
| 202 | die($message); |
||
|
0 ignored issues
–
show
|
|||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param $cacheId |
||
| 208 | */ |
||
| 209 | public function delete($cacheId) |
||
| 210 | { |
||
| 211 | $file = $this->filePath($cacheId); |
||
| 212 | if (is_file($file)) { |
||
| 213 | unlink($file); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return int |
||
| 219 | */ |
||
| 220 | public function getTtl(): int |
||
| 221 | { |
||
| 222 | return $this->ttl; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param int $ttl |
||
| 227 | */ |
||
| 228 | public function setTtl(int $ttl) |
||
| 229 | { |
||
| 230 | $this->ttl = $ttl; |
||
| 231 | } |
||
| 232 | } |
||
| 233 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.