dev-master
9999999-dev https://github.com/greg-md/php-cacheA powerful Cache for PHP.
MIT
The Requires
- php ^7.1
The Development Requires
cache php php-cache greg-md greg-php web-artisans
A powerful Cache for PHP.
A powerful Cache for PHP., (*2)
^7.1
There are two ways of working with cache strategies. Directly or via a cache manager., (*3)
A cache manager could have many cache strategies and a default one. The cache manager implements the same cache strategy and could act as default one if it's defined., (*4)
In the next example we will use a cache manager., (*5)
First of all, you have to initialize a cache manager and register some strategies:, (*6)
$manager = new \Greg\Cache\CacheManager(); // Register a file cache $manager->registerStrategy('store1', new \Greg\Cache\FileCache(__DIR__ . '/storage')); // Register a sqlite cache $manager->register('store2', function() { $pdo = new \PDO('sqlite:' . __DIR__ . '/storage/store2.sqlite'); return new \Greg\Cache\SqliteCache($pdo); }); // Register a redis cache $manager->register('store3', function() { $redis = new \Redis(); $redis->connect('127.0.0.1'); return new \Greg\Cache\RedisCache($redis); });
Optionally, you can define a default store to be used by the cache manager., (*7)
$manager->setDefaultStoreName('store2');
Then, you can set or get some data:, (*8)
// Add some data in "store1" $manager->store('store1')->set('foo', 'FOO'); // Add some data in default store, which is "store2" $manager->set('bar', 'BAR'); // Get "bar" value from default store. $value = $manager->get('bar'); // result: BAR
If you want, you can create your own strategies.
They should implement the \Greg\Cache\CacheStrategy
interface., (*9)
Below you can find a list of supported methods., (*10)
key => value
pairs in the cache, with an optional TTL;key => value
pairs in the cache;Determines whether an item is present in the cache., (*11)
has(string $key): bool
$key
- The cache item key., (*12)
Example:, (*13)
$strategy->has('foo');
Determines whether an item is present in the cache., (*14)
hasMultiple(array $keys): bool
$keys
- The cache items keys., (*15)
Example:, (*16)
$strategy->hasMultiple(['foo', 'bar']);
Fetch a value from the cache., (*17)
get(string $key, mixed $default = null): mixed
$key
- The unique key of this item in the cache;
$default
- Default value to return if the key does not exist., (*18)
Return the value of the item from the cache, or $default
in case of cache miss., (*19)
Example:, (*20)
$strategy->get('foo');
Obtains multiple cache items by their unique keys., (*21)
getMultiple(array $keys, $default = null): mixed
$keys
- A list of keys that can obtained in a single operation;
$default
- Default value to return for keys that do not exist., (*22)
Return a list of key => value
pairs. Cache keys that do not exist or are stale will have $default
as value., (*23)
Example:, (*24)
$strategy->get('foo');
Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time., (*25)
set(string $key, $value, ?int $ttl = null): $this
$key
- The key of the item to store;
$value
- The value of the item to store, must be serializable;
$ttl
- Optional. The TTL value of this item. If no value is sent and
the driver supports TTL then the library may set a default value
for it or let the driver take care of that., (*26)
Example:, (*27)
$strategy->set('foo', 'FOO');
Persists a set of key => value
pairs in the cache, with an optional TTL., (*28)
setMultiple(array $values, ?int $ttl = null): $this
$values
- A list of key => value
pairs for a multiple-set operation;
$ttl
- Optional. The TTL value of this item. If no value is sent and
the driver supports TTL then the library may set a default value
for it or let the driver take care of that., (*29)
Example:, (*30)
$strategy->setMultiple(['foo' => 'FOO', 'bar' => 'BAR']);
Persists forever data in the cache, uniquely referenced by a key., (*31)
forever(string $key, $value): $this
$key
- The key of the item to store;
$value
- The value of the item to store, must be serializable., (*32)
Example:, (*33)
$strategy->forever('foo', 'FOO'); // or $strategy->set('foo', 'FOO', 0);
Persists forever a set of key => value
pairs in the cache., (*34)
foreverMultiple(array $values): $this
$values
- A list of key => value
pairs for a multiple-set operation., (*35)
Example:, (*36)
$strategy->foreverMultiple(['foo' => 'FOO', 'bar' => 'BAR']); // or $strategy->setMultiple(['foo' => 'FOO', 'bar' => 'BAR'], 0);
Delete an item from the cache by its unique key., (*37)
delete(string $key): $this
$key
- The unique cache key of the item to delete., (*38)
Example:, (*39)
$strategy->delete('foo');
Delete multiple items from the cache by their unique keys., (*40)
deleteMultiple(array $keys): $this
$keys
- The unique cache keys of the items to delete., (*41)
Example:, (*42)
$strategy->deleteMultiple(['foo', 'bar']);
Wipes clean the entire cache's keys., (*43)
clear(): $this
Example:, (*44)
$strategy->clear();
Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist., (*45)
remember(string $key, callable($this): mixed $callable, ?int $ttl = null): mixed
$key
- The unique key of this item in the cache;
$callable
- The value callable of the item to store when the key is not present in the cache. The value must be serializable;
$ttl
- Optional. The TTL value of this item. If no value is sent and
the driver supports TTL then the library may set a default value
for it or let the driver take care of that., (*46)
Example:, (*47)
$strategy->remember('foo', function() { return 'FOO'; });
Increment a value., (*48)
increment(string $key, int $amount = 1, ?int $ttl = null): $this
$key
- The unique key of this item in the cache;
$abount
- The amount to increment;
$ttl
- Optional. The TTL value of this item. If no value is sent and
the driver supports TTL then the library may set a default value
for it or let the driver take care of that., (*49)
Example:, (*50)
$strategy->increment('foo'); $strategy->increment('foo', 10);
Decrement a value., (*51)
decrement(string $key, int $amount = 1, ?int $ttl = null): $this
$key
- The unique key of this item in the cache;
$abount
- The amount to decrement;
$ttl
- Optional. The TTL value of this item. If no value is sent and
the driver supports TTL then the library may set a default value
for it or let the driver take care of that., (*52)
Example:, (*53)
$strategy->decrement('foo'); $strategy->decrement('foo', 10);
Increment a float value., (*54)
incrementFloat(string $key, float $amount = 1.0, ?int $ttl = null): $this
$key
- The unique key of this item in the cache;
$abount
- The amount to increment;
$ttl
- Optional. The TTL value of this item. If no value is sent and
the driver supports TTL then the library may set a default value
for it or let the driver take care of that., (*55)
Example:, (*56)
$strategy->incrementFloat('foo'); $strategy->incrementFloat('foo', 1.5);
Decrement a float value., (*57)
decrementFloat(string $key, float $amount = 1.0, ?int $ttl = null): $this
$key
- The unique key of this item in the cache;
$abount
- The amount to decrement;
$ttl
- Optional. The TTL value of this item. If no value is sent and
the driver supports TTL then the library may set a default value
for it or let the driver take care of that., (*58)
Example:, (*59)
$strategy->decrementFloat('foo'); $strategy->decrementFloat('foo', 1.5);
Set a new expiration on an item., (*60)
touch(string $key, ?int $ttl = null): $this
$key
- The unique key of this item in the cache;
$ttl
- Optional. The TTL value of this item. If no value is sent and
the driver supports TTL then the library may set a default value
for it or let the driver take care of that., (*61)
Example:, (*62)
$strategy->touch('foo', 100);
Retrieve and delete an item from the cache., (*63)
pull(string $key, $default = null): mixed
$key
- The unique key of this item in the cache;
$default
- Default value to return for keys that do not exist., (*64)
Return the value of the item from the cache, or $default
in case of cache miss., (*65)
Example:, (*66)
$strategy->pull('foo'); // return foo value $strategy->pull('foo'); // return null
Persists data in the cache if it's not present., (*67)
add(string $key, $value, ?int $ttl = null): $this
$key
- The key of the item to store;
$value
- The value of the item to store, must be serializable;
$ttl
- Optional. The TTL value of this item. If no value is sent and
the driver supports TTL then the library may set a default value
for it or let the driver take care of that., (*68)
Return true
if the item is actually added to the cache. Otherwise, return false
., (*69)
Example:, (*70)
$strategy->add('foo', 'FOO'); // return true $strategy->add('foo', 'FOO2'); // return false
MIT © Grigorii Duca, (*71)
, (*72)
A powerful Cache for PHP.
MIT
cache php php-cache greg-md greg-php web-artisans