PHP code example of ryanj93 / php-tiny-cacher

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

    

ryanj93 / php-tiny-cacher example snippets


$cache = new PHPTinyCacher();
//Set an optional namespace for entries.
$cache->setNamespace('your namespace here');
//Set the default TTL (in seconds), by default entries have no expire.
$cache->setDefaultTTL(120);

//Store data within the class instance.
$cache->setStrategy(PHPTinyCacher::STRATEGY_LOCAL);
//Store data within the script but shared across class instances.
$cache->setStrategy(PHPTinyCacher::STRATEGY_SHARED);
//Or you can also save data within the session.
$cache->setStrategy(PHPTinyCacher::STRATEGY_SESSION);

//address, port, DB index
$cache->setStrategy(PHPTinyCacher::STRATEGY_REDIS)->connectToRedis('127.0.0.1', 6379, 0);

//A sequential array that contains the information about each node as array.
$cache->setStrategy(PHPTinyCacher::STRATEGY_MEMCACHED)->connectToMemcached(array(
	array('127.0.0.1', 11211)
));

$cache->setStrategy(PHPTinyCacher::STRATEGY_SQLITE)->connectToSQLite('path/to/sqlite.db');

$cache->setStrategy(PHPTinyCacher::STRATEGY_FILE)->setStorageDirectory('path/to/storage/directory');

//Save one element.
//key, value, overwrite, ttl
$cache->push('key', 'value', true, 60);
//Save multiple elements.
//elements, overwrite, ttl
$cache->pushMulti(array(
	'someKey' => 'Some value',
	'key' => 'value'
), true, 60);

//key, quiet (return null instead of throwing an exception).
//Element will contain the value of the element or null if no such value were found and quiet mode has been enabled.
$element = $cache->pull('key', true);
//You can pull multiple elements by using this method and passing an array of keys.
//array of keys, quiet.
//The elements are returned as associative array having as key the entry key and as value the corresponding value or null if no such value were found and quiet mode has been enabled.
$elements = $cache->pullMulti(array('key', 'some other key'), true);

//Result is a boolean value.
$result = $cache->has('key');
//You can check for multiple keys as well.
//Results is an associative array having as key the element key and as value a boolean variable.
$results = $cache->hasMulti(array('key', 'some other key'));
//And you can check if all the given keys exist.
//Result is a boolean value.
$result = $cache->hasAll(array('key', 'some other key'));

$cache->remove('key');
//You can remove multiple elements with a single call as following:
$cache->removeMulti(array('key', 'some other key'));

//Pass the element key and the increment value, it can be a negative value as well, by default 1 is used.
$cache->increment('key', 3);
//Of course you can apply increment on multiple elements.
$cache->incrementMulti(array('key', 'some other key'), 3);
//And decrement values, note that these methods internally call the methods "increment" or "incrementMulti" using a negative increment value.
$cache->decrement('key', 3);

//If you pass "true" as parameter it will remove all elements created by this class, no matter the namespace.
$cache->invalidate();

//If you pass "true" as parameters, it will close all connections, no matter the storage option in use.
$cache->closeConnections();

//Remove expired elements saved in shared storage.
PHPTinyCacher::runGlobalGarbageCollector();
//Remove expired elements saved in session.
PHPTinyCacher::runSessionGarbageCollector();
//Remove expired elements saved in local storage.
$cache->runGarbageCollector();
//Remove expired elements saved in a SQLite3 database.
$cache->runSQLite3GarbageCollector();
`bash
composer