06.Redis GETEX - 获取value并设置key的过期时间
Redis GETEX - 语法
GETEX key [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | PERSIST]
Redis GETEX命令,用于获取value并设置key的过期时间。类似GET,只是多了一个写入命令的选项:
- EX seconds -- N秒后过期。
- PX milliseconds -- N毫秒后过期。
- EXAT timestamp-seconds -- 在秒级Unix时间戳到达时过期。
- PXAT timestamp-milliseconds -- 在毫秒级Unix时间戳到达时过期。
- PERSIST -- 移除过期时间,直接持久化。
Redis GETEX - 时间复杂度
O(1)
Redis GETEX - 返回值
key中的value,key存在且value是字符串类型;nil,key不存在;报错,key存在且value不是字符串类型。
Redis GETEX - 实例演示
redis> set site "www.cregend.com"
"OK"
redis> getex site
"www.cregend.com"
redis> ttl site
(integer) -1
redis> getex site ex 60
"www.cregend.com"
redis> ttl site
(integer) 56
redis> getex site persist
"www.cregend.com"
redis> ttl site
(integer) -1
redis> getex symbol
(nil)
redis> lpush symbol "AAPL"
(integer) 1
redis> getex symbol
(error) WRONGTYPE Operation against a key holding the wrong kind of value
轻松一刻
👉👉👉 【武映三千道】教您最小成本化试错:大长腿、露香肩、会撒娇,女主美若天仙即可,其余皆可恐龙......