-
-
Notifications
You must be signed in to change notification settings - Fork 1
Cache
You may need to import this feature on top of your code.
use \Scarlets\Library\Cache;
Before you use this library, you must set the configuration in /config/cache.php
.
The currently supported driver is localfile
.
The default credential can be set with cache_storage
and you will need to connect to the cache like below
$cache = Cache::connect($credential=null);
You can assign any data in the cache.
Maybe like array, string, object, but not function.
$cache->set($key, $value, $seconds=0);
// Example: set timestamp that will expire in 20 seconds
$data = $cache->set('timestamp', time(), 20);
When you have any data in the cache, you can obtain the data with it's key.
$cache->get($key, $defaultValue=null);
// Example: obtain timestamp, or return 0 if the
// cache was not found or expired
$data = Cache->get('timestamp', 0);
The returned value will be either true
or false
.
If the cache was already expired it will return false.
$cache->has($key);
This function is similar to get
.
But after you pulled the data, the cache will immediately removed.
$cache->pull($key, $default=null);
$cache->forget($key);
By using forget, it will force expiration of a key and remove associated data about it.
Flushing cache will remove all available cache.
$cache->flush($key='*');
This feature will help you extend time of a key.
But if the key was already expired this function will return false.
$cache->extendTime($key, $seconds);