How to search for a key in HGETALL response in redis? - redis

127.0.0.1:6379> hgetall "box_vehicle_mapping"
1) "A9AC"
2) "{\"box_version\": \"v8_3zo\", \"box_sub_version\": \"8\", \"box_number\": \"A9AC\", \"box_usage_type\": \"vehicle\", \"version\": 8, \"current_state\": 2, \"registration_number\": [\"KA-03-AE-0017\"]}"
3) "A99V"
4) "{\"registration_number\": \"KA-05-AH-8889\", \"box_version\": \"v8_3zo\"}"
5) "A9A3"
6) "{\"box_version\": \"v8_3zo\", \"box_sub_version\": 17, \"box_number\": \"A9A3\", \"box_usage_type\": \"vehicle\", \"version\": 8, \"current_state\": 2, \"registration_number\": [\"KA-03-AE-0016\"]}"
7) "A9AD"
8) "{\"box_version\": \"v8_3zo\", \"box_sub_version\": \"12\", \"box_number\": \"A9AD\", \"box_usage_type\": \"vehicle\", \"version\": 8, \"current_state\": 2, \"registration_number\": [\"KA-03-AE-0021\"]}"
127.0.0.1:6379>
How can i do a search over it, say I want to know if "A99V" key is present in the result of hgetall key?

Just use HEXISTS if all you care about is one key existing, or HGET to get one value.
HGET box_vehicle_mapping A99V

Related

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.
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.

Redis: Sorted Set sorted by member ¿?

The Redis ZSET Sorted Set (member, score) sorts the set by the score.
The Redis SET are an unordered collection of unique Strings.
What I need is a method that returns the members in a Sorted Set matching a pattern as in ZRANGEBYLEX but with members with different scores.
Is is possible at all with redis?
Well, it seems I did not know about the SCAN suite. In this case ZSCAN solves this issue, however with cost O(N) where N is the number of items in sorted set because it iterates over the whole set.
Example of elements in:
127.0.0.1:6379> zrange l 0 -1 WITHSCORES
1) "foodgood:irene"
2) "1"
3) "foodgood:pep"
4) "1"
5) "sleep:irene"
6) "1"
7) "sleep:juan"
8) "1"
9) "sleep:pep"
10) "1"
11) "sleep:toni"
12) "1"
13) "foodgood:juan"
14) "2"
Now ZSCAN for those with prefix foodgood:
127.0.0.1:6379> ZSCAN l 0 match foodgood:*
1) "0"
2) 1) "foodgood:irene"
2) "1"
3) "foodgood:pep"
4) "1"
5) "foodgood:juan"
6) "2"
The first returned value "0" if zero indicates collection was completely explored.
What I would have liked is that to be O(log(N)+M) where M is the number of elements retrieved similar to Binary Search Tree.

Redis CLUSTER NODES showing in slowlog

I am using a redis cluster with 3 master and 3 slave as mysql cache,and the client is redisson with #Cacheabel annotation.But I found some slow logs with the command CLUSTER NODES like:
3) 1) (integer) 4
2) (integer) 1573033128
3) (integer) 10955
4) 1) "CLUSTER"
2) "NODES"
5) "192.168.110.102:57172"
6) ""
4) 1) (integer) 3
2) (integer) 1573032928
3) (integer) 10120
4) 1) "CLUSTER"
2) "NODES"
5) "192.168.110.90:59456"
6) ""
So ,I want to know what was the problem?

REDIS. Get where clause

I have this keys list:
redis 127.0.0.1:6379> keys *
1) "r:fd:g1:1377550557255"
2) "r:fd:g1:1377550561240"
3) "r:fd:g1:1377550561561"
4) "r:fd:g1:1377550562300"
5) "r:fd:g1:1377550558977"
6) "r:fd:g1:1377550561344"
7) "r:fd:g1:1377550561832"
8) "r:fd:g1:1377550560344"
9) "r:fd:g1:1377550559978"
10) "r:fd:g1:1377550557777"
11) "r:fd:g1:1377550554258"
12) "r:fd:g1:1377550556772"
13) "r:fd:g1:1377550559649"
14) "r:fd:g1:1377550555460"
15) "r:fd:g1:1377550560895"
16) "r:fd:g1:1377550559139"
17) "r:fd:g1:1377550556595"
18) "r:fd:g1:1377550557634"
How i can get only this keys where timestamp is more then 1377550561300 ?
You can't do this.
But you can use sorted sets and write timestamps as scores, then you'll be able to use
zrangebyscore:
zrangebyscore key 1377550561300) +inf

Why these statement are slow in redis?

I have got following slow query log in redis. I have disable writing to disk. So database is in memory database. I am not able to understand why these two query are slow?
FYI
I have total 462698 hash. Having pattern key:<numeric_number>
1) 1) (integer) 34
2) (integer) 1364981115
3) (integer) 10112
4) 1) "HMGET"
2) "key:123"
3) "is_working"
6) 1) (integer) 29
2) (integer) 1364923711
3) (integer) 87705
4) 1) "HMSET"
2) "key:538771"
3) "status_app"
4) ".. (122246 more bytes)"