hgetsmembers

Look up the contents of a Set in a Hash

Returns Array of values
Keys
  1. Hash
  2. Set containing fields from the Hash
Args None

Description

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.

Example

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

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

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

SHA

700c06c5ce9835bf9eef2198c8bc4d268b3b5095

Explore Your Redis Memory Usage with an Interactive Map

Memetria shows you large keys, memory leaks, old keys, and other potential problems quickly. Try it today without a credit card.

Last updated 30 Jul 2014.

← More scripts from the Memetria Library

License

All scripts on this site are in the public domain, unless otherwise noted, and are provided without warranty express or implied. Memetria make no claim for the effectiveness, safety, security or quality of the scripts contained within.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.