PHP code example of rundiz / simple-cache

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

    

rundiz / simple-cache example snippets


// For Memcache driver
$memcache = new \Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$SimpleCache = new \Rundiz\SimpleCache\Drivers\Memcached($memcache);
unset($memcache);

// For Memcached driver
$memcached = new \Memcached;
$memcached->addServer('localhost', 11211) or die ("Could not connect");
$SimpleCache = new \Rundiz\SimpleCache\Drivers\Memcached($memcached);
unset($memcached);

// For APC (deprecated, use APCu instead).
$SimpleCache = new \Rundiz\SimpleCache\Drivers\Apc();

// For APCu
$SimpleCache = new \Rundiz\SimpleCache\Drivers\Apcu();

// For File system (very basic cache driver)
$SimpleCache = new \Rundiz\SimpleCache\Drivers\FileSystem();

// For memory cache (This is using PHP array, if class was unset everything will be removed)
$SimpleCache = new \Rundiz\SimpleCache\Drivers\Memory();

// To get or fetch cache data.
$SimpleCache->get('cache_key');

// To get multiple cache data.
$SimpleCache->getMultiple(['cache_key1', 'cache_key2']);

// To check cache exists.
$SimpleCache->has('cache_key');

// To save cache data.
$SimpleCache->set('cache_key', 'cache data. (any type of data... string, integer, double, array, object, etc.)', 90);

// To save multiple cache data.
$SimpleCache->setMultiple([
    'cache_key1' => 'string value',
    'cache_key2' => 12345,
], 90);

// To delete cache.
$SimpleCache->delete('cache_key');

// To delete multiple cache.
$SimpleCache->deleteMultiple(['cache_key1', 'cache_key2']);

// To clear all cached.
$SimpleCache->clear();

if (class_exists('\\Memcached')) {
    $memcached = new \Memcached;
    $memcached->addServer('localhost', 11211) or die ("Could not connect");
    $SimpleCache = new \Rundiz\SimpleCache\Drivers\Memcached($memcached);
    unset($memcached);
} elseif (class_exists('\\Memcache')) {
    $memcache = new \Memcache;
    $memcache->connect('localhost', 11211) or die ("Could not connect");
    $SimpleCache = new \Rundiz\SimpleCache\Drivers\Memcached($memcache);
    unset($memcache);
} elseif (function_exists('apcu_fetch')) {
    $SimpleCache = new \Rundiz\SimpleCache\Drivers\Apcu();
} else {
    $SimpleCache = new \Rundiz\SimpleCache\Drivers\FileSystem();
}