summaryrefslogtreecommitdiff
path: root/classes/cache/FileCache.php
diff options
context:
space:
mode:
Diffstat (limited to 'classes/cache/FileCache.php')
-rw-r--r--classes/cache/FileCache.php93
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
- }
-
-}