phlak/stash

简单的PHP缓存库

维护者

详细信息

github.com/PHLAK/Stash

源代码

问题

支持包维护!
PHLAK
Paypal

7.0.0 2020-02-08 07:59 UTC

README

Stash

轻量级PHP缓存库 • 由 Chris Kankiewicz (@PHLAK) 创建

Join our Community Become a Sponsor One-time Donation
Latest Stable Version Total Downloads License GitHub branch checks state

介绍

Stash 是一个轻量级的 PHP 缓存库,支持多种可互换的缓存后端和一个表达式的 (Laravel 启发) API。

支持的缓存后端

  • 文件 - 基于文件的缓存。将缓存项存储在磁盘上的目录中的文件中。
  • Memcached - 高性能、分布式内存对象缓存系统
  • Redis - 内部数据结构存储。
  • APCu - PHP 的原生 APC 用户缓存。
  • 临时 - 一个短暂的、仅在脚本生命周期内存在的内存数组。

要求

使用 Composer 安装

composer require phlak/stash

初始化客户端

首先,导入 Stash

use PHLAK\Stash;

然后使用命名构造函数实例化 Stash 以选择后端

$stash = Stash\Cache::file($config);
$stash = Stash\Cache::memcached($config);
$stash = Stash\Cache::redis($config);
$stash = Stash\Cache::apcu($config);
$stash = Stash\Cache::ephemeral();

$config 参数接受一个特定驱动器的 闭包,用于设置您选择驱动器的配置选项。有关更多信息,请参阅以下每个驱动器的特定文档。并非所有驱动器都需要配置函数。

或者,您可以使用 Stash\Cache::make() 工厂方法来实例化您的驱动器。

$stash = Stash\Cache::make($driver, $config);

make() 方法接受两个参数。第一个 ($driver) 应该是以下表示您所需缓存驱动器的以下小写字符串之一。

  • apcu
  • ephemeral
  • file
  • memcached
  • redis

第二个 ($config) 是与使用命名构造函数时相同的特定驱动器配置闭包。有关更多信息,请参阅以下每个驱动器的特定文档。

文件缓存

文件缓存配置闭包必须调用 $this->setCacheDir($path),其中 $path 是要存储缓存文件的有效目录的路径。

$stash = Stash\Cache::file(function (): void {
    $this->setCacheDir('path/to/cache');
});

Memcached

Memcached 配置闭包接收一个 Memcached 对象 实例作为其唯一参数,您可以使用此参数连接和配置 Memcached。至少,您必须通过 addServer()addServers() 方法连接到一个或多个 Memcached 服务器。

请参阅 PHP Memcached 文档 以获取更多配置选项。

$stash = Stash\Cache::memcached(function (Memcached $memcached): void {
    $memcached->addServer('localhost', 11211);
    // $memcached->setOption(Memcached::OPT_PREFIX_KEY, 'some_prefix');
});

Redis

Redis 配置闭包接收一个 Redis 对象 实例作为其唯一参数,您可以使用此参数连接到并配置 Redis。至少,您必须通过 connect()pconnect() 方法连接到一个或多个 Redis 服务器。

请参阅 phpredis 文档 以获取更多配置选项。

$stash = Stash\Cache::redis(function (Redis $redis): void {
    $redis->pconnect('localhost', 6379);
    // $redis->setOption(Redis::OPT_PREFIX, 'some_prefix');
});

APCu

APCu 驱动程序在 PHP 的 APC 用户缓存中缓存项。

$stash = Stash\Cache::apcu();

APCu 驱动程序不需要配置闭包。但是,如果您希望设置缓存前缀,可以传递一个配置闭包,该闭包调用 $this->setPrefix($prefix),其中 $prefix 是您想要的字符串前缀。

$stash = Stash\Cache::apcu(function (): void {
    $this->setPrefix('some_prefix');
});

临时

临时驱动程序在 PHP 数组中缓存项,该数组仅在脚本的生命周期内存在于内存中。临时驱动程序不采用配置闭包。

$stash = Stash\Cache::ephemeral();

使用

Cacheable::put(string $key, mixed $data[, $minutes = 0]) : bool

将项目添加到缓存,指定持续时长。

示例
// Cache a value for 15 minutes
$stash->put('foo', 'some value', 15);

// Cache a value indefinitely
$stash->put('bar', false);

Cacheable::forever(string $key, mixed $data) : bool

永久将项目添加到缓存。

示例
$stash->forever('foo', 'some value');

Cacheable::get(string $key[, $default = false]) : mixed

从缓存中检索项目。

示例
$stash->get('foo');

// Return 'default' if 'bar' doesn't exist
$stash->get('bar', 'default');

Cacheable::has(string $key) : bool

检查项目是否存在于缓存中。

示例
$stash->has('foo');

Cacheable::remember(string $key, int $minutes, Closure $closure) : mixed

从缓存中检索项目或,当项目不存在时,执行闭包。然后将闭包的结果存储在缓存中,指定时长,并立即返回。

示例
$stash->remember('foo', 60, function() {
    return new FooClass();
});

Cacheable::rememberForever(string $key, Closure $closure) : mixed

从缓存中检索项目或,当项目不存在时,执行闭包。然后将闭包的结果永久存储在缓存中。

示例
$stash->rememberForever('pokemon', function() {
    return new Pokemon($name, $description);
});

Cacheable::increment(string $key[, int $value = 1]) : mixed

增加已存储在缓存中的整数。

示例
// Increment by 1
$stash->increment('foo');

// Increment by 10
$stash->increment('bar', 10);

Cacheable::decrement(string $key[, int $value = 1]) : mixed

减少已存储在缓存中的整数。

示例
 // Decrements by 1
$stash->decrement('foo');

 // Decrements by 10
$stash->decrement('bar', 10);

Cacheable::touch(string|array $key[, int $minutes = 0]) : bool

扩展缓存中项目的过期时间。

示例
 // Extend the expiration by 5 minutes
$stash->touch('foo', 5);

 // Extend the expiration indefinitely
$stash->touch('bar');

// Extend the expiration of multiple items by 5 minutes
$stash->touch(['foo', 'bar', 'baz'], 5);

Cacheable::forget(string $key) : bool

从缓存中移除项目。

示例
$stash->forget('foo');

Cacheable::flush() : bool

删除缓存中的所有项目。

示例
$stash->flush();

变更日志

可以在GitHub 发布页面找到变更列表。

故障排除

为了获得一般帮助和支持,请加入我们的GitHub 讨论区或通过Twitter联系我们。

请将错误报告到GitHub 问题跟踪器

版权

此项目遵循MIT 许可证