PHP code example of kemist / cache

1. Go to this page and download the library: Download kemist/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/ */

    

kemist / cache example snippets




mist\Cache\Cache as Cache;

$storage=new Kemist\Cache\Storage\FileStorage();
$cache=new Cache($storage);


// Stores a variable in cache
$cache->store('variable','test variable');

// Reads back a variable from cache with existence checking
if ($cache->has('variable')){
  echo $cache->get('variable');
}

// Deletes a variable from cache
$cache->delete('variable');

// Reads back a variable passing a default value in case it doesn't exist
echo $cache->get('variable','default value');

// Reads back a variable and deletes it from cache
$var=$cache->pull('variable');

// Deletes all cached variables
$cache->flush();


// Stores a variable in cache compressed (in case current storage adapter supports it)
$cache->store('test.compressed','test variable',true);

// By reading back you don't need to know whether variable was compressed or not
echo $cache->get('test.compressed');


// Stores a variable until manual deletion (default working)
$cache->store('test.manual','test variable',false,0);

// Stores a variable valid for 5 minutes (specified in seconds)
$cache->store('test.five_minutes','test variable',false,300);

// Stores a variable valid until specified date
$cache->store('test.concrete_date','test variable',false,'2015-01-01');

// Stores a variable valid for 2 weeks (you can use any valid date string)
$cache->store('test.two_weeks','test variable',false,'2weeks');

// Gets a cached item TTL (time to live: the difference between expiration and creation time in seconds)
echo $cache->getTTL('test.five_minutes');

// You can set TTL without modifying cached value
echo $cache->setTTL('test.manual',300);

// You can do the same with specifying the expiry date
echo $cache->setExpiry('test.manual','5 minutes');


$object=new stdClass();
$object->property=5;
// By default every variable is stored serialized so even objects can be cached
$cache->store('test.object',$object,false,0,Cache::STORE_METHOD_SERIALIZE);
var_dump($cache->get('test.object'));

$array=array('a'=>'apple','b'=>'bike');
// You can switch storing method to JSON for arrays if you prefer
$cache->store('test.array',$array,false,0,Cache::STORE_METHOD_JSON);
// By reading back you don't need to know what was the storing method of the variable
var_dump($cache->get('test.array'));


// Get can return a default value if cached variable doesn't exist
echo $cache->get('test.default','default');

// You can pass even a closure
echo $cache->get('test.default',function(){return 'default';});

// Initial value setting (store default value if cached variable doesn't exist)
echo $cache->getOrStore('test.initial','initial',false,'3hours');

// The same with closure
echo $cache->getOrStore('test.initial',function(){return 'initial';},false,'3hours');


// Sets cache value and assigns tags to it
$cache->storeTagged('test.tagged1','tagged',array('tag1','tag2'));
$cache->storeTagged('test.tagged2','tagged2','tag2');

// Retrieves cache values having the given tags
var_dump($cache->getTagged('tag2'));

// Adds more tags to the specified cache value
$cache->addTags('test.tagged2','tag3');

// Change tags without modifying cache value
$cache->setTags('test.tagged1',array('tag2','tag3','tag4'));

// Retrieves tags of a cached value
$cache->getTags('test.tagged1');

// Deletes cache variables having specified tags
var_dump($cache->deleteTagged('tag2'));


// Displays cache creation time (first stored) in specified date format 
// (by default it returns a unix timestamp)
echo 'Created:'.$cache->getCreated('test.compressed','Y-m-d H:i:s');

// Displays cache expiry in specified date format
echo 'Expiry:'.$cache->getExpiry('test.two_weeks','Y-m-d H:i:s');

// Displays time when cached variable was last accessed (either read or write)
echo 'Last access:'.$cache->getLastAccess('test.array','Y-m-d H:i:s');

// Displays time when cached variable was last read
echo 'Last read:'.$cache->getLastRead('test.object','Y-m-d H:i:s');

// Displays time when cached variable was last written 
echo 'Last write:'.$cache->getLastWrite('test.array','Y-m-d H:i:s');

// Displays how many times cached value was written since creation
echo 'Write count:'.$cache->getWriteCount('variable');

// Displays how many times cached value was read since creation
echo 'Read count:'.$cache->getReadCount('test.compressed');

// Displays cache hits
echo 'Cache hits:'.$cache->getHits();

// Displays cache misses
echo 'Cache misses:'.$cache->getMisses();

// You can retrieve all of the above statistics at once
var_dump($cache->getInfo('test.object'));

// Displays all cached variable names
var_dump($cache->getKeys());

// Displays all cached variable names that were read in current runtime session
var_dump($cache->getReadKeys());

// Retrieves all tag values currently in use
var_dump($cache->getAllTags());