diff options
| author | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-03-12 07:16:23 +0200 |
|---|---|---|
| committer | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-03-12 07:16:23 +0200 |
| commit | 7076343338ae3439f3c86f01144818abe8c31978 (patch) | |
| tree | 794abb1e6b8f821091fd095341885496b6dad51d /classes/cache/FileCache.php | |
| parent | 47cbb529f5723b246125ae083a193e11481b89ef (diff) | |
| download | classroom-7076343338ae3439f3c86f01144818abe8c31978.tar.gz classroom-7076343338ae3439f3c86f01144818abe8c31978.tar.bz2 classroom-7076343338ae3439f3c86f01144818abe8c31978.zip | |
add container helpers; constuct public directory-tree
Diffstat (limited to 'classes/cache/FileCache.php')
| -rw-r--r-- | classes/cache/FileCache.php | 93 |
1 files changed, 0 insertions, 93 deletions
diff --git a/classes/cache/FileCache.php b/classes/cache/FileCache.php deleted file mode 100644 index fb7eee5..0000000 --- a/classes/cache/FileCache.php +++ /dev/null @@ -1,93 +0,0 @@ -<?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 - } - -} |
