phlak / stash
简单的PHP缓存库
Requires
- php: >=7.1
- nesbot/carbon: ^1.21 || ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^1.0 || ^2.0
- phpunit/phpunit: ^6.0 || ^7.0 || ^8.0
- psy/psysh: ^0.9.0
- vimeo/psalm: ^3.8
Suggests
- ext-apcu: Required to use the APCu cache driver.
- ext-memcached: Required to use the Memcached driver.
- ext-redis: Required to use the Redis cache driver.
This package is auto-updated.
Last update: 2024-08-29 14:22:37 UTC
README
轻量级PHP缓存库 • 由 Chris Kankiewicz (@PHLAK) 创建
简介
Stash 是一个轻量级的PHP缓存库,支持多种可互换的缓存后端和表达式的(Laravel 启发式)API。
支持的缓存后端
- 文件 - 文件缓存。将缓存项存储在磁盘上的目录中的文件中。
- Memcached - 高性能、分布式内存对象缓存系统
- Redis - 内存数据结构存储。
- APCu - PHP的本地APC用户缓存。
- 临时 - 仅存在于脚本生命周期的临时内存数组。
需求
- PHP >= 7.2
使用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 Releases页面找到。
故障排除
对于一般帮助和支持,请加入我们的GitHub Discussions或通过Twitter联系。
请将错误报告到GitHub Issue Tracker。
版权
本项目采用MIT许可。