PHP code example of mcustiel / php-simple-cache

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

    

mcustiel / php-simple-cache example snippets


$cacheManager = new \Mcustiel\SimpleCache\Drivers\memcache\Cache();
$config = new \stdClass();
$config->host = 'yourmemcached.host.com';
$config->port = 11211;
$config->timeout = 1;
$cacheManager->init($config);

$connection = new \Memcache();
$connection->connect();
$cacheManager = new \Mcustiel\SimpleCache\Drivers\memcache\Cache($connection);
// No init() call 

use \Mcustiel\SimpleCache\SimpleCache;

$factory = new SimpleCache();
$cacheManager = $factory->getCacheManager('memcache');
$config = new \stdClass();
$config->host = 'yourmemcached.host.com';
$config->port = 11211;
$config->timeout = 1;
$cacheManager->init($config);

use \Mcustiel\SimpleCache\Types\Key;

$key = new Key('cached-data-key');
$data = $cacheManager->get($key);
if ($data === null) {
	$data = someHeavyProcessThatGeneratesDataThatCanBeCached();
	$cacheManager->set($key, $data);
}

use \Mcustiel\SimpleCache\Types\Key;

$key = new Key('cached-data-key');
$cacheManager->delete($key);

$config = new \stdClass()
# Memcache server address, default is localhost
$config->host = 'yourmemcached.host.com';
# Memcache server port, default is php.ini:memcache.default_port
$config->port = 11211;
# Connection timeout in seconds, default is 1
$config->timeoutInSeconds = 1;
# Whether or not to use a persistent connection, default is false
$config->persistent = false;

$config = new \stdClass()
# Redis server address, default is localhost
$config->host = 'yourredis.host.com';
# Redis server port, default is null
$config->port = 6379;
# Connection timeout in seconds, default is null
$config->timeoutInSeconds = 1;
# Redis server auth password, default is null
$config->password = 'yourRedisPasswd';
# Database number, default is 0 
$config->database = 2;
# Persistent connection id. If not specified, then  persistent connection is not used.
$config->persistedId = 'yourPersistentConnectionId';

$fileService = new Mcustiel\SimpleCache\Drivers\file\Utils\FileService('/path/to/cache/files')
javascript
{
    "    "mcustiel/php-simple-cache": ">=1.3.1"
    }
}