Skip to content
StefansArya edited this page Apr 18, 2019 · 4 revisions

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);

set

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);

get

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);

has

The returned value will be either true or false.
If the cache was already expired it will return false.

$cache->has($key);

pull

This function is similar to get.
But after you pulled the data, the cache will immediately removed.

$cache->pull($key, $default=null);

forget

$cache->forget($key);

By using forget, it will force expiration of a key and remove associated data about it.

flush

Flushing cache will remove all available cache.

$cache->flush($key='*');

extendTime

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);
Clone this wiki locally