I have a class that holds functions for caching using PHPFastCache
. There are quite a few static functions in here and for each one I am declaring:
$cache = phpFastCache();
Is there a better way I can do this? It just feels wrong. Below is the full class maybe there are other changes I can do.
/**
* Class Cache
* Using PHPFastCache to cache data
*/
class Cache
{
/**
* put a data in to cache with a key, and time in minuets
* @param $key
* @param $value
* @param int $minuets
* @return mixed
*/
public static function put($key, $value, $minuets = 1)
{
$cache = phpFastCache();
return $cache->set($key, $value, $minuets * 60);
}
/**
* Add value to Cache if does not already exist
* @param $key
* @param $value
* @param int $minuets
* @return bool
*/
public static function add($key, $value, $minuets = 1)
{
if (self::has($key)) {
return false;
} else {
self::put($key, $value, $minuets);
return true;
}
}
/**
* Check if value is in Cache
* @param $key
* @return mixed
*/
public static function has($key)
{
$cache = phpFastCache();
return $cache->isExisting($key);
}
/**
* Add a Key to the cache forever(25years)
* @param $key
* @param $value
* @return mixed
*/
public static function forever($key, $value)
{
return self::put($key, $value, 1 * 60 * 60 * 24 * 365 * 25);
}
/**
* Get a value from cache and then remove from cache.
* @param $key
* @return bool
*/
public static function pull($key)
{
$value = self::get($key);
self::Remove($key);
return $value;
}
/**
* get value out of cache, ability to specify default if key is not in the cache
* @param $key
* @param bool $default
* @return bool
*/
public static function get($key, $default = False)
{
$cache = phpFastCache();
if ($default !== false && self::has($key) === false) {
return $default;
} else {
return $cache->get("$key");
}
}
/**
* remove key from cache
* @param $key
*/
public static function forget($key)
{
$cache = phpFastCache();
$cache->delete($key);
}
/**
* get information on key, such as time left in cache.
* @param $key
* @return mixed
*/
public static function cacheInfo($key)
{
$cache = phpFastCache();
return $cache->getInfo($key);
}
/**
* remove everything from the cache
*/
public static function clearCache()
{
$cache = phpFastCache();
$cache->clean();
}