diff options
| author | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-04-27 03:47:30 +0300 |
|---|---|---|
| committer | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-04-27 03:47:30 +0300 |
| commit | 059e0d95d0c28bc5060e87e146eaf7411f51bf90 (patch) | |
| tree | 7c21ac432f16dde2329a0d5954d70711eceb48e6 /core/classes/cache/FileCache.php | |
| parent | 26cd8ee99659ef1926c96a049c93645ffc9b169d (diff) | |
| download | gyraf1gov-059e0d95d0c28bc5060e87e146eaf7411f51bf90.tar.gz gyraf1gov-059e0d95d0c28bc5060e87e146eaf7411f51bf90.tar.bz2 gyraf1gov-059e0d95d0c28bc5060e87e146eaf7411f51bf90.zip | |
skeleton commit; based on an anom project
Diffstat (limited to 'core/classes/cache/FileCache.php')
| -rw-r--r-- | core/classes/cache/FileCache.php | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/core/classes/cache/FileCache.php b/core/classes/cache/FileCache.php new file mode 100644 index 0000000..fb7eee5 --- /dev/null +++ b/core/classes/cache/FileCache.php @@ -0,0 +1,93 @@ +<?php + +/* FileCache + * --- + * Saves serialized data into filesystem. + * Use it for expensive database-queries. + * Performanve on a NVMe-SSD drive is really great; + * (even better than MemCached and Redis) + * performance on a typical HDD is just fair. + * + * NOTE: + * Memcached and Redis are much faster cache-technologies + * and generally recommended; but they are not always available; + * FileCache is (almost) always available; + * + * Before tou decide on your caching technology + * run some benchmarks. + */ +class FileCache implements Cache_interface +{ + + /** store + * --- + * serializes and saves data in a file + * along with TTL (time-to-live) + */ + public function set(string $key, $data, int $ttl) + { + // filename is a hashed 48-hex-digit string + // probability of collision = exp( (-k*(k-1)) / 2N ) + // ex: for 10tril.samples P(collision) = 1.5E-11% + $filename = FILECACHE_PATH . sha1($key); + + // Opening file for write + $h = fopen($filename, 'w'); + if (!$h) throw new Exception('Can not write to cache'); + + // Serialize along with the TTL + $data = serialize( array( + time() + $ttl, // array[0] holds expiration time + $data) // array[1] holds the actual data + ); + + if (fwrite($h,$data) === false) { + throw new Exception('Can not write to cache'); + } + fclose($h); + } + + + /** fetch + * --- + * feth data for certain key + * @param $key (string) + * @return fetched $data -or- false on failure + */ + public function get(string $key) + { + $filename = FILECACHE_PATH . sha1($key); + + // can not read the cache-file? return false + if (!file_exists($filename) || !is_readable($filename)) return false; + + $data = file_get_contents($filename); // get cache-file contents + $data = @unserialize($data); // unserialize + + if (!$data) { + + // Unlinking the file when unserializing failed + unlink($filename); + return false; + + } + + // checking if the data was expired + if (time() > $data[0]) { + + // Unlinking + unlink($filename); + return false; + + } + + return $data[1]; + } + + + public function flush(int $olderThan = 0) + { + return false; // reply false until implementation + } + +} |
