MaplePHP Cache, a clean and user-friendly caching library. MaplePHP Cache is designed to provide a seamless caching experience with simplicity and ease of use as its core principles. Whether you're familiar with PSR-6 or PSR-16, this library offers a smooth and intuitive caching solution for your PHP applications.

Initiate cache library

Start with initiate the MaplePHP Cache class and pass on a Handler to it.

use MaplePHP\Cache\Cache;
use MaplePHP\Cache\Handlers\FileSystemHandler;

$cache = new Cache(new FileSystemHandler(dirname(__FILE__)."/storage/cache"));


It is super easy to use

$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");

Error handling

While not required, it is advisable to handle critical throwable errors that may occur, such as an invalid cache key, insufficient file permissions, or possible connection issues with a cache server. Logging these exceptions can be beneficial for error tracking and troubleshooting purposes.

$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();


File system

Save cache as a file on you system.

Arg1: (string) Path to directory where you want to save the cache fiels

use MaplePHP\Cache\Handlers\FileSystemHandler;
$fileSystem = new FileSystemHandler(dirname(__FILE__)."/storage/cache");


Use Memcached to save cache in memory (high performance)

Arg1: (string|array) Host to server (or get default with class constant "MemcachedHandler::HOST")

Arg2: (int|null) Port to server (or get default with class constant "MemcachedHandler::PORT")

Arg3: (int) Weight to server (Arg is default 0 but you can also set it with default with class constant "MemcachedHandler::WEIGHT")

use MaplePHP\Cache\Handlers\MemcachedHandler;
// One server
$memcached = new MemcachedHandler(MemcachedHandler::HOST, MemcachedHandler::PORT, MemcachedHandler::WEIGHT);
// Multiple servers
$memcached = new MemcachedHandler([
	["", 11211, 1], // Weight "1" (this server has priority)
	["", 11212, 2],
	["", 11300, 3]

Command list (PSR-16)

Get a cache item

Can return mixed values if successful if miss then return default value. The default value is not required and is by default null.

$cache->get("test", "Default value");

Check if cache item exists

Will return bool


Set a cache item

It is allowed to set cache values of mixed types like strings, arrays and including PSR-7: HTTP Streams. Will return bool.

// Set cache with 1 hour lifetime
$cache->set("test", "Lorem ipsum dolor", 3600);
// Set cache that will persist
$cache->set("test2", "Lorem ipsum dolor");

Delete a cache item

Will return bool


Clear and auto delete all cache items

Will return bool


Get multiple cache items

Will return array with mixed values if successful, if miss then return default value. The default value is not required and is by default null.

$cache->getMultiple(["test1", "test2"], "Default value");

Set multiple cache items

Will return bool

$cache->setMultiple(["test1" => "Lorem", "test2" => "Ipsum"], 3600);

Delete multiple cache items

Will return bool

$cache->deleteMultiple(["test1", "test2"]);

PSR-6 Example

If your application needs advanced caching features, hierarchical caching, or cache tagging, PSR-6 is a more suitable choice.

use MaplePHP\Cache\Handlers\FileSystemHandler;
$cache = new FileSystemHandler(dirname(__FILE__)."/storage/cache");

$item = $cache->getItem('test');

try {
	if(!$item->isHit()) {
		$item->set(["Lorem 1", "Lorem 2"])->expiresAfter(3600);

		echo "Insert to cache: ";

	} else {
		echo "Read from cache: ";

} catch (Exception $e) {
	echo $e->getMessage();

Last updated