PHP code example of romeoz / rock-cache

1. Go to this page and download the library: Download romeoz/rock-cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

romeoz / rock-cache example snippets


$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

$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);

...

$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');
}

$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']);


$cache->removeTag('tag_2');

$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']);


$cache->removeTag('tag_2');


$cache->get('key_1');
// result: false