angelxmoreno/cakephp-simplecache-bridge

CakePHP 缓存到 SimpleCache (PSR16) 的桥梁

v1.0.2 2018-09-15 19:22 UTC

This package is auto-updated.

Last update: 2024-09-16 08:56:37 UTC


README

CakePHP 缓存到 SimpleCache (PSR16) 的桥梁

Build Status Codacy Badge Maintainability Test Coverage License Minimum PHP Version

为什么要构建这个?

在我的几个 CakePHP 应用中,我使用了一些需要 PSR-16 兼容缓存引擎的第三方库。这个桥梁允许我重用 CakePHP 应用程序中已经存在的缓存引擎和缓存配置,消除了需要构建两套不同的缓存管理库的需求。

CakePHP 3.7 已经包含这个了吗?

不。CakePHP 3.7 带来一个 PSR-16 缓存引擎;这意味着您将能够装饰一个 PSR-16 对象以实现 Cake\Cache\CacheEngine 方法。这个桥梁提供的功能是从 Cake\Cache\CacheEngine 到 PSR-16,而不是相反。

新的 SimpleCacheEngine 类(在 CakePHP 3.7 中)实现了 PSR 16 接口,并装饰/包装 CacheEngine 类。

感谢 Admad 的澄清。

示例

Cache::config('short', [
    'className' => 'File',
    'duration' => '+1 hours',
    'path' => CACHE,
    'prefix' => 'cake_short_'
]);

$cache = new Bridge('short');

//setting cache data
$cache->set('some_key', 'some value');

//getting cache data with a default
$value = $cache->get('some_key', 'some default');

//overriding the Cache engine's TTL with an int
$cache->set('some_key', 'some value', 300); //cached for 300 seconds instead of `+1 hours`

//overriding the Cache engine's TTL with a `\DateTimeInterval` ( as per the SimpleCache Interface )
$interval = new \DateTimeInterval('P1Y'); // an interval of 1 year
$cache->set('some_key', 'some value', $interval); //cached for 1 year instead of `+1 hours`

要求

  • PHP >=5.6
  • CakePHP >=3.0

安装

您可以使用 composer 安装这个库。

作为 composer 包安装的推荐方式

composer require angelxmoreno/cakephp-simplecache-bridge

设置

一旦您定义了缓存配置,您只需在创建 Bridge 实例时传递配置名称即可,如下所示

Cache::config('short', [
    'className' => 'File',
    'duration' => '+1 hours',
    'path' => CACHE,
    'prefix' => 'cake_short_'
]);

$cache = new Bridge('short');

报告问题

如果您对这个库有问题,请打开 GitHub 上的问题。

许可证

此代码在 MIT 许可证 下提供。