Redis.py - sort hashes by individual fields

I use redis.py and wondered how I can sort by a given field. I read the documentation and tried using Google as an example, but didn't find anything.

In this case, I have a list of times and corresponding temperatures. For a given range of "time", say, from 1000 to 1100, I would return the highest values ​​for temp 'for a given range of times by assigning it to the hightemp variable . Similarly, I would like to do this with lowtemp .

Is it possible to do this in redis, as opposed to re-translating everything back to memory, as it would if I sorted using python

import redis
red = redis.Redis()

red.hmset('temperature', {'time':900, 'temp':123})
red.hmset('temperature', {'time':930, 'temp':123})
red.hmset('temperature', {'time':1000, 'time':121})
red.hmset('temperature', {'time':1030, 'time':125})
red.hmset('temperature', {'time':1100, 'time':126})
red.hmset('temperature', {'time':1130, 'time':127})
red.hmset('temperature', {'time':1200, 'time':128})
+3
2

, , . , , : , time time:temperature ( ). ZRANGEBYSCORE, :

redis 127.0.0.1:6379> zadd temperature 1000 1000:123
(integer) 1
redis 127.0.0.1:6379> zadd temperature 1050 1050:122
(integer) 1
redis 127.0.0.1:6379> zadd temperature 1100 1100:125
(integer) 1
redis 127.0.0.1:6379> zrangebyscore temperature 1000 1100
1) "1000:123"
2) "1050:122"
3) "1100:125"
redis 127.0.0.1:6379> 

, , , . , :

# Result from Redis
result = ['1000:123', '1050:122', '1100:125']
# Weed out the temperatures
temperatures = [int(x.split(':')[1]) for x in result]
# Get max and min temperatures
max_temp, min_temp = max(temperatures), min(temperatures)

, . , , , , SORT . , , !

: python sort() max/min Didier Speza.

+5

, red.hmset, . , , .

: 1, : 2...

redis

SORT mylist BY weight_ * GET _ *

.

, . , .

+2

All Articles