Search code examples
postgresqlrediscounterusergrid

Alternative for Apache Usergrid Counters


I am using Apache Usergrid for a project. However I have a serious problem with its counters. It may take to 30 seconds for a counter to be updated. So I want to use something other than usergrid counters. My question is:

Is there a structure for redis, postgresql or memcached that I will be able to use hierarchical counters. Or is there any other tool for that purpose?

Thank you all.


Solution

  • There's no such counter in Redis, but you can implement one with lua script.

    Hierarchical Counter

    Each time you increment a counter with the name of "first.second.third", it automatically increments 3 counters: "first.second.third", "first.second", and "first".

    hierarchical counter lua scripts: hc.lua

    local key = KEYS[1]
    while key do
        redis.call("incrby", key, ARGV[1])
        key = string.match(key, "(.+)%.")
    end
    

    Try it with redis-cli:

    ./redis-cli --eval hc.lua first.second.third , 2
    (nil)
    ./redis-cli mget first first.second first.second.third
    1) "2"
    2) "2"
    3) "2"