PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 3.9.0
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v3.9.0
4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.10 4.4.11 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5.0 4.5.1
embedpress / vendor / wpdevelopers / embera / src / Embera / Cache / Filesystem.php
embedpress / vendor / wpdevelopers / embera / src / Embera / Cache Last commit date
CacheAdapter.php 5 years ago CacheInterface.php 5 years ago Filesystem.php 5 years ago
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