PHP code example of soupmix / cache

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

    

soupmix / cache example snippets




$rConfig = ['host'=> "127.0.0.1"];
$handler = new Redis();
$handler->connect(
    $rConfig['host']
);

$cache = new Soupmix\Cache\RedisCache($handler);



$config = [
    'bucket' => 'test',
    'hosts'   => ['127.0.0.1'],
;
$handler = new Memcached($config['bucket']);
$handler->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
$handler->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
if (!count($handler->getServerList())) {
    $hosts = [];
    foreach ($config['hosts'] as $host) {
        $hosts[] = [$host, 11211];
    }
    $handler->addServers($hosts);
}

$cache = new Soupmix\Cache\MemcachedCache($handler);



$cache = new Soupmix\Cache\APCUCache();



$cache = new Soupmix\Cache\ArrayCache();

$cache->set($key, $value, $ttl);

$cache->set('my_key, 'my_value', TTL_DAY);

// returns bool(true)

$cache->has($key);

$cache->has('my_key');

// returns bool(true)

$cache->get($key, default=null);

$cache->get('my_key');

// returns  string(8) "my_value"

$cache->delete($key);

$cache->delete('my_key');

// returns bool(true)

$cache->setMultiple(array $items);

$items = ['my_key_1'=>'my_value_1', 'my_key_2'=>'my_value_2'];
$cache->setMultiple($items);

// returns bool(true)

$cache->getMultiple($keys, $default=null);

$keys = ['my_key_1', 'my_key_2'];
$cache->getMultiple($keys);
/*
returns array(2) {
          ["my_key_1"]=>
          string(3) "my_value_1"
          ["my_key_2"]=>
          string(3) "my_value_2"
        }
*/

$cache->deleteMultiple($keys);

$keys = ['my_key_1', 'my_key_2'];
$cache->deleteMultiple($keys);
 /*
 returns bool(true)
 */

$cache->clear();
// returns bool(true)