PHP code example of lit / redis-ext

1. Go to this page and download the library: Download lit/redis-ext library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lit / redis-ext example snippets


//连接redis
$redisHandler = new \Redis();
$redisHandler->connect("192.168.1.163");

//初始化计数器
Lit\RedisExt\LoopCounter::init($redisHandler);

/**
 * 分钟计数器: 实现每分钟5次限流器
 * 参数1 计数器redis key 可以根据使用维度自行设置
 * 参数2 计数器生命周期, 单位: 分钟
 * 参数3 是否完整分钟, true:完整的 x 分钟, false:从第一次计数开始 x 分钟)
 * 参数4 每次增加数量
 * */
if( Lit\RedisExt\LoopCounter::everyMinutes("test1", 1, false, 1) > 5 ) {
    //超限流
}else{
    //未超限流
}

/**
 * 小时计数器: 实现记录2小时之内第几次访问
 * 参数1 计数器redis key 可以根据使用维度自行设置
 * 参数2 计数器生命周期, 单位: 小时
 * 参数3 是否完整分钟, true:完整的 x 小时 false:从第一次计数开始 x 小时)
 * 参数4 每次增加数量
 * */
var_dump(Lit\RedisExt\LoopCounter::everyHours("test2", 2, true, 1));

//日期计数器 从当前开始3天
var_dump(Lit\RedisExt\LoopCounter::everyDays("test3", 3, false, 1));

//指定时间计数器 指定某时间过期
var_dump(Lit\RedisExt\LoopCounter::nextRoundAt("test4", time() + 3600 * 7));

//销毁一个计数器
var_dump(Lit\RedisExt\LoopCounter::destroy("test3"));

//获取一个计数器数值
var_dump(Lit\RedisExt\LoopCounter::get("test4"));

//初始化固定集合
Lit\RedisExt\CappedCollections::init($redisHandler);

/**
 * 参数1 固定集合的key
 * 参数2 要记录的数据
 * 参数3 固定集合限制条数
 * */
var_dump(Lit\RedisExt\CappedCollections::set("cappedKey", uniqid(), 20));

/**
 * 参数1 固定集合的key
 * */
var_dump(Lit\RedisExt\CappedCollections::size("cappedKey"));

/**
 * 参数1 固定集合的key
 * 参数2 偏移量
 * 参数3 获取数据条数限制
 * */
var_dump(Lit\RedisExt\CappedCollections::get("cappedKey", 15, 5));

/**
 * 参数1 固定集合的key
 * */
var_dump(Lit\RedisExt\CappedCollections::destroy("cappedKey"));

//初始化限流器
Lit\RedisExt\LoopThrottle::init($redisHandler);

/**
 * 参数1 限流器key
 * 参数2 限流次数
 * 参数2 限流周期
 * 返回 true: 可以访问, false: 被限流
 * */
var_dump(Lit\RedisExt\LoopThrottle::attempt("tKey1", 2, 10));

/**
 * 参数1 限流器key
 * 参数2 限流周期
 * 返回 周期内有效访问次数
 * */
var_dump(Lit\RedisExt\LoopThrottle::count("tKey1", 300));


/**
 * 参数1 限流器key
 * */
var_dump(Lit\RedisExt\LoopThrottle::destroy("tKey1"));

/**
 * 参数1: $redisHandler redis链接句柄
 * 参数2: int $collectionsLength 内置固定集合长度, 用于保存执行历史
 */
\Lit\RedisExt\AsyncMethod::init($redisHandler, 50);

/**
 * 参数1: 固定的RedisKey
 * 参数2: 被实例化的对象
 * 参数3: 要执行的方法名称
 * 参数4: 调用的所有参数 (注意,此参数会在执行是增加一个 _uniqId 下标的唯一ID,供使用者对进程进行监控, 详见demo)
 * 返回值: string 唯一的进程ID _uniqId
 */
\Lit\RedisExt\AsyncMethod::add("testKey", new \Demo\DemoClass(), 'staticClass', ["a" => 1, "b" => 3]);


/**
 * 参数1: 固定的RedisKey
 * 返回值: array 
 *      callable: 函数调用信息
 *      return: 异步方法返回值
 */
\Lit\RedisExt\AsyncMethod::run("testKey");

/**
 * 参数1: add 方法返回的唯一进程ID
 * 返回值: int|false 
 *     int 进程状态码
 *     false 未找到记录
 * 附进程状态码:
 *    -1 失败
 *     0 等待中
 *     1 运行中
 *     2 成功 
 */
var_dump(\Lit\RedisExt\AsyncMethod::getStatus("t-632ad7f381214"));

/**
 * 参数1: 指定一个RedisKey, 同 add 方法一致
 * 参数2: 列表分页 索引
 * 参数3: 列表分页 显示条数
 * 返回值: array 列表数据
 *    data: 数据
 *    nextIndex: 下一页索引开始
 *    count: 总条数
 */
var_dump(\Lit\RedisExt\AsyncMethod::getList("testKey",0 , 10));

\Lit\RedisExt\XLocks::init($redisHandler);

/**
 * 参数1: 锁的RedisKey
 * 参数2: 自动解锁时间(秒). 0为不自动解锁
 * 参数3: 使用完成后自动解锁(忽略自动解锁时间)
 * 返回值: bool true拿到锁, false未拿到锁
 */
var_dump(\Lit\RedisExt\XLocks::lock("testa", 20, true));

/**
 * 参数1: 锁的RedisKey
 * 返回值: bool true已解锁, false解锁失败或未上锁
 */
var_dump(\Lit\RedisExt\XLocks::unLock("testa"));

/**
 * 参数1: 锁的RedisKey
 * 返回值: int 锁的生命周期
 */
var_dump(\Lit\RedisExt\XLocks::ttl("testa"));

\Lit\RedisExt\CacheSup::init($redisHandler);

$version = "1.0.0";
var_dump(\Lit\RedisExt\CacheSup::get("tmpKey:0", $version));
//NULL 
// 或者
//array(2) {
//  [0]=>
//  string(19) "tmp:1:64586e4405fa5"
//  [1]=>
//  string(19) "tmp:2:64586e4406026"
//}


$version = "1.0.0";
var_dump(\Lit\RedisExt\CacheSup::set("tmpKey:0", ["tmp:1:" . uniqid(), "tmp:2:" . uniqid()], $version, 30));
//bool(true)

$version = "1.0.0";
$keyObject = new CacheStringKey("tmpKey:0:0", [1, 1]);
$data = \Lit\RedisExt\CacheSup::getOrSet($keyObject, function ($id1, $id2) {
    return $id1 . ":" . $id2 . ":" . uniqid();
}, $version, 30);


$version = "1.0.0";
var_dump(\Lit\RedisExt\CacheSup::mGet(["tmpKey:1", "tmpKey:2", "tmpKey:3"], $version));
//array(3) {
//  ["tmpKey:1"]=>
//  NULL
//  ["tmpKey:2"]=>
//  NULL
//  ["tmpKey:3"]=>
//  NULL
//}
//或者
//array(3) {
//  ["tmpKey:1"]=>
//  string(19) "tmp:1:64586e4407117"
//  ["tmpKey:2"]=>
//  string(19) "tmp:2:64586e4407140"
//  ["tmpKey:3"]=>
//  string(19) "tmp:3:64586e4407145"
//}

$version = "1.0.0";
$cacheData = ["tmpKey:1" => "tmp:1:" . uniqid(), "tmpKey:2" => "tmp:2:" . uniqid(), "tmpKey:3" => "tmp:3:" . uniqid()];
var_dump(\Lit\RedisExt\CacheSup::mSet($cacheData, $version, 30));
//bool(true)

$version = "1.0.0";
$keyObjects = [
    new CacheStringKey("tmpKey:1:1", [1, 1]),
    new CacheStringKey("tmpKey:2:2", [2, 2]),
    new CacheStringKey("tmpKey:3:3", [3, 3])
];
$data = \Lit\RedisExt\CacheSup::mGetOrSet($keyObjects, function ($id1, $id2) {
    return $id1 . ":" . $id2 . ":" . uniqid();
}, $version, 30);
var_dump($data);
//array(3) {
//  ["tmpKey:1:1"]=>
//  string(17) "1:1:64586db82eded"
//  ["tmpKey:2:2"]=>
//  string(17) "2:2:64586db82ee35"
//  ["tmpKey:3:3"]=>
//  string(17) "3:3:64586db82ee3a"
//}