/
list_hash.rb
49 lines (39 loc) · 960 Bytes
/
list_hash.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module Picky
module Backends
class Redis
class ListHash < Basic
# Writes the hash into Redis.
#
def dump hash
clear
hash.each_pair do |key, values|
redis_key = "#{namespace}:#{key}"
i = 0
values.each do |value|
i += 1
backend.zadd redis_key, i, value
end
end
end
# Clear the index for this list.
#
# Note: Perhaps we can use a server only command.
# This is not the optimal way to do it.
#
def clear
redis_key = "#{namespace}:*"
backend.keys(redis_key).each do |key|
backend.del key
end
end
# Get a collection.
#
# Internal API method for the index.
#
def [] key
backend.zrange "#{namespace}:#{key}", 0, -1
end
end
end
end
end