-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhgetsmembers.html
60 lines (50 loc) · 1.44 KB
/
hgetsmembers.html
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
50
51
52
53
54
55
56
57
58
59
60
---
title: hgetsmembers
brief: Look up the contents of a Set in a Hash
updated: 2014-07-30 18:44
layout: script
sha: 700c06c5ce9835bf9eef2198c8bc4d268b3b5095
return: Array - a list of values
---
<strong>Keys: 2</strong>
<ol class="args">
<li>a Hash</li>
<li>a Set containing fields from the Hash</li>
</ol>
<strong>Args: None</strong><br>
<h2>Description</h2>
This script can be used to pull part of a Hash, based on values in a Set.
This is useful when you want to maintain a particular Set of values for a
specific purpose from a larger Hash object.
<h2>Example</h2>
<p>The `groceries` Hash maintains counts of all the groceries we need. The
`produce` Set keeps track of the fields in `groceries` that refer to fruits and
vegetables. We can use this Lua script to pull the counts of just the produce
from `groceries`.</p>
<pre class="script-example">
> hset groceries bread 2
(integer) 1
> hset groceries apples 5
(integer) 1
> hset groceries oranges 6
(integer) 1
> hset groceries broccoli 1
(integer) 1
> sadd produce apples oranges broccoli
(integer) 3
> evalsha 700c06c5ce9835bf9eef2198c8bc4d268b3b5095 2 groceries produce
1) 1) "apples"
2) "5"
2) 1) "broccoli"
2) "1"
3) 1) "oranges"
2) "6"
</pre>
<h2>Source</h2>
<pre class="script-source">
local fields = redis.call("SMEMBERS", KEYS[2])
local values = redis.call("HMGET", KEYS[1], unpack(fields))
local result = {}
for i,k in ipairs(fields) do result[i] = {k, values[i]} end
return result
</pre>