Redis: turn off feeds in a list or sorted set?

I am doing caching news feeds with Redis as follows:

each channel activity is a key / value, for example activity: id, where the value is a JSON data string.

each news channel is currently a list, the key is a feed: user: user_id, and the list contains the keys for the corresponding actions.

to get the feed I am using, for example: 'sort feed: user: user_id by nosort get * limit 0 40'

I’m considering changing the feed to a sorted set, where the rating is an activity label, so the channel is always sorted by time.

I read http://arindam.quora.com/Redis-sorted-sets-and-lists-Pertaining-to-Newsfeed , which recommend using lists because of the time complexity of sorted sets, but keep using lists that I need to take care of order pasting, pasting, past history requires iterating over the list and searching for the desired index to click on. (which may cause new problems in distributed environments).

Should I continue to use lists or go for sorted sets?

is there a way to instantly get a feed from a sorted set (for example, using the sort ... get * command for a list) or it should be zrange and then iterate over the results and get each value

+4
source share
1 answer

, . , SORT. . O (log (N)) , . . - .

ZRANGEBYSCORE key min max WITHSCORES [LIMIT offset count], .

, "", ZREVRANGEBYSCORE .

: SCORES, , 15 . , SCORE -999999999999999 999999999999999. . , Redis (float) redis-string.

, Zulu Time: -20140313122802 . 1 100 , , . - float64, , " ", .

10 , (CCY CCYY), 10000 .

, ZRANGEBYSCORE REV. -inf ( ) LIMIT 0 100, 100 .

members ( 'keys', , ) SCORE, , SCORE .

, , TW

OP ( ZSET) (GET/SET HGET/HSET). JOIN , ZRANGEBYSCORE . , Lua script. Lua script . EVAL , SCRIPT EXISTS, SCRIPT LOAD EVALSHA. , script .

example.lua:

local r={}
local zkey=KEYS[1]
local a=redis.call('zrangebyscore', zkey, KEYS[2], KEYS[3], 'withscores', 'limit', 0, KEYS[4])
for i=1,#a,2 do
  r[i]=a[i+1]
  r[i+1]=redis.call('get', a[i])
end
return r

( , ):

redis-cli -p 14322 set activity:1 act1JSON
redis-cli -p 14322 set activity:2 act2JSON
redis-cli -p 14322 zadd feed 1 activity:1
redis-cli -p 14322 zadd feed 2 activity:2 

redis-cli -p 14322 eval '$(cat example.lua)' 4 feed '-inf' '+inf' 100

:

1) "1"
2) "act1JSON"
3) "2"
4) "act2JSON"
+3

Source: https://habr.com/ru/post/1531543/


All Articles