Cache

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

Usage

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.

Handlers

File system

Save cache as a file on you system.

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

Memcached

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

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.

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.

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.

Set multiple cache items

Will return bool

Delete multiple cache items

Will return bool

PSR-6 Example

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

Last updated

Was this helpful?