项目作者: greg-md

项目描述 :
A powerful Cache for PHP.
高级语言: PHP
项目地址: git://github.com/greg-md/php-cache.git
创建时间: 2016-10-04T20:42:50Z
项目社区:https://github.com/greg-md/php-cache

开源协议:MIT License

下载


Greg PHP Cache

StyleCI
Build Status
Total Downloads
Latest Stable Version
Latest Unstable Version
License

A powerful Cache for PHP.

Table of Contents:

Requirements

  • PHP Version ^7.1

Supported Drivers

  • Array - Use the PHP array as a storage;
  • Tmp - Use temporary files as a storage. They are automatically removed when script ends;
  • File - Use file based storage;
  • Memcached - Use Memcached driver as a storage;
  • Redis - Use Redis driver as a storage;
  • Sqlite - Use Sqlite database as a storage.

How It Works

There are two ways of working with cache strategies.
Directly or via a cache manager.

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.

In the next example we will use a cache manager.

First of all, you have to initialize a cache manager and register some strategies:

  1. $manager = new \Greg\Cache\CacheManager();
  2. // Register a file cache
  3. $manager->registerStrategy('store1', new \Greg\Cache\FileCache(__DIR__ . '/storage'));
  4. // Register a sqlite cache
  5. $manager->register('store2', function() {
  6. $pdo = new \PDO('sqlite:' . __DIR__ . '/storage/store2.sqlite');
  7. return new \Greg\Cache\SqliteCache($pdo);
  8. });
  9. // Register a redis cache
  10. $manager->register('store3', function() {
  11. $redis = new \Redis();
  12. $redis->connect('127.0.0.1');
  13. return new \Greg\Cache\RedisCache($redis);
  14. });

Optionally, you can define a default store to be used by the cache manager.

  1. $manager->setDefaultStoreName('store2');

Then, you can set or get some data:

  1. // Add some data in "store1"
  2. $manager->store('store1')->set('foo', 'FOO');
  3. // Add some data in default store, which is "store2"
  4. $manager->set('bar', 'BAR');
  5. // Get "bar" value from default store.
  6. $value = $manager->get('bar'); // result: BAR

Cache Strategy

If you want, you can create your own strategies.
They should implement the \Greg\Cache\CacheStrategy interface.

Below you can find a list of supported methods.

  • has - Determines whether an item is present in the cache;
  • hasMultiple - Determines whether multiple items are present in the cache;
  • get - Fetch a value from the cache;
  • getMultiple - Obtains multiple cache items by their unique keys;
  • set - Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time;
  • setMultiple - Persists a set of key => value pairs in the cache, with an optional TTL;
  • forever - Persists forever data in the cache, uniquely referenced by a key;
  • foreverMultiple - Persists forever a set of key => value pairs in the cache;
  • delete - Delete an item from the cache by its unique key;
  • deleteMultiple - Delete multiple items from the cache by their unique keys;
  • clear - Clear the storage;
  • remember - Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn’t exist;
  • increment - Increment a value;
  • decrement - Decrement a value;
  • incrementFloat - Increment a float value;
  • decrementFloat - Decrement a float value;
  • touch - Set a new expiration on an item;
  • pull - Retrieve and delete an item from the cache;
  • add - Persists data in the cache if it’s not present.

has

Determines whether an item is present in the cache.

  1. has(string $key): bool

$key - The cache item key.

Example:

  1. $strategy->has('foo');

hasMultiple

Determines whether an item is present in the cache.

  1. hasMultiple(array $keys): bool

$keys - The cache items keys.

Example:

  1. $strategy->hasMultiple(['foo', 'bar']);

get

Fetch a value from the cache.

  1. 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.

Return the value of the item from the cache, or $default in case of cache miss.

Example:

  1. $strategy->get('foo');

getMultiple

Obtains multiple cache items by their unique keys.

  1. 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.

Return a list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.

Example:

  1. $strategy->get('foo');

set

Persists data in the cache,
uniquely referenced by a key with an optional expiration TTL time.

  1. 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.

Example:

  1. $strategy->set('foo', 'FOO');

setMultiple

Persists a set of key => value pairs in the cache, with an optional TTL.

  1. 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.

Example:

  1. $strategy->setMultiple(['foo' => 'FOO', 'bar' => 'BAR']);

forever

Persists forever data in the cache, uniquely referenced by a key.

  1. forever(string $key, $value): $this

$key - The key of the item to store;
$value - The value of the item to store, must be serializable.

Example:

  1. $strategy->forever('foo', 'FOO');
  2. // or
  3. $strategy->set('foo', 'FOO', 0);

foreverMultiple

Persists forever a set of key => value pairs in the cache.

  1. foreverMultiple(array $values): $this

$values - A list of key => value pairs for a multiple-set operation.

Example:

  1. $strategy->foreverMultiple(['foo' => 'FOO', 'bar' => 'BAR']);
  2. // or
  3. $strategy->setMultiple(['foo' => 'FOO', 'bar' => 'BAR'], 0);

delete

Delete an item from the cache by its unique key.

  1. delete(string $key): $this

$key - The unique cache key of the item to delete.

Example:

  1. $strategy->delete('foo');

deleteMultiple

Delete multiple items from the cache by their unique keys.

  1. deleteMultiple(array $keys): $this

$keys - The unique cache keys of the items to delete.

Example:

  1. $strategy->deleteMultiple(['foo', 'bar']);

clear

Wipes clean the entire cache’s keys.

  1. clear(): $this

Example:

  1. $strategy->clear();

remember

Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn’t exist.

  1. 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.

Example:

  1. $strategy->remember('foo', function() {
  2. return 'FOO';
  3. });

increment

Increment a value.

  1. 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.

Example:

  1. $strategy->increment('foo');
  2. $strategy->increment('foo', 10);

decrement

Decrement a value.

  1. 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.

Example:

  1. $strategy->decrement('foo');
  2. $strategy->decrement('foo', 10);

incrementFloat

Increment a float value.

  1. 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.

Example:

  1. $strategy->incrementFloat('foo');
  2. $strategy->incrementFloat('foo', 1.5);

decrementFloat

Decrement a float value.

  1. 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.

Example:

  1. $strategy->decrementFloat('foo');
  2. $strategy->decrementFloat('foo', 1.5);

touch

Set a new expiration on an item.

  1. 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.

Example:

  1. $strategy->touch('foo', 100);

pull

Retrieve and delete an item from the cache.

  1. 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.

Return the value of the item from the cache, or $default in case of cache miss.

Example:

  1. $strategy->pull('foo'); // return foo value
  2. $strategy->pull('foo'); // return null

add

Persists data in the cache if it’s not present.

  1. 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.

Return true if the item is actually added to the cache. Otherwise, return false.

Example:

  1. $strategy->add('foo', 'FOO'); // return true
  2. $strategy->add('foo', 'FOO2'); // return false

License

MIT © Grigorii Duca

Huuuge Quote

I fear not the man who has practiced 10,000 programming languages once, but I fear the man who has practiced one programming language 10,000 times. © #horrorsquad