简体   繁体   English

Laravel Redis使用ttl创建一个集合?

[英]laravel redis creating a set with ttl?

so for creating a set I can do 所以我可以做一套

Redis::sadd('example',[1,4,6,1,])

I tried many variations to also create a set with a ttl non worked: 我尝试了多种变体来创建一个ttl不起作用的集合:

Redis::sadd('example',100,[1,4,6,1,])
Redis::sadd('example',[1,4,6,1,],100)
Redis::saddex('example',100,[1,4,6,1,])
Redis::saddex('example',[1,4,6,1,],100)

For additional reference, If you want to check whether a [sorted set] key (in your case, the 'example') exists or not, You can either do the ff: 作为其他参考,如果要检查是否存在[sorted set]键(在您的情况下为“ example”),则可以执行以下操作:

Proposal 1: (What you've preferred) 建议1 :(您的首选)

$iTtlRedisKey = Redis::ttl('example');
if ($iTtlRedisKey <= 0) {
    Redis::sadd('example', [1,4,6,1]);
    Redis::expire('example', 30);
}

$aSortedExampleSets = Redis::smembers('example');
// array(3) { [0]=> string(1) "1" [1]=> string(1) "4" [2]=> string(1) "6" }

Proposal 2: Using [exists] method 建议2:使用[存在]方法

$bCheckRedisKey = Redis::exists('example');
if (boolval($bCheckRedisKey) !== true) {
    Redis::sadd('example', [1,4,6,1]);
    Redis::expire('example', 30);
}

$aSortedExampleSets = Redis::smembers('example');
// same results as well.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM