go - 如果存在,如何获得值(value)指数?

标签 go redis

我需要存储字符串并为每个字符串关联一个唯一的整数。整数必须尽可能短/小。有可能在 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/

相关文章:

go - 如何同时遍历 N 个文件以计算唯一单词的出现次数

sockets - 服务器套接字编程——读取HTTP头

caching - Azure Redis 缓存 - 多个错误 TimeoutException : Timeout performing GET {key}

redis - 从快照转储恢复后,Redis 快照是否恢复过期值?

go - 在 Go 中实现信号量的最佳方式是什么

go - 如何安装特定版本的 golint 以供全局使用?

go - 最初未知时在 neo4j 中建模关系

ruby-on-rails - Ruby on Rails 4 Redis 重用模型

multithreading - Grails,线程,自动服务

tomcat - 在 AWS 上使用 ElastiCache 和 ElasticBeanstalk 配置 Redis