在redis中插入密钥 [英] get inserted key in redis

查看:212
本文介绍了在redis中插入密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码:

sadd my_set "el1"

将"el1"插入my_set. sadd仅返回插入元素的数量.我需要的是插入元素的键,以便以后可以检索它.我确信有一种我不知道的方法.难道是正确的功能,还是我应该选择其他类似设置/获取?

to insert "el1" into my_set. sadd only returns the number of inserted elements. What I need is the key of the inserted element so I can retrieve it later. I am sure there is a way that I am not aware of. Is sadd the right function or I should choose something else like set/get?

我需要类似mysql中的auto_increment键的东西.当我插入某些内容时,请获取最后插入的元素以供进一步使用.

I need something like auto_increment key in mysql. When I insert something, get the last inserted element for further use.

我需要这样的东西:

key: 1
value: {"name": "jack", "tel": "12412415"}

所以我可以使用key = 1来获得数组.

so I could get the array using key = 1.

推荐答案

要做与"auto_increment"类似的事情,我来看一下INCR函数:

To do something vaguely similar to "auto_increment," I would look at the INCR function:

http://redis.io/commands/incr

它将增加一个值,并将新值返回给您-它是原子的(就像大多数/所有Redis命令一样),因此您不必担心线程问题.因此,您的步骤将类似于:

It will increment a value, returning the new value to you - and it is atomic (like most/all Redis commands), so you don't need to worry about threading issues. So your steps would be something like:

  1. 设置一个增量键.
  2. 要添加值时,请INCR键,然后使用返回的值INCR设置新值.
  3. 此时INCR已增加了增量键的值,因此任何重复的值插入都将使用下一个"数字.

如果要存储可以通过索引查找的项目列表,则可能需要执行以下操作(在编程伪代码中):

If you want to store a list of items which can be looked up by index, you probably want to do something like this (in programming pseudocode):

// When you initialize your database for the first time.
SET index "0"

// When you want to insert a new item:
INCR index
SET myList:(index value) "My Value"

// When you want to retrieve an item, and you have the index for it:
GET myList:(index value)

在此示例中,我假设在您的程序中您正在跟踪INCR返回的值. INCR返回的值将是插入新项目的索引,以及以后用于查找项目的索引.因此,在我的示例代码中,将(索引值)替换为从INCR获得的存储值(当然,如何执行此操作取决于您使用的编程语言).

In this example, I'm assuming that in your program you are keeping track of the values returned by INCR. The value INCR returns is going to be the index at which you insert the new item, as well as the index with which you'll look up your item later. So, in my example code, replace (index value) with the stored value you got back from INCR (how you do this depends on what programming language you're using, of course).

请注意,此操作确实允许通过DEL myList:(index value)删除中间的项目,因为您正在使用index跟踪最后一个索引,因此即使删除了一个项目,最后一个索引仍然保持不变-行为与大多数SQL服务器中的自动增量"字段非常相似.

Note that this DOES allow deletion of items in the middle, via DEL myList:(index value), because you're tracking the last index with index, so even if an item is deleted, the last index will still remain the same - this behaves very similarly to "auto increment" fields in most SQL servers.

您真的不想为此使用集合;集合本质上是无序的,并且它们并不是真正通过键"来查找事物的集合-集合中的项甚至根本没有键.集合对于您可以对它们执行的其他集合操作(​​例如SINTER或SDIFF)更有用.

You really don't want to use sets for this; sets are inherently unordered, and they are not really made to look things up by "key" - items in a set don't even really have a key. Sets are more useful for the other set operations you can perform on them, like SINTER or SDIFF.

这篇关于在redis中插入密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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