PHP code example of bdk / simplecache

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

    

bdk / simplecache example snippets


// create \Memcached object pointing to your Memcached server
$client = new \Memcached();
$client->addServer('localhost', 11211);
// create KeyValueStore object
$cache = new \bdk\SimpleCache\Adapters\Memcached($client);

// create stampede protector layer over our real cache
$cache = new \bdk\SimpleCache\Scale\StampedeProtector($cache);

// create Pool (psr/cache) object from cache engine
$pool = new \bdk\SimpleCache\Psr6\Pool($cache);

// get item from Pool
$item = $pool->getItem('key');

// get item value
$value = $item->get();

// ... or change the value & store it to cache
$item->set('updated-value');
$pool->save($item);

// create \Memcached object pointing to your Memcached server
$client = new \Memcached();
$client->addServer('localhost', 11211);
// create KeyValueStore object
$cache = new \bdk\SimpleCache\Adapters\Memcached($client);

// create \Redis object pointing to your Redis server
$client = new \Redis();
$client->connect('127.0.0.1');
// create KeyValueStore object
$cache = new \bdk\SimpleCache\Adapters\Redis($client);

// create \CouchbaseBucket object pointing to your Couchbase server
$cluster = new \CouchbaseCluster('couchbase://localhost');
$bucket = $cluster->openBucket('default');
// create KeyValueStore object
$cache = new \bdk\SimpleCache\Adapters\Couchbase($bucket);

// create KeyValueStore object
$cache = new \bdk\SimpleCache\Adapters\Apc();

// create mysqli object
$client = new \mysqli('mysql', 'root', '', 'cache');
// create KeyValueStore object
$cache = new \bdk\SimpleCache\Adapters\MySQLi($client);

// create \PDO object pointing to your MySQL server
$client = new PDO('mysql:dbname=cache;host=127.0.0.1', 'root', '');
// create KeyValueStore object
$cache = new \bdk\SimpleCache\Adapters\PdoMySQL($client);

// create \PDO object pointing to your PostgreSQL server
$client = new PDO('pgsql:user=postgres dbname=cache password=');
// create KeyValueStore object
$cache = new \bdk\SimpleCache\Adapters\PdoPgSQL($client);

// create \PDO object pointing to your SQLite server
$client = new PDO('sqlite:cache.db');
// create KeyValueStore object
$cache = new \bdk\SimpleCache\Adapters\PdoSQLite($client);

// create KeyValueStore object
$cache = new \League\Flysystem\Filesystem('/path/to/cache');

// create Flysystem object
$adapter = new \League\Flysystem\Adapter\Local('/path/to/cache', LOCK_EX);
$filesystem = new \League\Flysystem\Filesystem($adapter);
// create KeyValueStore object
$cache = new \bdk\SimpleCache\Adapters\Flysystem($filesystem);

// create KeyValueStore object
$cache = new \bdk\SimpleCache\Adapters\Memory();

// create buffered cache layer over our real cache
$cache = new \bdk\SimpleCache\Buffered\Buffered($cache);

// create transactional cache layer over our real cache
$cache = new \bdk\SimpleCache\Buffered\Transactional($keyValueStore);

// begin a transaction
$cache->begin();

// set a value
// it won't be stored in real cache until commit() is called
$cache->set('key', 'value'); // returns true

// get a value
// it won't get it from the real cache (where it is not yet set), it'll be read
// from PHP memory
$cache->get('key'); // returns 'value'

// now commit write operations, this will effectively propagate the update to
// 'key' to the real cache
$cache->commit();

// ... or rollback, to discard uncommitted changes!
$cache->rollback();

// create stampede protector layer over our real cache
$cache = new \bdk\SimpleCache\Scale\StampedeProtector($cache);

// boilerplate code example with Redis, but any
// bdk\SimpleCache\KeyValueStoreInterface implementation will work
$client = new \Redis();
$client->connect('192.168.1.100');
$cache1 = new \bdk\SimpleCache\Adapters\Redis($client);

// a second Redis server...
$client2 = new \Redis();
$client2->connect('192.168.1.101');
$cache2 = new \bdk\SimpleCache\Adapters\Redis($client);

// create shard layer over our real caches
// now $cache will automatically distribute the data across both servers
$cache = new \bdk\SimpleCache\Scale\Shard($cache1, $cache2);

// create Pool object from KeyValueStoreInterface object
$pool = new \bdk\SimpleCache\Psr6\Pool($cache);

// get item from Pool
$item = $pool->getItem('key');

// get item value
$value = $item->get();

// ... or change the value & store it to cache
$item->set('updated-value');
$pool->save($item);

// create Simplecache object from KeyValueStoreInterface object
$simplecache = new \bdk\SimpleCache\Psr16\SimpleCache($cache);

// get value from cache
$value = $simplecache->get('key');

// ... or store a new value to cache
$simplecache->set('key', 'updated-value');

// let's create a Memcached cache object
$client = new \Memcached();
$client->addServer('localhost', 11211);
$cache = new \bdk\SimpleCache\Adapters\Memcached($client);

$articleCache = $cache->collection('articles');
$sessionCache = $cache->collection('sessions');

// all of these are different keys
$cache->set('key', 'value one');
$articleCache->set('key', 'value two');
$sessionCache->set('key', 'value three');

// this clears our the entire 'articles' subset (thus removing 'value two'),
// while leaving everything else untouched
$articleCache->clear();

// this removes everything from the server, including all of its subsets
// ('value one' and 'value three' will also be deleted)
$cache->clear();

$articleCache = $cache->collection('articles');
$sessionCache = $cache->collection('sessions');

// create Pool objects from both KeyValueStore collections
$articlePool = new \bdk\SimpleCache\Psr6\Pool($articleCache);
$sessionPool = new \bdk\SimpleCache\Psr6\Pool($sessionCache);