Simple and fast cache system implementing PSR-16: interface for caching libraries
With Composer
$ composer require amber/cache
use Amber\Cache\Cache;
$cache = Cache::getInstance();
Alternatively you can set the driver before geting the instance of the cache.
use Amber\Cache\Cache;
$cache = Cache::driver('file');
You can choose from these drivers:
$drivers = [
'file' => 'Amber\Cache\Driver\SimpleCache',
'json' => 'Amber\Cache\Driver\JsonCache',
'array' => 'Amber\Cache\Driver\ArrayCache',
'apcu' => 'Amber\Cache\Driver\ApcuCache',
];
Or you could set the driver class:
$cache = Cache::driver(Amber\Cache\Driver\SimpleCache::class);
Finally you could instantiate the driver by yourself:
$cache = new \Amber\Cache\Driver\SimpleCache();
Fetches a value from the cache.
$cache->get($key, $default = null);
Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
$cache->set($key, $value, $ttl = null);
Delete an item from the cache by its unique key.
$cache->delete($key);
Wipes clean the entire cache's keys.
$cache->clear();
Determines whether an item is present in the cache.
$cache->has($key);
Obtains multiple cache items by their unique keys.
$cache->getMultiple($keys, $default = null);
Persists a set of key => value pairs in the cache, with an optional TTL.
$cache->setMultiple($values, $ttl = null);
Deletes multiple cache items in a single operation.
$cache->deleteMultiple($keys);
You can use all the method from the Cache class statically, like this:
use Amber\Cache\Cache;
Cache::set('key', 'value');
Cache::has('key'); // Returns true
Cache::get('key'); // Returns "value"
Cache::delete('key');
// Set the driver and then call the desired method.
Cache::driver('json')->set('key', 'value');