我需要存储字符串并为每个字符串关联一个唯一的整数。整数必须尽可能短/小。有可能在 Redis 中做到这一点吗?基本上我需要类似 SADD
的东西,但为了返回集合中的元素数量,我需要它返回插入的元素(新存储的或现有的)的索引。
伪代码:
// if mystring already exists in myset it returns its index
// otherwise stores it and returns its index.
index := storeOrReturnIndex(myset, mystring)
最佳答案
使用散列图是否可以涵盖您要查找的内容?
> HSET hashmap 0 "first string"
(integer) 1
> HSET hashmap 1 "second string"
(integer) 1
> HSET hashmap 2 "third string"
(integer) 1
> HGET hashmap 1
"second string"
> HLEN hashmap
3
您可以将最后修改的索引存储在一个键中:
> SET last_modified 1
然后检索它:
> GET last_modified
关于go - 如果存在,如何获得值(value)指数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47473569/