eftec/cacheone

具有最小依赖关系的缓存库

2.18 2024-03-02 16:16 UTC

This package is auto-updated.

Last update: 2024-08-31 00:32:09 UTC


README

CacheOne 是一个针对 PHP 的缓存类服务。它支持 Redis、Memcache、PDO 和/或 APCU。

与其他缓存库不同,此库基于组(可选)。因此,它可以用于无效化单个键或整个元素组。

Packagist Total Downloads Maintenance composer php php CocoaPods

示例

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(添加)到数组的开头。如果数组不存在,则创建一个新数组。此命令允许限制数组中元素的数量。

语法

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

memcachedapcu中,它删除所有缓存

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中,您可以创建配置,测试它,加载和保存。 example/cli1.jpg

要打开它,必须运行以下脚本

php vendor/bin/cacheonecli
# or (windows)
vendor/bin/cacheonecli.bat

示例 REDIS

  • 打开CLI
  • 转到菜单缓存
  • 选择redis
  • 选择yes
  • 然后选择默认信息
  • 完成后,选择测试以测试连接

example/cliredis.jpg

  • 最后,保存配置(选择保存)
  • 选择yes
  • 选择文件名(例如此示例中的c1)

example/cliredis2.jpg

配置将保存为: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及以下版本的 support。如果您想使用此库的旧版本,则可以保留版本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。