从 redis 列表中删除特定索引处的元素 [英] Remove element at specific index from redis list

查看:86
本文介绍了从 redis 列表中删除特定索引处的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以删除 redis 列表中特定索引处的元素?没有真正找到我想要的.修剪允许您选择一组特定的元素,LREM 允许您按值删除列表中的项目,但我没有该值.

Is it possible to remove an element at a specific index in an redis list ? Not really finding what I want. There is trim which allows you to select an specific set of elements, LREM allows you to delete an item in a list by value, but I dont have the value.

我发现了一个技巧,您可以使用 LSET 将元素的值更改为 UID 或字符串,例如'DELETED',然后你在这个值上调用 LREM.不过这感觉有点脏.

I found an hack where you use LSET to change the value of the element to a UID or string e.g. 'DELETED', and the you call LREM on this value. This just feels a bit dirty though.

推荐答案

因此,实现我想要的唯一方法是将索引处的值设置为预先确定的字符串,然后按值进行删除.

So the only way to accomplish what I wanted was to set the value at the index to a pre determined string and then do an removal by value.

请参阅此处的讨论 https://groups.google.com/forum/#!topic/redis-db/c-IpJ0YWa9I

在ruby中如下

@redis.lset("#{@namespace}/#{specified_queue}", index, "DELETED")
@redis.lrem("#{@namespace}/#{specified_queue}", 1, "DELETED")

LSET 文档 http://redis.io/commands/lsetLREM 文档 http://redis.io/commands/lrem

LSET docs http://redis.io/commands/lset LREM docs http://redis.io/commands/lrem

这篇关于从 redis 列表中删除特定索引处的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆