What storages can be used:
All storage objects have one interface, so you can switch them without changing the working code.
- One interface for all storages - you can change storage without changing your code
- Tagging cache (approach versioning and grouping)
- Locking - "race condition" ("dog-pile" or "cache miss storm") effects are excluded
- Serializer for value (json or PHP-serializer)
- Automatic unserialization
- Standalone module/component for Rock Framework
- Installation
- Quick Start
- Documentation
- Demo
- Requirements
- Storages comparison
- Differences between the approaches a tagging
From the Command Line:
composer require romeoz/rock-cache
or in your composer.json:
{
"require": {
"romeoz/rock-cache": "*"
}
}
####Memcached
$config = [
'hashKey' => CacheInterface::HASH_MD5, // Default: HASH_MD5
'serializer' => CacheInterface::SERIALIZE_JSON // Default: SERIALIZE_PHP - php serializator
];
$memcached = new \rock\cache\Memcached($config); // or \rock\cache\versioning\Memcached for approach versioning
$tags = ['tag_1', 'tag_2'];
$value = ['foo', 'bar'];
$expire = 0; // If use expire "0", then time to live infinitely
$memcached->set('key_1', $value, $expire, $tags);
// automatic unserialization
$memcached->get('key_1'); // result: ['foo', 'bar'];
$memcached->flush(); // Invalidate all items in the cache
####MongoDB
$connection = new \rock\mongodb\Connection;
$collection = $connection->getCollection('cache')
$collection->createIndex('id', ['unique' => true]);
$collection->createIndex('expire', ['expireAfterSeconds' => 0]); // create TTL index
$config = [
'storage' => $connection,
'cacheCollection' => 'cache'
];
$mongoCache = new \rock\cache\MongoCache($config);
...
####Locking key
Race conditions can occur in multi-threaded mode. To avoid the effect, you need to install a lock on the key.
$memcached = new \rock\cache\Memcached
$value = $memcached->get('key_1');
if ($value !== false) {
return $value;
}
if ($memcached->lock('key_1')) {
// the query to DBMS or other...
$memcached->set('key_1', 'foo');
$memcached->unlock('key_1');
}
####get($key) Returns value by key.
####getMulti(array $keys) Returns multiple values by keys.
####set($key, mixed $value, $expire = 0, array $tags = null) Sets a value to cache.
####setMulti($key, mixed $value, $expire = 0, array $tags = null) Sets a multiple key-values to cache.
####add($key, mixed $value, $expire = 0, array $tags = null) Adds a value to cache.
Return false, if already exists on the server.
####exists($key) Checks existence key.
####touch($key, $expire = 0) Changes expire (TTL) for key.
####touchMulti(array $keys, $expire = 0) Changes expire (TTL) for multiple keys .
####increment($key, $offset = 1, $expire = 0, $create = true) Increment a value to cache.
####decrement($key, $offset = 1, $expire = 0, $create = true) Decrement a value to cache.
####remove($key) Removes value from cache.
####removeMulti(array $keys) Removes multiple values from cache.
####getTag($tag) Returns a keys in accordance with tag.
####getMultiTags(array $tags) Returns a keys in accordance with multiple tags.
####existsTag($tag) Checks existence tag.
####removeTag($tag) Removes a tag.
####removeMultiTag(array $tags) Removes a multiple tags.
####getAllKeys() Returns all keys.
Supported:
Memcached
,Redis
,APC
.
####getAll() Returns all values.
Supported:
Memcached
,APC
.
####lock($key) Sets a lock on the key.
####unlock($key) Unlocking key.
####flush() Removes all values from cache.
####status() Returns a status server.
Supported:
Memcached
,Memcache
,Redis
,APC
,Couchbase
.
####getStorage() Returns a native instance cache-storage.
- Install Docker or askubuntu
docker run --name demo -d -p 8080:80 romeoz/docker-rock-cache
- Open demo http://localhost:8080/
You can use each storage separately, requirements are individually for storages.
- PHP 5.4+ and 7.0+
- Redis 2.8/3.0/3.2/4.0. Should be installed
apt-get install redis-server
ordocker run --name redis -d -p 6379:6379 romeoz/docker-redis:4.0
(recommended). Also should be installed PHP extensionapt-get install php-redis
- Memcached. Should be installed
apt-get install memcached
ordocker run --name memcached -d -p 11211:11211 romeoz/docker-memcached
(recommended). Also should be installed php-extension Memcacheapt-get install php-memcache
or Memcachedapt-get install php-memcached
.
php-extension Memcached require dependencies
- APCu. Should be installed
apt-get install php-apcu
.
For PHP 5.6 and lower allowed pecl-extensions APCu 4.0 and lower. Example installation.
- Couchbase DB 4.x.x. PHP 5.6 or higher, php-extension couchbase 2.3.x-2.4.x. Step-by-step installation or
docker run --name couchbase -d couchbase/server:community-4.1.1
(Example installation). - MongoDB 2.6-3.0. For using MongoDB as storage required Rock MongoDB:
composer require romeoz/rock-mongodb
All unbolded dependencies is optional
There is a ready docker-container: docker run --name phpfpm_full -d romeoz/docker-phpfpm:5.6-full
.
But, it may be redundant for you because of the many of installed pecl-extensions
Redis is the best key-value storage for cache. Use Couchbase if you need fault-tolerant and very easy scalable cluster and if you can afford it (recommended hardware requirements). Also, data in Redis and Couchbase storages will be restored even after server reboot.
###Grouping tags
Fastest method, but there is a possibility of overflow cache.
Set a value:
$cache = new \rock\cache\Memcached;
$cache->set('key_1', 'text_1', 0, ['tag_1', 'tag_2']);
$cache->set('key_2', 'text_2', 0, ['tag_1']);
View in memory:
key_1: text_1
key_2: text_2
tag_1: [key_1, key_2]
tag_2: [key_1]
Removing tag:
$cache->removeTag('tag_2');
View in memory:
key_2: text_2
tag_1: [key_1, key_2]
###Versioning tags
Is the best practice, but slower than the approach with the grouping tags, because when getting the cache containing tags, sent multiple requests to compare versions. There is no cache overflows.
References: nablas by D.Koterov (RUS) or "Reset group caches and tagging" by A.Smirnov (RUS).
Set a value:
$cache = new \rock\cache\versioning\Memcached;
$cache->set('key_1', 'text_1', 0, ['tag_1', 'tag_2']);
$cache->set('key_2', 'text_2', 0, ['tag_1']);
View in memory:
key_1: [
value : text_1,
tags : [
tag_1 : 0.20782200 1403858079,
tag_2 : 0.20782200 1403858079
]
]
// tag : microtime
key_2: [
value : text_2,
tags : [
tag_1 : 0.20782200 1403858079,
]
]
tag_1: 0.20782200 1403858079
tag_2: 0.20782200 1403858079
Removing tag:
$cache->removeTag('tag_2');
View in memory:
key_1: [
value : text_1,
tags : [
tag_1 : 0.20782200 1403858079,
tag_2 : 0.20782200 1403858079
]
]
key_2: [
value : text_2,
tags : [
tag_1 : 0.20782200 1403858079,
]
]
tag_1: 0.20782200 1403858079
tag_2: 0.29252400 1403858537
Returns value:
$cache->get('key_1');
// result: false
View in memory:
key_2: [
value : text_2,
tags : [
tag_1 : 0.20782200 1403858079,
]
]
tag_1: 0.20782200 1403858079
The Rock Cache library is open-sourced software licensed under the MIT license