菜单
开源

Client.lindex(key)

返回存储在 key 处列表中的指定元素。索引是基于零的。负数索引可用于指定从列表尾部开始的元素。

参数

参数类型描述
keystring持有要获取元素的列表的键。

返回值

类型成功时返回失败时
Promise<string>成功时,promise 返回所请求的元素。如果列表不存在或索引超出范围,promise 将因错误而被拒绝。

示例

JavaScript
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 = await redisClient.lindex('mylist', 0);
  if (item !== 'first') {
    throw new Error('lindex operation should have returned first');
  }

  await redisClient.lrange('mylist', 1, 2);
}