#29 - PHP Simple Cache Class
Data: 2018-10-06 12:00 - PHP
Simple class for caching values in files.
<?php
define('CACHE_FOLDER', 'cache');
class Cache {
public static function hasCacheFor($name, $maxSecondsOld) {
$cacheFile = static::getFilename($name);
return file_exists($cacheFile) && time() - filemtime($cacheFile) <= $maxSecondsOld;
}
public static function get($name) {
$cacheFile = static::getFilename($name);
if(file_exists($cacheFile))
return unserialize(file_get_contents($cacheFile));
return null;
}
public static function set($name, $value) {
file_put_contents(static::getFilename($name), serialize($value));
}
private static function getFilename($name) {
return './' . CACHE_FOLDER . '/' . $name;
}
}