eftec / cacheone
最小依赖的缓存库
Requires
- php: >=7.4
- ext-json: *
- eftec/clione: ^1.32.1
Requires (Dev)
- ext-apcu: *
- ext-memcache: *
- ext-redis: *
- eftec/documentstoreone: ^1.26
- eftec/pdoone: ^4.6.1
- phpunit/phpunit: ^8.5.36
Suggests
- ext-apcu: CacheOneApcu requires it
- ext-memcache: CacheOneMemcached requires it
- ext-redis: CacheOneRedis requires it
README
CacheOne 是一个针对 PHP 的缓存类服务。它支持 Redis、Memcache、PDO 和/或 APCU。
与其他缓存库不同,此库基于组(可选)。因此,它适用于使单个键或整个元素组失效。
示例
use eftec\CacheOne; include "vendor/autoload.php"; // composer's autoload $cache=new CacheOne("redis","127.0.0.1","",6379); $cacheValue=$cache->get('','countries'); // read the cache (if any) otherwise false if($cacheValue===false) { echo "generating a new list of countries..<br>"; $countries=['USA','Canada','Japan','Chile']; $cache->set('','countries',$countries,500); // store into the cache for 500 seconds. } else { echo "read from cache<br>"; $countries=$cacheValue; } var_dump($countries);
目录
定义
创建 CacheOne 的新实例
使用 Redis 创建新的连接(Redis 是一种内存缓存库)
use eftec\CacheOne; include "../vendor/autoload.php"; $cache=new CacheOne("redis","127.0.0.1","",6379);
使用 apcu 创建新的连接(APCU 是 PHP 缓存内容的扩展)
use eftec\CacheOne; include "../vendor/autoload.php"; $cache=new CacheOne("apcu");
使用 PdoOne 创建新的连接(PdoOne 是一个使用 PDO 连接数据库的库)
use eftec\PdoOne; use eftec\CacheOne; include "../vendor/autoload.php"; $pdo=new PdoOne('mysql','127.0.0.1','root','abc.123','travisdb',false,null,1,'KVTABLE'); $pdo->logLevel=3; // optional, if you want to debug the errors. $pdo->open(); // $pdo->createTableKV(); // you should create the key-value table if it doesn't exist. $cache=new CacheOne("pdoone"); // the instance $pdo is injected automatically into CacheOne.
或使用它创建 PdoOne 实例
$cache=new CacheOne( "pdoone", ['mysql','127.0.0.1','root','abc.123','travisdb',false,null,1,'KVTABLA'] );
使用 memcache 创建新的连接(Memcache 是一个老式但仍然功能齐全的内存缓存服务器)
use eftec\CacheOne; include "../vendor/autoload.php"; $cache=new CacheOne("memcache","127.0.0.1"); // minimum configuration $cache=new CacheOne("memcache","127.0.0.1",11211,'schema'); // complete configuration
使用类 DocumentOne 创建新的连接(文件系统)
此示例需要库 eftec/documentstoreone
use eftec\CacheOne; include "../vendor/autoload.php"; $cache=new CacheOne("documentone",__DIR__."/base","schema"); // folder /base/schema must exists
DocumentStoreOne 库支持并发。
或创建新的连接,使用 redis、memcache、apcu 或 documentone(取第一个可用的)
use eftec\CacheOne; include "../vendor/autoload.php"; $cache=new CacheOne("auto");
存储值
function set($group, $key, $value, $duration = 1440): bool
它在一个组和键内存储一个值。如果操作失败,则返回 false。
注意:对于 "documentone",持续时间被忽略
$cache->set("group","key1","hello world",500); $cache->set("group","key2","hola mundo",500);
组是可选的,如果需要使整个组(删除)失效,则可以使用它。
获取值
function get($group, $key, $defaultValue = false)
它获取存储在组(可选)和键中的值。如果找不到值,则返回 false。注意:false 值可以是有效的值。
$result=$cache->get("group","key1"); $result=$cache->get("","key2"); $result=$cache->get("","key2","not found"); // if not key2 (groupless) then it returns not found
setDefaultTTL
$result=$cache->setDefaultTTL(50); // it sets the default time to live. "documentone" one uses it. $result=$cache->getDefaultTTL(); // it gets the time to live
从数组中推入和弹出值
push
它在数组的 末尾 添加一个新值。如果数组不存在,则创建一个新的数组。此命令允许限制数组中元素的数量。
语法
push($groups, $key, $value, $duration = null, $limit = 0, $limitStrategy = 'shiftold') : bool
- $Limit 用于限制数组中最大元素的数量,如果为零,则不限制元素。
- $LimitStrategy 用于确定在添加新元素且达到限制时应该做什么,shiftold 移除数组中的第一个元素,nonew 不允许添加新元素,popold 移除数组中的最后一个元素(如果达到限制)。
// cart could be [1,2,3] $cache->push('','cart',4,2000); // it adds a new element into the cart unlimitely cart is [1,2,3,4] $cache->push('','cart',5,2000,4,'shiftold'); // it limits the cart to 20 elements, pop old item if req. cart is [2,3,4,5] $cache->push('','cart',6,2000,4,'nonew'); // if the cart has 20 elements, then it doesn't add $item. cart now is [2,3,4,5]
unshift
它在数组的 开头 添加一个新值。如果数组不存在,则创建一个新的数组。此命令允许限制数组中元素的数量。
语法
unshift($groups, $key, $value, $duration = null, $limit = 0, $limitStrategy = 'popold') : bool
- $Limit 用于限制数组中最大元素的数量,如果为零,则不限制元素。
- $LimitStrategy 用于确定在添加新元素且达到限制时应该做什么,shiftold 移除数组中的第一个元素,nonew 不允许添加新元素,popold 移除数组中的最后一个元素(如果达到限制)。
// cart could be [1,2,3] $cache->unshift('','cart',4,2000); // it adds a new element into the cart unlimitely cart is [4,1,2,3] $cache->unshift('','cart',5,2000,4,'shiftold'); // it limits the cart to 20 elements, pop old item if req. cart is [2,3,4,5] $cache->unshift('','cart',6,2000,4,'nonew'); // if the cart has 20 elements, then it doesn't add $item. cart now is [2,3,4,5]
pop
它从数组的 末尾 提取一个值。如果值不存在,则返回 $defaultValue。原始数组被修改,移除数组的最后一个元素。
语法
pop($group, $key, $defaultValue = false, $duration = null) : mixed
// cart could be [1,2,3,4]; $element=$this->pop('','cart'); // now cart is [1,2,3] and $element is 4
shift
它从数组的开头移动(提取)一个值。如果该值不存在,则返回 $defaultValue。原始数组将被修改,移除最后一个元素。
语法
pop($group, $key, $defaultValue = false, $duration = null) : mixed
// cart could be [1,2,3,4]; $element=$this->shift('','cart'); // now cart is [2,3,4] and $element is 1
使键失效
function invalidate($group = '', $key = ''): bool
它使一个特定的键无效。如果操作失败,则返回 false
$cache->invalidate("group","key1"); // invalidate a key inside a group $cache->invalidate("","key1"); // invalidate a key without a group.
使组失效
invalidateGroup($group): bool
它使组内的所有键无效。它还会清理组的目录,并将其设置为空数组。
$cache->invalidateGroup("group"); // invalidate all keys inside group $cache->invalidateGroup(["group1","group2"]); // invalidate all key inside group1 and group2
使所有内容失效
invalidateAll()
它使所有缓存无效。
在 redis 中,它删除当前的模式。如果没有模式,则删除所有 Redis
在 memcached 和 apcu 中,它删除所有缓存
在 documentone 中,它删除数据库文件夹的内容
$cache->invalidateAll();
setSerializer($serializer)
它设置值序列化的方式。默认情况下是 PHP。
$cache->setSerializer('php'); // set the serializer to php (default value). It is fastest but it uses more space. $cache->setSerializer('json-array'); // set the serializer to json-array. It uses fewer space than PHP however it is a bit slower. $cache->setSerializer('json-object'); // set the serializer to json-object. $cache->setSerializer('none'); // set the serializer to none (the value must be serialized)
getSerializer();
获取值序列化的方式。
$type=$cache->getSerializer(); // get php,json-array,json-object or none
选择数据库(Redis/PdoOne)
select($dbindex)
它选择不同的数据库。默认情况下,数据库是 0。
$cache->select(1); $cache->select('table'); // PdoOne
CLI
此库还有一个交互式 CLI。在此 CLI 中,您可以创建配置、测试它、加载和保存。
要打开它,您必须运行以下脚本
php vendor/bin/cacheonecli
# or (windows)
vendor/bin/cacheonecli.bat
示例 REDIS
- 打开 CLI
- 进入菜单缓存
- 选择 redis
- 选择 yes
- 并选择默认信息
- 完成后,选择测试以测试连接
- 最后,保存配置(选择保存)
- 选择 yes
- 选择文件名(此例中为 c1)
配置将保存为:c1.config.php。
这是一个配置文件,您可以在代码中包含或复制粘贴。
<?php // eftec/CliOne(1.28) PHP configuration file (date gen: 2023-04-08 10:19). DO NOT EDIT THIS FILE /** * It is a configuration file */ $cacheOneConfig=[ 'type' => 'redis', 'cacheserver' => '127.0.0.1', 'schema' => '', 'port' => '6379', 'user' => NULL, 'password' => NULL, ];
示例
include "c1.config.php"; $cache=CacheOne::factory($cacheOneConfig);
版本
- 2.18 (2024-03-02)
- 更新依赖到 PHP 7.4。PHP 7.2 的扩展支持已于 3 年前结束。
- 在代码中添加了更多的类型提示。
- 2.17
- [composer.json] 更新依赖
- [CacheOneCli] 现在如果可用,将加载一个 PdoOneCli 实例
- 2.16.1 2023-04-08
- [composer.json] 将 cacheonecli 添加到 /bin
- [CacheOne] 添加了方法 factory()
- [CacheOneCli] 配置现在使用变量 $cacheOneConfig
- 2.16 2023-04-08
- [CacheOneCli] (1.3) 此版本打破了与 PdoOne 的依赖关系。现在它是可选的。
- 2.15 2023-04-07
- [CacheOneCli] 更新到 1.2
- [CacheOne] 更新了构造函数,因此它允许以数组的形式传递 PdoOne 的配置
- 2.14
- 更新了依赖。
- 2.13
- [redis] 修复了 redis 和 get() 的问题
- 2.12.4
- [fixed] 解决了缓存未找到的问题
- 2.12.3
- [fixed] 解决了 invalidateCache() 的问题
- 2.12 2022-06-12
- [fixed] CacheOne 现在可以正确注入在任何情况下。
- [new] [redis] 在 Redis 中,$schema 用于设置数据库(如果是数字),或用于前缀值。
- 2.11 2022-03-20
- [fixed] 添加了更多的类型 "提示"(类型验证)
- [new] 允许使用静态方法 CacheOne::instance() 获取 CacheOne 的实例(如果有的话)
- [new] 允许获取提供者(PdoOne、DocumentStoreOne、Redis、Memcache)的实例 $this->getInstanceProvider()
- 2.10 2022-03-15
- [new] getRenew() 方法,获取一个值并更新其持续时间。
- [update] 提供者 DocumentOne 现在支持 TTL。
- 2.9 2022-03-12
- 删除了一些引用并在代码中添加了类型提示。
- 在 Redis 中:如果存在模式,invalidateAll() 不会删除所有服务器。
- 在 PdoOne 中:如果目录为 null,set() 不会在 PHP 8.1 中崩溃。
- 2.8 2022-02-10
- 添加了新的提供者 PdoOne(Pdo / 数据库)。
- 放弃了对 PHP 7.1 及以下版本的支持。如果您想使用此库的旧版本,则可以保留版本 2.7。
- 2.7 2021-06-13
- 由提供者使用的 get() 方法,从未需要家族/组,因此已删除。这是 2.7 版本的最后一个版本。
- 2.6.1 2021-06-12
- 更改了 composer.json 中的依赖关系
- 2.6 2021-06-12
- 添加了方法 push()、pop()、shift() 和 unshift()
- 2.5 2020-09-20
- 将提供者分离到不同的类中。现在它还允许使用文件系统(DocumentOne)。
- 2.4 2020-09-13
- 代码已重构。
- 2.3.1
- 修复:即使序列化器是 json-object,目录也始终存储为数组
- 2.3
- 添加了方法 setSerializer() 和 getSerializer()。默认情况下,CacheOne 使用 PHP 进行序列化。使用此功能,可以使用 json 或不进行序列化
- 2.2.2 2020-03-13
- 现在目录的持续时间始终晚于键的持续时间
- 测试了缓存的持续时间和过期时间。
- phpunit 现在是 "require-dev" 部分,而不是 "require" 部分
- 2.2.1 2020-03-12
- 内部:键名不存储在组内。组存储在模式内
- 内部:目录的持续时间由 $cache->catDuration(秒)定义
- 2.2 2020-03-12
- 包装器 getCache()、setCache()、invalidateCache()
- 2.1 2020-03-12
- 单元测试
- get() 有一个默认值 $defaultValue
- 新增方法 invalidateAll()
- 2.0 2020-03-12 更新了整个类。现在它作为一个单类工作。
- 1.4.2 2019-07-30 添加了 select() 以选择不同的数据库索引。它还为构造函数添加了超时
- 1.4.1 2018-08-15 添加了一个获取 id 的内部函数。
- 1.4 2018-09-05 修复了组
- 1.3.1 2018-06-09 首次发布版本
许可证
双许可,商业和 MIT 许可证。版权所有 Jorge Castro Castillo