Skip to content

Instantly share code, notes, and snippets.

@pete-rai
Created January 11, 2019 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pete-rai/f3fbe8d1037573045e2b28d8c9bd97ae to your computer and use it in GitHub Desktop.
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
<?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
*/
@pete-rai
Copy link
Author

If you need a simpler cache without file expiry built in, you can grab one here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment