PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 2.0.3
JetBackup – Backup, Restore & Migrate v2.0.3
3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / com / lib / BackupGuard / Core / SGBGCache.php
backup / com / lib / BackupGuard / Core Last commit date
ISGArchiveDelegate.php 3 years ago SGBGArchive.php 3 years ago SGBGArchiveCdr.php 3 years ago SGBGArchiveHelper.php 3 years ago SGBGCache.php 3 years ago SGBGCacheableFile.php 3 years ago SGBGDirectoryTreeFile.php 3 years ago SGBGFile.php 3 years ago SGBGFileHelper.php 3 years ago SGBGJsonFile.php 3 years ago SGBGLog.php 3 years ago SGBGReloader.php 3 years ago SGBGStateFile.php 3 years ago SGBGTask.php 3 years ago
SGBGCache.php
234 lines
1 <?php
2 /*
3 @ class Cache
4 @ version 1.0.0
5 @ updated 23/01/2021
6 */
7
8 class SGBGCache
9 {
10 const CACHE_MODE_SIZE = 1;
11 const CACHE_MODE_TIMEOUT = 2;
12
13 private $cacheMode = 0;
14 private $cacheSize = 4000000; //4mb
15 private $cacheTimeout = 5; //seconds
16 private $cacheData = '';
17 private $lastFlushTs = 0;
18 private $flushCallable = null;
19
20 /**
21 *
22 * Constructor.
23 *
24 * @param callable $flushCallable the function to call when cache is flushed (data will be passed as argument)
25 * @return null
26 */
27 public function __construct($flushCallable = null)
28 {
29 //set default mode
30 $this->setCacheMode(self::CACHE_MODE_SIZE);
31
32 //we set flush timestamp to now so we don't flush the first coming data
33 $this->setLastFlushTs(time());
34
35 $this->flushCallable = $flushCallable;
36 }
37
38 /**
39 *
40 * Set the cache mode. 2 mods are available:
41 *
42 * 1) CACHE_MODE_SIZE: cache data until the specified size (in bytes) is reached.
43 * 2) CACHE_MODE_TIMEOUT: cache data until the specified timeout (in seconds) is reached.
44 *
45 * @param int $cacheMode the cache mode (use bitwise OR for multiple modes)
46 * @return null
47 */
48 public function setCacheMode($cacheMode)
49 {
50 $this->cacheMode = $cacheMode;
51 }
52
53 /**
54 *
55 * Get the cache mode.
56 * Use bitwise AND to check if individual modes are available.
57 *
58 * @return int the cache mode
59 */
60 public function getCacheMode()
61 {
62 return $this->cacheMode;
63 }
64
65 /**
66 *
67 * Set the cache size.
68 *
69 * @param int $cacheSize the cache size (in bytes)
70 * @return null
71 */
72 public function setCacheSize($cacheSize)
73 {
74 $this->cacheSize = $cacheSize;
75 }
76
77 /**
78 *
79 * Get the cache size.
80 *
81 * @return int the cache size (in bytes)
82 */
83 public function getCacheSize()
84 {
85 return $this->cacheSize;
86 }
87
88 /**
89 *
90 * Set the cache timeout inerval.
91 *
92 * @param int $cacheTimeout the cache timeout (in seconds)
93 * @return null
94 */
95 public function setCacheTimeout($cacheTimeout)
96 {
97 $this->cacheTimeout = $cacheTimeout;
98 }
99
100 /**
101 *
102 * Get the cache timeout interval.
103 *
104 * @return int the cache timeout (in seconds)
105 */
106 public function getCacheTimeout()
107 {
108 return $this->cacheTimeout;
109 }
110
111 /**
112 *
113 * Set the cache data.
114 *
115 * @param string $cacheData the cache data
116 * @return null
117 */
118 protected function setCacheData($cacheData)
119 {
120 $this->cacheData = $cacheData;
121 }
122
123 /**
124 *
125 * Get the cached data in buffer.
126 *
127 * @return string the cached data
128 */
129 public function getCacheData()
130 {
131 return $this->cacheData;
132 }
133
134 /**
135 *
136 * Set the last time data was flushed.
137 *
138 * @param int $lastFlushTs the last flush timestamp
139 * @return null
140 */
141 protected function setLastFlushTs($lastFlushTs)
142 {
143 $this->lastFlushTs = $lastFlushTs;
144 }
145
146 /**
147 *
148 * Get the last time data was flushed.
149 *
150 * @return int the last flush timestamp
151 */
152 public function getLastFlushTs()
153 {
154 return $this->lastFlushTs;
155 }
156
157 /**
158 *
159 * Check whether cache should flush.
160 *
161 * @return bool
162 */
163 public function shouldFlush()
164 {
165 if ($this->getCacheMode() & self::CACHE_MODE_SIZE) {
166 if (strlen($this->cacheData) >= $this->getCacheSize()) {
167 return true;
168 }
169 }
170
171 if ($this->getCacheMode() & self::CACHE_MODE_TIMEOUT) {
172 if (time() - $this->getLastFlushTs() >= $this->getCacheTimeout()) {
173 return true;
174 }
175 }
176
177 return false;
178 }
179
180 /**
181 *
182 * Cache data.
183 *
184 * @param string $data the data to cache
185 * @return null
186 */
187 public function cache($data)
188 {
189 if (empty($data) || !is_string($data)) {
190 return;
191 }
192
193 $this->cacheData .= $data;
194
195 if ($this->shouldFlush()) {
196 $this->setLastFlushTs(time());
197 $this->flush();
198 }
199 }
200
201 /**
202 *
203 * Flush data in cache buffer.
204 * The flush callable will be called and the cache buffer will be cleared.
205 *
206 * @return null
207 */
208 public function flush()
209 {
210 $data = $this->getCacheData();
211
212 if (empty($data)) {
213 return;
214 }
215
216 if (is_callable($this->flushCallable)) {
217 call_user_func($this->flushCallable, $data);
218 }
219
220 $this->clear();
221 }
222
223 /**
224 *
225 * Clear cache buffer without flushing.
226 *
227 * @return null
228 */
229 public function clear()
230 {
231 $this->setCacheData('');
232 }
233 }
234