Redis keys 命令
下表给出了与 Redis 键相关的基本命令:
命令 | 描述 |
---|---|
DEL key | 该命令用于在 key 存在是删除 key |
DUMP key | 序列化给定 key ,并返回被序列化的值 |
EXISTS key | 检查给定 key 是否存在 |
EXPIRE key seconds | 为给定 key 设置过期时间 |
EXPIREAT key timestamp | EXPIREAT 的作用和 EXPIRE 类似,都用于为 key 设置过期时间。 不同在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳(unix timestamp) |
PEXPIRE key milliseconds | 设置 key 的过期时间亿以毫秒计 |
PEXPIREAT key milliseconds-timestamp | 设置 key 过期时间的时间戳(unix timestamp) 以毫秒计 |
KEYS pattern | 查找所有符合给定模式( pattern)的 key |
MOVE key db | 将当前数据库的 key 移动到给定的数据库 db 当中 |
PERSIST key | 移除 key 的过期时间,key 将持久保持 |
PTTL key | 以毫秒为单位返回 key 的剩余的过期时间 |
TTL key | 以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live) |
RANDOMKEY | 从当前数据库中随机返回一个 key |
RENAME key newkey | 修改 key 的名称 |
RENAMENX key newkey | 仅当 newkey 不存在时,将 key 改名为 newkey |
TYPE key | 返回 key 所储存的值的类型 |
class RedisKeyDemo{
public $redis; // redis实例
public $type_list = ['Not Found', 'String', 'Set', 'List', 'Zset', 'Hash', 'Stream']; // 数据类型
public function __construct(){
$this->redis = new Redis();
}
/**
* 链接 Redis
* @param string $host
* @param int $port
* @return bool
*/
public function connect(string $host = '127.0.0.1', int $port = 6379): bool{
return $this->redis->connect($host, $port);
}
/**
* 打印到html
* @param $str
*/
public function printToHtml(string $str = ''): void{
echo $str."<br>";
}
/**
* 打印所有keys
*/
public function printAllKeys(): void{
$this->keys();
}
/**
* 判断键是否存在
* @param string $key
* @return bool|int
*/
public function isExists(string $key = ''): void{
$this->redis->exists($key) ? $this->printToHtml("$key is exists") : $this->printToHtml("$key is not exists");
}
/**
* 返回键类型
* @param string $type
*/
public function keyType(string $key = ''): void{
$this->printToHtml($this->type_list[$this->redis->type($key)]);
}
/**
* 删除指定键
* 原本方法可以 key1,key2···, 接受string或array
* @param string $key
*/
public function delKey(string $key = ''): void{
$this->printToHtml($this->redis->del($key) ? "'$key'删除成功" : "'$key'删除失败");
}
/**
* 为指定键设置过期时间
* @param string $key
* @param int $ttl 过期时间
* @param string $type second 使用秒, timestamp 使用时间戳, milliseconds 使用时间亿以毫秒计
*/
public function expiredKey(string $key = '', int $ttl, string $type = 'second'): void{
$error = "设置过期时间失败";
if ($ttl > 0){
switch ($type){
case 'second':
$this->printToHtml($this->redis->expire($key, $ttl) ? "$key 将在 $ttl s 后删除" : $error);break;
case 'timestamp':
$this->printToHtml($this->redis->expireAt($key, $ttl) ? "$key 将在 ".date('Y-m-d H:i:s', $ttl)." 删除": $error);break;
case 'milliseconds':
$this->printToHtml($this->redis->pExpire($key, $ttl) ? "$key 将在 $ttl 亿以毫秒后删除" : $error);break;
default:
$this->printToHtml('$type: second 使用秒, timestamp 使用时间戳, milliseconds 使用时间亿以毫秒计');
}
} else {
$this->printToHtml($error);
}
}
/**
* 查找符合给定模式的key
* 例如 查找te开头的所有key: te* ; 查找包含a的key: *a* ; 获取所有key: *
* @param string $pattern
*/
public function keys(string $pattern = '*'): void{
$this->printToHtml(json_encode($this->redis->keys($pattern)));
}
/**
* 将当前数据库的 key 移动到给定的数据库 db 当中
* @param string $key
* @param int $db
* @return bool
*/
public function move(string $key = '', int $db = 0): bool{
return $this->redis->move($key, $db);
}
/**
* 移除 key 的过期时间,key 将持久保持。
* @param string $key
* @return bool
*/
public function persist(string $key): bool{
return $this->redis->persist($key);
}
/**
* 返回key的剩余过期时间
* @param string $key
* @param string $type ttl以秒为单位, pttl以毫秒为单位
*/
public function ttl(string $key, string $type = 'ttl'): void{
$error_flag = [
-1 => '没有设置过期时间',
-2 => '不存在的key',
false => '不存在的key'
];
switch ($type){
case 'ttl':
$temp = $this->redis->ttl($key);
$this->printToHtml($error_flag[$temp] ?? "剩余 $temp 秒");break;
case 'pttl':
$temp = $this->redis->pttl($key);
$this->printToHtml($error_flag[$temp] ?? "剩余 $temp 毫秒");break;
default:
$this->printToHtml('$type ttl以秒为单位, pttl以毫秒为单位');
}
}
/**
* 从数据库中随机返回一个key
*/
public function randomKey(){
$this->printToHtml("Random Key: ".$this->redis->randomKey());
}
/**
* 修改 key 的名称
* @param string $key
* @param string $new_name
* @param bool $strict == true时, 仅当 new_name 不存在时,将 key 改名为 new_name
*/
public function rename(string $key = '', string $new_name = '', bool $strict = false): void{
$flag = !$strict ? $this->redis->rename($key, $new_name) : $this->redis->renameNx($key, $new_name);
$this->printToHtml($flag ? "重命名 $key->$new_name" : '重命名失败');
}
}
{/collapse-item}
{collapse-item label="调用示例"}
$demo = new RedisKeyDemo();
$demo->connect();
// 判断指定键是否存在
$demo->isExists('cao');
// 判断指定键类型
$demo->keyType('redis_yes');
$demo->keyType('myname');
// 删除键
//$demo->delKey('myname');
$demo->printAllKeys();
// 为key设置过期时间
$demo->expiredKey('test', time()+9999, 'timestamp'); // 使用时间戳
// 查找'te'开头的所有key
$demo->keys('te*');
// 查看key的过期时间
$demo->ttl('test');
$demo->ttl('philip', 'pttl');
$demo->ttl('hstab');
// 移除过期时间
$demo->printToHtml('test 移除过期时间'. ($demo->persist('test') ? '成功' : '失败'));
// 从数据库中随机获取一个key
$demo->randomKey();
// 重命名key
$demo->redis->set('test2', 5555);
$demo->rename('test2', 'testt');
$demo->rename('testt', 'hstab', true); // 仅当new_name不存在时改名
{/collapse-item}
评论