PHP code example of ihor / cachalot

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

    

ihor / cachalot example snippets


// With Cachalot cache you can easily cache results of different types of functions
$cache = new \Cachalot\ArrayCache();

// built-in functions
$length = $cache->getCached('strlen', ['hello world']); 

// user defined functions
$unique = $cache->getCached('uniqueValues', [[1, 2, 3, 1, 2, 3]]);

// static methods
$result = $cache->getCached(['Calculator', 'subtract'], [1, 2]);

// instance methods
$square = $cache->getCached([new Calculator(), 'square'], [5]);

// anonymous functions
$reason = $cache->getCached($getErrorReason, [], \Cachalot\Cache::ONE_DAY, 'error-reason');

// callable objects
$trimed = $cache->getCached(new Trimmer(), [' hello world ']);

$length = $cache->getCached('strlen', ['hello world']);

if ($cache->contains('lastVisit')) {
    echo 'This is not the first visit';
}

if ($lastVisitDate = $cache->get('lastVisit')) {
    echo sprintf('Last visit was at %s', $lastVisitDate);
}

$cache->set('lastVisit', time());

$cache->delete('lastVisit');

$cache->clear();

$cache = new Cachalot\ApcCache();

$cache = new Cachalot\XcacheCache();

$memcache = new \Memcache();
$memcache->connect('unix:///usr/local/var/run/memcached.sock', 0);

$cache = new \Cachalot\MemcacheCache($memcache);

$memcached = new \Memcached();
$memcached->addServer('/usr/local/var/run/memcached.sock', 0);

$cache = new \Cachalot\MemcachedCache($memcached);

$redis = new \Redis();
$redis->connect('127.0.0.1');
$redis->select(1);

$cache = new \Cachalot\RedisCache($redis);

$couchbase = new \Couchbase('127.0.0.1', '', '', 'default');

$cache = new \Cachalot\CouchbaseCache($couchbase);

$cluster = new \CouchbaseCluster('couchbase://localhost');
$bucket = $cluster->openBucket('default');

$cache = new \Cachalot\Couchbase2Cache($bucket);

$cache = new \Cachalot\ArrayCache();

$cache = new \Cachalot\BlackholeCache();