redis - 在 Redis 计数器中避免在 SET 之前获取 GET

标签 redis

我有兴趣在我的网络应用程序中创建几个不同的基于 redis 的计数器。很多这些东西基本上都是用于指标等,但这并没有什么区别。我的问题基本上如下,是否可以避免这样做:

if $redis.get(key) != null
  // increment key
else
  // create key with a counter of 1 

理想情况下,像这样的东西会更优化

$redis.incr(key, 1) // increment key by 1, and if it does not exist, start it at the value 1

我是否忽略了 redis 文档?目前有办法做到这一点吗?

最佳答案

有一个 INCR 命令,如果键不存在,则将键的值设置为 1

$redis.incr()

应该可以。

参见 http://redis.io/commands/incr

关于redis - 在 Redis 计数器中避免在 SET 之前获取 GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14866166/

相关文章:

redis - 如何找到嵌入式Redis端口并杀死嵌入式Redis服务器

ssl - logstash 可以通过 ssl 访问 redis 输入吗?

python-3.x - 如何计算值与 redis-py 中的模式匹配的键数?

ruby-on-rails - 在 heroku 应用程序上使用 redis 代替数据库

redis - redis的部分重新同步是什么意思?

redis - 有没有办法用哈希来模拟列表的修剪功能?有没有更好的数据结构呢?

Laravel 和 Redis - 尽管超时设置为 1800 秒,但 10 分钟后作业被终止

redis - Jedis 基准测试 - Jedis 有多快

node.js - 等待所有 promise 在 nodejs 中用 Bluebird 完成

spring - Redis @Reference 在 Spring Data Redis 中不起作用