Client.expire(key, seconds)
为 key 设置超时时间,超时后 key 将自动删除。请注意,使用非正超时时间调用 Expire 将导致 key 被删除,而不是过期。
参数
参数 | 类型 | 描述 |
---|---|---|
key | string | 要设置过期时间的 key。 |
seconds | number | 要设置的过期时间,单位为秒。 |
返回值
类型 | Promise 成功时 | Promise 失败时 |
---|---|---|
Promise<boolean> | 成功时,如果超时已设置,Promise 将解析为 true ;如果超时未设置,则解析为 false 。 |
示例
import redis from 'k6/experimental/redis';
// Instantiate a new redis client
const redisClient = new redis.Client('redis://:6379');
export default async function () {
await redisClient.set('mykey', 'myvalue', 10);
await redisClient.expire('mykey', 100);
const ttl = await redisClient.ttl('mykey');
if (ttl <= 10 || ttl >= 100) {
throw new Error('mykey should have a ttl of 10 <= x < 100');
}
}