首页
关于
搜索
1
微信【限制新设备登录功能】解决办法
939 阅读
2
使用Mem Reduct定时清理内存
451 阅读
3
TP6+Workerman实现聊天室功能
374 阅读
4
PlanetScale 线上免费的数据库
231 阅读
5
PHPExcel快速生成字母表
197 阅读
默认分类
搜索
标签搜索
PHP
Windows 软件
GO
TrafficMonitor
网速悬浮窗
Mem Reduct
内存
PHPExcel
PlanetScale
ZipArchive
One Note插件
Redis
FRP
内网穿透
Philip
累计撰写
20
篇文章
累计收到
1
条评论
首页
栏目
默认分类
页面
关于
搜索到
1
篇与
的结果
2022-12-13
Redis 键(key) PHP用法
Redis keys 命令下表给出了与 Redis 键相关的基本命令:命令描述DEL key该命令用于在 key 存在是删除 keyDUMP key序列化给定 key ,并返回被序列化的值EXISTS key检查给定 key 是否存在EXPIRE key seconds为给定 key 设置过期时间EXPIREAT key timestampEXPIREAT 的作用和 EXPIRE 类似,都用于为 key 设置过期时间。 不同在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳(unix timestamp)PEXPIRE key milliseconds设置 key 的过期时间亿以毫秒计PEXPIREAT key milliseconds-timestamp设置 key 过期时间的时间戳(unix timestamp) 以毫秒计KEYS pattern查找所有符合给定模式( pattern)的 keyMOVE key db将当前数据库的 key 移动到给定的数据库 db 当中PERSIST key移除 key 的过期时间,key 将持久保持PTTL key以毫秒为单位返回 key 的剩余的过期时间TTL key以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)RANDOMKEY从当前数据库中随机返回一个 keyRENAME key newkey修改 key 的名称RENAMENX key newkey仅当 newkey 不存在时,将 key 改名为 newkeyTYPE key返回 key 所储存的值的类型{collapse}{collapse-item label="业务代码(PHP)" open}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}{/collapse}
2022年12月13日
123 阅读
0 评论
0 点赞