juneym / zf2-cache-mongo
ZF2 cache storage compatible library using MongoDB's TTL collection
Installs: 1 293
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.3.23
- zendframework/zend-cache: 2.4.*
Requires (Dev)
- phpunit/phpunit: ~3.0
Suggests
- ext-mongo: MongoDB, PHP client for Mongodb
README
ZF2 cache storage compatible library using MongoDB's TTL collection
Overview
It seems that there aren't that many people out there who is interested in creating a ZF2 compatible cache storage backend library for MongoDB, hence this project.
The library utilizes the Time To Live (TTL) collection feature introduced in MongoDB v2.2
Using the Library
-
Update your
composer.json
(if you have)(to be added)
-
Instantiate the library
$options = array( 'dsn' => 'mongodb://127.0.0.1', 'mongoOptions' => array(/* any of the valid \Mongo or \MongoClient options */), 'dbname' => 'cachedb', 'collection' => 'cache', 'ttl' => 10, 'namespace' => 'stl' );
$mongoCache = new \Juneym\Cache\Storage\Adapter\Mongo($options); $cacheKey = md5('This is a sample key');
$created = $mongoCache->setItem($cacheKey, array('x' => 12345, 'y' => 'ABCDEF' . rand(0,10000))); if (!$created) { die("Cached using key: " . $cacheKey . "\n"); }
$data = $mongoCache->getItem($cacheKey); print_r($data); unset($mongoCache);
Cache Item Attributes
There are applications that require additional metadata as part of the cache record in MongoDB such
as "current page addres" or "remote host ip". To do this, use the setCacheItemAttributes(array)
to attach an array attributes
to a cache item. When the item is retrieve with an attribute, the data is accessible via the attr
key. Following is a snippet from the
tests file.
public function testCanAttachCacheItemAttributes() { $mongoCache = new Storage\Adapter\Mongo($this->options); $cacheKey = md5('this is a test key with attributes' . __METHOD__); $itemAttribute = array( 'current_page' => 'http://hello-world.com/about-us.html', 'browser' => 'Firefox' ); $mongoCache->setCacheItemAttributes($itemAttribute); $result = $mongoCache->setItem($cacheKey, array('x' => 11111, 'y' => 'ABCDEF' . rand(0,10000))); $this->assertTrue($result); $data = $mongoCache->getItem($cacheKey); $this->assertArrayHasKey('attr', $data); $this->assertEquals($itemAttribute['current_page'], $data['attr']['current_page']); $this->assertEquals($itemAttribute['browser'], $data['attr']['browser']); $itemAttribute1 = $mongoCache->getCacheItemAttributes(); $this->assertTrue(is_array($itemAttribute1)); $this->assertTrue(empty($itemAttribute1)); unset($mongoCache); }
About TTL Index & Cache Expiry
There are two ways a cached data will expire.
- When the difference between the current time and the cache item's
created
time is more than the cache item'sttl
value (in seconds) - When the record's
created
value is way past the MongoDB's cache collection TTL index (expireAfterSeconds
). Note that MongoDB's garbage collection runs every 60 seconds so don't be surprised if the cached item is still available. MongoDB's garbage collector will eventually remove all qualified records in the background.
Required Index
Assuming that the cache database is called "cachedb" and the collection name is "cache", fhe following indexes are required:
use cachedb
db.cache.ensureIndex({ns:1}, {background:true});
db.cache.ensureIndex({ns:1, key:1}, {background:true});
db.cache.ensureIndex({ns:1, tags:1}, {background:true});
If you are using version below v0.2.0, please use the following TTL index definition:
db.cache.ensureIndex({created:1}, {background:true, expireAfterSeconds: 3600, name: 'colRecordTTl'});
Starting in v0.2.0, the expireAt
field is populated based on the ttl
value at the time the cache entry has been created (or saved). Use the following index to enforce automatic expiration of record based on expireAt
field.
db.cache.ensureIndex({created:1, expireAt:1}, {background: true});
db.cache.ensureIndex({expireAt:1}, {expireAfterSeconds: 0, name: 'cache_expire_at'});