Class: Redis::HashKey

Inherits:
EnumerableObject show all
Defined in:
lib/redis/hash_key.rb

Overview

Class representing a Redis hash.

Instance Attribute Summary

Attributes inherited from BaseObject

#key, #options

Instance Method Summary collapse

Methods inherited from EnumerableObject

#as_json, #each, #sort

Methods inherited from BaseObject

#allow_expiration, #as_json, #redis, #set_expiration, #to_hash, #to_json

Methods included from Redis::Helpers::CoreCommands

#exists, #exists?, #expire, #expireat, #marshal, #move, #persist, #rename, #renamenx, #serializer, #ttl, #type, #unmarshal

Constructor Details

#initialize(key, *args) ⇒ HashKey

Returns a new instance of HashKey.



8
9
10
11
# File 'lib/redis/hash_key.rb', line 8

def initialize(key, *args)
  super
  @options[:marshal_keys] ||= {}
end

Instance Method Details

#allObject Also known as: clone, value

Retrieve the entire hash. Redis: HGETALL



63
64
65
66
67
# File 'lib/redis/hash_key.rb', line 63

def all
  h = redis.hgetall(key) || {}
  h.each{|k,v| h[k] = unmarshal(v, options[:marshal_keys][k]) }
  h
end

#bulk_get(*fields) ⇒ Object

Get keys in bulk, takes an array of fields as arguments. Redis: HMGET



115
116
117
118
119
120
121
122
123
124
# File 'lib/redis/hash_key.rb', line 115

def bulk_get(*fields)
  hsh = {}
  get_fields = *fields.flatten
  return hsh if get_fields.empty?
  res = redis.hmget(key, get_fields)
  get_fields.each do |k|
    hsh[k] = unmarshal(res.shift, options[:marshal_keys][k])
  end
  hsh
end

#bulk_set(*args) ⇒ Object Also known as: update

Set keys in bulk, takes a hash of field/values => ‘val1’. Redis: HMSET

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
101
# File 'lib/redis/hash_key.rb', line 94

def bulk_set(*args)
  raise ArgumentError, "Argument to bulk_set must be hash of key/value pairs" unless args.last.is_a?(::Hash)
  allow_expiration do
    redis.hmset(key, *args.last.inject([]){ |arr,kv|
      arr + [kv[0], marshal(kv[1], options[:marshal_keys][kv[0]])]
    })
  end
end

#bulk_values(*fields) ⇒ Object

Get values in bulk, takes an array of fields as arguments. Values are returned in a collection in the same order than their keys in *keys Redis: HMGET



128
129
130
131
132
133
# File 'lib/redis/hash_key.rb', line 128

def bulk_values(*fields)
  get_fields = *fields.flatten
  return [] if get_fields.empty?
  res = redis.hmget(key, get_fields)
  get_fields.collect{|k| unmarshal(res.shift, options[:marshal_keys][k])}
end

#decrby(field, by = 1) ⇒ Object Also known as: decr

Decrement value by integer at field. Redis: HINCRBY



145
146
147
# File 'lib/redis/hash_key.rb', line 145

def decrby(field, by=1)
  incrby(field, -by)
end

#decrbyfloat(field, by = 1.0) ⇒ Object

Decrement value by float at field. Redis: HINCRBYFLOAT



159
160
161
# File 'lib/redis/hash_key.rb', line 159

def decrbyfloat(field, by=1.0)
  incrbyfloat(field, -by)
end

#delete(*field) ⇒ Object

Delete fields. Redis: HDEL



37
38
39
# File 'lib/redis/hash_key.rb', line 37

def delete(*field)
  redis.hdel(key, field)
end

#each_key(&block) ⇒ Object

Enumerate through each keys. Redis: HKEYS



72
73
74
# File 'lib/redis/hash_key.rb', line 72

def each_key(&block)
  keys.each(&block)
end

#each_value(&block) ⇒ Object

Enumerate through all values. Redis: HVALS



77
78
79
# File 'lib/redis/hash_key.rb', line 77

def each_value(&block)
  values.each(&block)
end

#empty?Boolean

Returns true if dict is empty

Returns:

  • (Boolean)


89
90
91
# File 'lib/redis/hash_key.rb', line 89

def empty?
  true if size == 0
end

#fetch(field, *args, &block) ⇒ Object

Fetch a key in a way similar to Ruby’s Hash#fetch



42
43
44
45
46
47
48
49
# File 'lib/redis/hash_key.rb', line 42

def fetch(field, *args, &block)
  value = hget(field)
  default = args[0]

  return value if value || (!default && !block_given?)

  block_given? ? block.call(field) : default
end

#fill(pairs = {}) ⇒ Object

Set keys in bulk if they do not exist. Takes a hash of field/values => ‘val1’. Redis: HSETNX

Raises:

  • (ArgumentError)


105
106
107
108
109
110
111
112
# File 'lib/redis/hash_key.rb', line 105

def fill(pairs={})
  raise ArgumentError, "Argument to fill must be a hash of key/value pairs" unless pairs.is_a?(::Hash)
  allow_expiration do
    pairs.each do |field, value|
      redis.hsetnx(key, field, marshal(value, options[:marshal_keys][field]))
    end
  end
end

#has_key?(field) ⇒ Boolean Also known as: include?, key?, member?

Verify that a field exists. Redis: HEXISTS

Returns:

  • (Boolean)


29
30
31
# File 'lib/redis/hash_key.rb', line 29

def has_key?(field)
  redis.hexists(key, field)
end

#hget(field) ⇒ Object Also known as: get, []

Redis: HGET



22
23
24
# File 'lib/redis/hash_key.rb', line 22

def hget(field)
  unmarshal redis.hget(key, field), options[:marshal_keys][field]
end

#incrby(field, by = 1) ⇒ Object Also known as: incr

Increment value by integer at field. Redis: HINCRBY



136
137
138
139
140
141
# File 'lib/redis/hash_key.rb', line 136

def incrby(field, by=1)
  allow_expiration do
    ret = redis.hincrby(key, field, by)
    ret.to_i
  end
end

#incrbyfloat(field, by = 1.0) ⇒ Object

Increment value by float at field. Redis: HINCRBYFLOAT



151
152
153
154
155
156
# File 'lib/redis/hash_key.rb', line 151

def incrbyfloat(field, by=1.0)
  allow_expiration do
    ret = redis.hincrbyfloat(key, field, by)
    ret.to_f
  end
end

#keysObject

Return all the keys of the hash. Redis: HKEYS



52
53
54
# File 'lib/redis/hash_key.rb', line 52

def keys
  redis.hkeys(key)
end

#sizeObject Also known as: length, count

Return the size of the dict. Redis: HLEN



82
83
84
# File 'lib/redis/hash_key.rb', line 82

def size
  redis.hlen(key)
end

#store(field, value) ⇒ Object Also known as: []=

Redis: HSET



14
15
16
17
18
# File 'lib/redis/hash_key.rb', line 14

def store(field, value)
  allow_expiration do
    redis.hset(key, field, marshal(value, options[:marshal_keys][field]))
  end
end

#valuesObject Also known as: vals

Return all the values of the hash. Redis: HVALS



57
58
59
# File 'lib/redis/hash_key.rb', line 57

def values
  redis.hvals(key).map{|v| unmarshal(v) }
end