Skip to content

Instantly share code, notes, and snippets.

@pete-rai
Created January 3, 2019 17:29
Show Gist options
  • Save pete-rai/f6aad6eedb29501defcbe2a95c84ac90 to your computer and use it in GitHub Desktop.
Save pete-rai/f6aad6eedb29501defcbe2a95c84ac90 to your computer and use it in GitHub Desktop.
A super simple PHP file based cache for any data type or structure
<?php
/*
* A super simple PHP file based cache for 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
*
*/
class Cache
{
protected static $dir = './cache/'; // with the trailing slash
protected static $ext = '.txt'; // with leading dot
protected static function file ($tags)
{
return self::$dir.implode ('-', is_array ($tags) ? $tags : [$tags]).self::$ext;
}
public static function has ($tags)
{
return file_exists (self::file ($tags));
}
public static function lacking ($tags)
{
return !self::has ($tags);
}
public static function get ($tags)
{
return unserialize (file_get_contents (self::file ($tags)));
}
public static function put ($tags, $data)
{
file_put_contents (self::file ($tags), serialize ($data));
}
public static function clear ($tags)
{
if (self::has ($tags)) unlink (self::file ($tags));
}
public static function flush ()
{
array_map ('unlink', glob (self::file ('*')));
}
}
/*
// --- test code only - leave commented out once working
// you can store any type of data: simple data types, arrays, objects, etc
$data1 = "test string";
$data2 = ["test array item 1", "test array item 2", "test array item 3"];
// you use tags to build up the cache filename
$tags1 = "foo"; // ./cache/foo.txt
$tags2 = ["foo", "bar"]; // ./cache/foo-bar.txt
// helper function to show boolean output
function show ($bool) { echo ($bool ? 'T' : 'F')."\n"; }
// start tests
Cache::flush (); // deletes all existing cache files
// first test
show (Cache::has ($tags1)); // will give F
show (Cache::lacking ($tags1)); // will give T
Cache::put ($tags1, $data1); // makes the cache file
show (Cache::has ($tags1)); // will give T
show (Cache::lacking ($tags1)); // will give F
var_dump (Cache::get ($tags1)); // will give $data1
Cache::clear ($tags1); // deletes the cache file
show (Cache::has ($tags1)); // will give F
show (Cache::lacking ($tags1)); // will give T
// second test
show (Cache::has ($tags2)); // will give F
show (Cache::lacking ($tags2)); // will give T
Cache::put ($tags2, $data2); // makes the cache file
show (Cache::has ($tags2)); // will give T
show (Cache::lacking ($tags2)); // will give F
var_dump (Cache::get ($tags2)); // will give $data2
Cache::clear ($tags2); // deletes the cache file
show (Cache::has ($tags2)); // will give F
show (Cache::lacking ($tags2)); // will give T
// end tests
Cache::flush (); // deletes all existing cache files
*/
@pete-rai
Copy link
Author

If you need a cache which supports automatic file expiry, take a look at my other PHP cache gist here

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