1. Go to this page and download the library: Download maplephp/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/ */
maplephp / cache example snippets
use MaplePHP\Cache\Cache;
use MaplePHP\Cache\Handlers\FileSystemHandler;
$cache = new Cache(new FileSystemHandler(dirname(__FILE__)."/storage/cache"));
$expireInOneHour = 3600; // 3600 seconds = 1 hour
if(!$cache->has("test") && $cache->set("test", "Lorem ipsum dolor", $expireInOneHour)) {
echo "Cache has been set<br>";
}
echo "Get cache: ".$cache->get("test");
$cache = new Cache(new FileSystemHandler(dirname(__FILE__)));
try {
// Invalid key set
print_r($cache->get("te st"));
} catch (Exception $e) {
// Will trigger: Invalid cache key. Only alphanumeric characters, underscores, and dots are allowed.
echo $e->getMessage();
}
use MaplePHP\Cache\Handlers\FileSystemHandler;
$fileSystem = new FileSystemHandler(dirname(__FILE__)."/storage/cache");
use MaplePHP\Cache\Handlers\MemcachedHandler;
// One server
$memcached = new MemcachedHandler(MemcachedHandler::HOST, MemcachedHandler::PORT, MemcachedHandler::WEIGHT);
// Multiple servers
$memcached = new MemcachedHandler([
["Memcached.server1.com", 11211, 1], // Weight "1" (this server has priority)
["Memcached.server2.com", 11212, 2],
["Memcached.server3.com", 11300, 3]
]);
$cache->get("test", "Default value");
$cache->has("test");
// Set cache with 1 hour lifetime
$cache->set("test", "Lorem ipsum dolor", 3600);
// Set cache that will persist
$cache->set("test2", "Lorem ipsum dolor");