菜单
开源

Client.lrem(key, count, value)

从存储在 key 的列表中移除 count 个首次出现的 value。如果 count 为正数,则从列表开头移除元素。如果 count 为负数,则从列表末尾移除元素。如果 count 为零,则移除所有匹配 value 的元素。

参数

参数类型描述
keystring存储要移除元素的列表的 key。
countnumber要移除的元素数量。
valuestring要从列表中移除的值。

返回值

类型成功时解析为失败时拒绝
Promise<number>成功时,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', 'first');
  await redisClient.rpush('mylist', 'second');
  await redisClient.lrem('mylist', 0, 'second');

  const length = await redisClient.lrem('mylist', 1, 'first');
  if (length !== 1) {
    throw new Error('lrem operations should have left 1 item behind');
  }

  await redisClient.lpop('mylist');
}