Created
January 11, 2019 12:57
-
-
Save pete-rai/f3fbe8d1037573045e2b28d8c9bd97ae to your computer and use it in GitHub Desktop.
A simple PHP file based cache with support for cache expiry and storing any data type or structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* A simple PHP file based cache with support for cache expiry and storing any data type or structure | |
* | |
* Released with the karmaware tag - https://pete-rai.github.io/karmaware | |
* | |
* Website : http://www.rai.org.uk | |
* GitHub : https://github.com/pete-rai | |
* LinkedIn : https://uk.linkedin.com/in/raipete | |
* NPM : https://www.npmjs.com/~peterai | |
* | |
*/ | |
define ('CACHE_EXPIRY' , PHP_INT_MAX); // in seconds, defaults to never | |
define ('CACHE_CLEAN' , true ); // auto clean on each get - if false do it manually by calling clean | |
define ('CACHE_PATH' , '../cache/' ); // needs trailing slash | |
define ('CACHE_PREFIX' , 'cache_' ); | |
define ('CACHE_EXT' , '.txt' ); // needs preceding dot | |
// --- a simple file cache | |
class Cache | |
{ | |
protected $expiry = CACHE_EXPIRY; | |
protected $prefix = CACHE_PREFIX; | |
// --- constructor | |
public function __construct ($prefix = '', $expiry = 0) // prefix with no underscore and expiry in seconds | |
{ | |
if ($prefix) $this->prefix = $prefix; | |
if ($expiry) $this->expiry = $expiry; | |
} | |
// --- returns the cache file name for a given hash | |
protected function file ($hash) | |
{ | |
return dirname (__FILE__).'/'.CACHE_PATH.$this->prefix.'_'.$hash.CACHE_EXT; | |
} | |
// --- returns whether a given cache file has expired | |
protected function expired ($file) | |
{ | |
return time () - filemtime ($file) > $this->expiry; | |
} | |
// --- tests the existence of a non-expired entry for a given hash | |
public function has ($hash) | |
{ | |
$file = $this->file ($hash); | |
return file_exists ($file) && !$this->expired ($file); | |
} | |
// --- gets the contents of the cache for a given hash | |
public function get ($hash) // null return means missing or expired | |
{ | |
if (CACHE_CLEAN) | |
{ | |
$this->clean (); // clean old files from the cache on every call | |
} | |
$body = null; | |
if ($this->has ($hash)) | |
{ | |
$body = unserialize (file_get_contents ($this->file ($hash))); | |
} | |
return $body; | |
} | |
// --- puts contents into the cache for a given hash | |
public function put ($hash, $body) | |
{ | |
file_put_contents ($this->file ($hash), serialize ($body)); | |
} | |
// --- clears the cache file for the given hash | |
public function clear ($hash) | |
{ | |
if ($this->has ($hash)) | |
{ | |
unlink ($this->file ($hash)); | |
} | |
} | |
// --- resets the cache by deleting all the files | |
public function reset () | |
{ | |
foreach (glob ($this->file ('*')) as $file) | |
{ | |
unlink ($file); | |
} | |
} | |
// --- cleans the cache by deleting all the expired files | |
public function clean () | |
{ | |
foreach (glob ($this->file ('*')) as $file) | |
{ | |
if ($this->expired ($file)) | |
{ | |
unlink ($file); | |
} | |
} | |
} | |
} | |
/* | |
// --- test code only - leave commented out once working | |
$expiry = 20; // seconds | |
$cache = new Cache ('test', $expiry); | |
// helper function to show boolean output | |
function show ($bool) { echo ($bool ? 'T' : 'F')."\n"; } | |
// cache tests | |
$cache->reset (); // deletes all existing cache files | |
show ($cache->has ("foobar")); // will give F | |
$cache->put ("foobar", ["plinth", "gusset", "catflap"]); // makes the cache file ./cache/test_foobar.txt | |
show ($cache->has ("foobar")); // will give T | |
var_dump ($cache->get ("foobar")); // will output the data | |
sleep ($expiry + 1); | |
show ($cache->has ("foobar")); // will give F - item has expired and auto clean is on | |
var_dump ($cache->get ("foobar")); // will output null | |
$cache->put ("foobar", ["plinth", "gusset", "catflap"]); // makes the cache file | |
show ($cache->has ("foobar")); // will give T | |
var_dump ($cache->get ("foobar")); // will output the data | |
$cache->clear ("foobar"); // deletes the cache file | |
show ($cache->has ("foobar")); // will give F | |
var_dump ($cache->get ("foobar")); // will output null | |
// end tests | |
$cache->reset (); // deletes all existing cache files | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you need a simpler cache without file expiry built in, you can grab one here