, (*1)
Amber/Cache
Simple and fast cache system implementing PSR-16: interface for caching libraries, (*2)
Getting started
Installation
With Composer, (*3)
$ composer require amber/cache
API Usage
use Amber\Cache\Cache;
$cache = Cache::getInstance();
Drivers
Alternatively you can set the driver before geting the instance of the cache., (*4)
use Amber\Cache\Cache;
$cache = Cache::driver('file');
You can choose from these drivers:, (*5)
$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:, (*6)
$cache = Cache::driver(Amber\Cache\Driver\SimpleCache::class);
Finally you could instantiate the driver by yourself:, (*7)
$cache = new \Amber\Cache\Driver\SimpleCache();
get()
Fetches a value from the cache., (*8)
$cache->get($key, $default = null);
set()
Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time., (*9)
$cache->set($key, $value, $ttl = null);
delete()
Delete an item from the cache by its unique key., (*10)
$cache->delete($key);
clear()
Wipes clean the entire cache's keys., (*11)
$cache->clear();
has()
Determines whether an item is present in the cache., (*12)
$cache->has($key);
Multiple actions
getMultiple()
Obtains multiple cache items by their unique keys., (*13)
$cache->getMultiple($keys, $default = null);
setMultiple()
Persists a set of key => value pairs in the cache, with an optional TTL., (*14)
$cache->setMultiple($values, $ttl = null);
deleteMultiple()
Deletes multiple cache items in a single operation., (*15)
$cache->deleteMultiple($keys);
Static Usage
You can use all the method from the Cache class statically, like this:, (*16)
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');