1

我有兴趣在我的 Web 应用程序中创建几个不同的基于 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 文档?目前有没有办法做到这一点?

4

1 回答 1

5

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

$redis.incr()

应该管用。

http://redis.io/commands/incr

于 2013-02-14T01:07:44.753 回答