Redis: increase TTL by value - redis

For example, assume all operation is fast within 1 sec.
I accept solution with multiple step
but it should be atomic
and perofrmance is key for me
like it will update 1000 times in a sec
TTL mykey <- 10
INCRTTL mykey 5 // I need something like this
TTL mykey <- 15
TTL mykey <- 10
INCRTTL mykey 5
INCRTTL mykey 5
INCRTTL mykey 5
TTL mykey <- 25

Related

Redis query by expiration time

Let's say I have two Redis keys
1) Test1 -> expiry/ttl after 2 days
2) Test2 -> expiry/ttl after 1 day
3) Test3 -> expiry after 1month
Is it possible to just query keys by TTL or expiry time?
I could have read the keys & compare the ttl but that would be very expensive operation.

Reset value for key in redis

I am storing some values in redis like for key: 1 the value will be
{"counter":1,"counter1":2}
Now I need to reset value of counter while the counter1 should be remaining same.
To increase counter I am use the command SETEX mykey 60 redis .
But it will also reset the value of counter1. So is there any way I can reset one value for a single key.
Let me know if I need to add some more info.
Instead of string you may use hash, then it will be easy. you can increment by some other value, delete counter etc etc. Each key in your json will be hash field.
127.0.0.1:6379> hset mykey counter 1 counter1 2
(integer) 2
127.0.0.1:6379> hgetall mykey
1) "counter"
2) "1"
3) "counter1"
4) "2"
127.0.0.1:6379> hset mykey counter 25
(integer) 0
127.0.0.1:6379> hgetall mykey
1) "counter"
2) "25"
3) "counter1"
4) "2"
127.0.0.1:6379> HINCRBY mykey counter 15
(integer) 40
127.0.0.1:6379> hgetall mykey
1) "counter"
2) "40"
3) "counter1"
4) "2"
127.0.0.1:6379>

Is there a anyway to make redis key value decrease by 1 over time?

I want to use only 1 key value pair in Redis database. and the value will be decreased by 1 in every 60 seconds. Is it possible?
An interesting question :) Yes, you can do that with a trick.
As we known Redis TTL can be decreased automatically over time. So you can use TTL as the value, and the TTL will decrease by 1 every second.
Say, you want to set a value of N, in order to achieve your goal, you can set a key-value pair with expiration TTL = 60 * N:
SET key N EX TTL
When you want to get the value, just get its TTL, and do some math:
ttl = TTL key
if (ttl > 0)
value = ttl / 60 + 1
else
// no longer exist

what is the max value of an offset in setbit

I have tried to store bit using setbit in redis like
setbit mykey 123 1 and also use more than maximal int value 2147483647 + 100 as the offset value so it would be like this :
setbit mykey 2147483747 1 this one works.
And also I tried to add severals number until hit the 4547483747 and got
ERR bit offset is not an integer or out of range
my question is : what is the maximal exact value of the offset?
According to the documentation:
The offset argument is required to be greater than or equal to 0, and smaller than 2³²
So the maximum value is 4,294,967,295 (or 2³² - 1). Confirmation via the CLI:
127.0.0.1:6379> setbit mykey 4294967295 1
(integer) 1
127.0.0.1:6379> setbit mykey 4294967296 1
(error) ERR bit offset is not an integer or out of range

Redis - how to sort by hash field in redis as opposed to key?

Let's suppose I want have redis hash a = {1:10, 2:15, 3:5, 4:0, 5:20}, and a set b = (5,3,4). I want to get a list containing elements from b, sorted by values of a[b] (result in this case is [4,3,5]).
When I try to do this, it doesn't work well.
redis 127.0.0.1:6379> hmset a 1 10 2 15 3 5 4 0 5 20
redis 127.0.0.1:6379> sadd b 5 3 4
redis 127.0.0.1:6379> sort b by a->*
1) "3"
2) "4"
3) "5"
Obviously, asterisk in hash field placeholder doesn't work. Are there other ways beside declaring a:1-a:5 to do this task by means of Redis?
P.S. This is not a duplicate of Redis : How can I sort my hash by keys?, as that question clearly discusses the a:* approach.
this is a know issue: link
you could do the following:
redis 127.0.0.1:6379> sadd b 5 3 4
redis 127.0.0.1:6379> zadd a 10 1 15 2 5 3 0 4 20 5
redis 127.0.0.1:6379> zinterstore result 2 a b
redis 127.0.0.1:6379> zrange result 0 -1
1) "4"
2) "3"
3) "5"
Maybe you can model it using sorted sets instead? Use the values as score, and the keys as members. Sorted sets are more or less like hashes sorted by value. I'd love to give you an example, but I'm not sure exactly what the problem you're trying to solve is. If you could elaborate maybe I could help.