biurad/cache

一个提供PSR-6和PSR-16高级缓存系统的库

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

README

Poakium Cache

Latest Version Workflow Status Software License Maintenance Status

一个提供高性能缓存系统以存储昂贵的计算、数据库查询或网络请求结果的PHP库。支持PSR-6PSR-16缓存标准。

📦 安装

此项目需要PHP 7.2或更高版本。推荐使用Composer进行安装。只需运行

$ composer require biurad/cache

📍 快速开始

此库的一个关键特性是能够缓存可调用对象或函数的结果,允许缓存复杂的计算或数据库查询。此外,它还包括一个回退加载功能,确保即使缓存已过期,缓存值也始终被检索。

以下是如何使用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
}

注意:缓存方法中找到的beta参数的默认值是1.0,更高的值表示更早的重计算。将其设置为0以禁用早期重计算,将其设置为INF以强制立即重计算

📓 文档

有关如何使用此库的深入了解文档可以在docs.biurad.com找到。还建议浏览tests目录中的单元测试。

🙌 赞助商

如果这个库被纳入了您的项目,或者您有兴趣支持我们,请考虑捐赠以支持未来的开发。

👥 信用 & 致谢

📄 许可证

Poakium Cache 完全免费,并按照BSD 3许可证发布。