Client.lrange(key, start, stop)
返回存储在 key
上的列表中指定的元素。偏移量 start 和 stop 是基于零的索引。这些偏移量可以是负数,表示从列表末尾开始的偏移量。
参数
参数 | 类型 | 描述 |
---|---|---|
key | string | 持有列表的 key,用于获取范围。 |
start | number | 要返回的第一个元素的索引。 |
stop | number | 要返回的最后一个元素的索引。 |
返回值
类型 | 解析为 | 拒绝于 |
---|---|---|
Promise<string[]> | 成功时,Promise 解析为指定范围内的元素列表。 |
示例
import redis from 'k6/experimental/redis';
// Instantiate a new redis client
const redisClient = new redis.Client('redis://:6379');
export default async function () {
await redisClient.rpush('mylist', 'first');
await redisClient.rpush('mylist', 'second');
await redisClient.rpush('mylist', 'third');
const item = redisClient.lindex('mylist', 0);
if (item !== 'first') {
throw new Error('lindex operation should have returned first');
}
await redisClient.lrange('mylist', 1, 2);
}