dose
dose

Reputation: 33

Redis increment keyN or key:N

in Redis, increment value can be stored or we can increment value of keys. Like

127.0.0.1:6379> set _inc 0
OK
127.0.0.1:6379> INCR _inc
(integer) 1
127.0.0.1:6379> INCR _inc
(integer) 2
127.0.0.1:6379> get _inc
"2"

or we can save items like

item:UNIQUE-ID

item:UNI-QUE-ID

But how to save items with increment N ID like:

item:1
item:2
item:3
item:4
...

So far I found a solution with LUA Script

127.0.0.1:6379> eval 'return redis.call("set", "item:" .. redis.call("incr","itemNCounter"), "item value")' 0
OK
...
127.0.0.1:6379> keys item:*
 1) "item:10"
 2) "item:14"
 3) "item:13"
 4) "item:6"
 5) "item:15"
 6) "item:9"
 7) "item:4"
 8) "item:1"
 9) "item:5"
10) "item:3"
11) "item:12"
12) "item:7"
13) "item:8"
14) "item:11"
15) "item:2"

Question: Is there a method to make it without running Lua script or reliable method?

I expect that there would be a Redis command to make it.

Upvotes: 0

Views: 238

Answers (1)

Efran Cobisi
Efran Cobisi

Reputation: 6454

Question: Is there a method to make it without running Lua script or reliable method?

No, there isn't. However, EVAL is supported since Redis version 2.6 and LUA scripts are first-class citizens in Redis.

Upvotes: 2

Related Questions