bravo3/cache

A PHP 5.4 缓存接口,包括 Redis 和 Bravo3/ORM 等多种实现

0.1.4 2015-11-10 04:54 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:54:33 UTC


README

这是一个 PHP 5.4 缓存接口,具有对常见键值存储引擎的实现

PSR 提案

本库中的接口遵循当前 PSR-6 草案对缓存接口的规定。需要注意的是,如果 PHP-FIG 采纳 PSR 标准用于缓存接口,本库中的接口将被 PSR 标准替换。

更多信息

可能变更

采用的方法遵循上述元文档中描述的“强项”或“仓库模式”。如果 PSR 标准通过,变化将很小。如果通过替代方法,本库的大部分内容以及基于这些接口的任何实现都需要重构。

当 PSR-6 通过时,将对本库应用新的主版本号。

用法

基本用法

$pool = new RedisCachePool('tcp://10.0.0.1:6379');
$item = $pool->get('foo');

$item->get();       // Pull the value from the database
$item->isHit();     // Check if the retrieval was a cache hit

$item->exists();    // Check if the entry exists in the datbase (MAY avoid actually retrieving the value)

$item->set('bar');  // Save to cache
$item->delete();    // Remove from cache

$items = $pool->getItems(['test1', 'test2', 'test3']);  // Get a collection of items

可以使用 \DateTime 对象或以秒为单位的整数偏移量来设置 TTL

$item = $pool->getItem('foo');

$dt = new \DateTime();
$dt->modify('+10 seconds');

$item->set('bar', $dt);   // Set TTL with a \DateTime object
$item->set('bar', null);  // Clear the TTL, item never expires
$item->set('bar', 10);    // Set the TTL to 10 seconds

实现

临时存储

临时存储实现是单会话存储,当会话结束时,其数据会丢失。这对于测试或在缺少真实缓存存储设备时作为默认实现很有用。或者,如果您真的不关心数据的易变性。

Redis

Redis 支持完全通过 Predis 库集成。

要启用 Redis 支持

composer require predis/predis

Bravo3/ORM

您可以使用任何 Bravo3/ORM 驱动程序来连接缓存连接,这有助于保持数据库的单点源。

要启用 ORM 支持

composer require bravo3/orm