biurad/cache

A library that provides an advanced caching system for PSR-6 and PSR-16

v0.2.4 2020-06-22 04:36 UTC

README

Latest Version Workflow Status Software License Maintenance Status

A PHP library that provides a high-performance caching system for storing the results of expensive computations, database queries, or network requests. Supports both PSR-6 and PSR-16 caching standards.

📦 Installation

This project requires PHP 7.2 or higher. The recommended way to install, is via Composer. Simply run:

$ composer require biurad/cache

📍 Quick Start

One of the key features of this library is its ability to cache the result of a callable or function, allowing for the caching of complex computations or database queries. Additionally, it includes a fallback load feature to ensure that the cached value is always fetched, even if it has expired from the cache.

Here is an example of how to use the library using symfony/cache:

use Biurad\Cache\FastCache as Cache;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;

$storage = new PhpFilesAdapter(directory: __DIR__.'/cache');
$cache = new Cache($storage);
$cache->setCacheItem(\Symfony\Component\Cache\CacheItem::class);

// The callable will only be executed on a cache miss.
$value = $cache->load(
    'my_cache_key',
    static function (CacheItemInterface $item): CacheItemInterface {
        $item->expiresAfter(3600);

        // ... do some HTTP request or heavy computations
        $item->set('foobar');

        return $item;
    }
);

echo $value; // 'foobar'

// ... and to remove the cache key
$cache->delete('my_cache_key');

// cache the result of the function
$ip = $cache->call('gethostbyaddr', "127.0.0.1");

function calculate(array $a, array $b): array {
    $result = [];
    foreach ($a as $i => $v) foreach ($v as $k => $m) $result[$i][$k] = $m + $b[$i][$k];

    return $result;
}

$matrix = $cache->wrap('calculate');
$result = $matrix([[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]) // [[8, 10, 12], [14, 16, 18]]

// Caching the result of printable contents using PHP echo.
if ($block = $cache->start($key)) {
	  ... printing some data ...

	  $block->end(); // save the output to the cache
}

NB: The beta parameter found on cache methods default value is 1.0 and higher values mean earlier recompute. Set it to 0 to disable early recompute and set it to INF to force an immediate recompute:

📓 Documentation

In-depth documentation on how to use this library can be found at docs.biurad.com. It is also recommended to browse through unit tests in the tests directory.

🙌 Sponsors

If this library made it into your project, or you interested in supporting us, please consider donating to support future development.

👥 Credits & Acknowledgements

📄 License

Poakium Cache is completely free and released under the BSD 3 License.