Class: Redis::HashKey

Inherits:
BaseObject show all
Includes:
Enumerable, Redis::Helpers::CoreCommands
Defined in:
lib/redis/hash_key.rb

Overview

Class representing a Redis hash.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Redis::Helpers::CoreCommands

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

Methods inherited from BaseObject

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

Constructor Details

#initialize(key, *args) ⇒ HashKey

Returns a new instance of HashKey.


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

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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key


13
14
15
# File 'lib/redis/hash_key.rb', line 13

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options


13
14
15
# File 'lib/redis/hash_key.rb', line 13

def options
  @options
end

Instance Method Details

#allObject Also known as: clone, value

Retrieve the entire hash. Redis: HGETALL


69
70
71
72
73
# File 'lib/redis/hash_key.rb', line 69

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

#as_jsonObject


187
188
189
# File 'lib/redis/hash_key.rb', line 187

def as_json(*)
  to_hash
end

#bulk_get(*fields) ⇒ Object

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


131
132
133
134
135
136
137
138
139
140
# File 'lib/redis/hash_key.rb', line 131

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)

110
111
112
113
114
115
116
117
# File 'lib/redis/hash_key.rb', line 110

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(*keys) ⇒ Object

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


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

def bulk_values(*keys)
  get_keys = *keys.flatten
  return [] if get_keys.empty?
  res = redis.hmget(key, get_keys)
  get_keys.inject([]){|collection, k| collection << unmarshal(res.shift, options[:marshal_keys][k])}
end

#clearObject

Clears the dict of all keys/values. Redis: DEL


105
106
107
# File 'lib/redis/hash_key.rb', line 105

def clear
  redis.del(key)
end

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

Decrement value by integer at field. Redis: HINCRBY


165
166
167
# File 'lib/redis/hash_key.rb', line 165

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

#decrbyfloat(field, by = 1.0) ⇒ Object

Decrement value by float at field. Redis: HINCRBYFLOAT


183
184
185
# File 'lib/redis/hash_key.rb', line 183

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

#delete(*field) ⇒ Object

Delete fields. Redis: HDEL


43
44
45
# File 'lib/redis/hash_key.rb', line 43

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

#each(&block) ⇒ Object

Enumerate through all fields. Redis: HGETALL


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

def each(&block)
  all.each(&block)
end

#each_key(&block) ⇒ Object

Enumerate through each keys. Redis: HKEYS


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

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

#each_value(&block) ⇒ Object

Enumerate through all values. Redis: HVALS


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

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

#empty?Boolean

Returns true if dict is empty

Returns:

  • (Boolean)

100
101
102
# File 'lib/redis/hash_key.rb', line 100

def empty?
  true if size == 0
end

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

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


48
49
50
51
52
53
54
55
# File 'lib/redis/hash_key.rb', line 48

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)

121
122
123
124
125
126
127
128
# File 'lib/redis/hash_key.rb', line 121

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)

35
36
37
# File 'lib/redis/hash_key.rb', line 35

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

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

Redis: HGET


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

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


152
153
154
155
156
157
158
159
160
161
# File 'lib/redis/hash_key.rb', line 152

def incrby(field, by=1)
  allow_expiration do
    ret = redis.hincrby(key, field, by)
    unless ret.is_a? Array
      ret.to_i
    else
      nil
    end
  end
end

#incrbyfloat(field, by = 1.0) ⇒ Object

Increment value by float at field. Redis: HINCRBYFLOAT


171
172
173
174
175
176
177
178
179
180
# File 'lib/redis/hash_key.rb', line 171

def incrbyfloat(field, by=1.0)
  allow_expiration do
    ret = redis.hincrbyfloat(key, field, by)
    unless ret.is_a? Array
      ret.to_f
    else
      nil
    end
  end
end

#keysObject

Return all the keys of the hash. Redis: HKEYS


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

def keys
  redis.hkeys(key)
end

#sizeObject Also known as: length, count

Return the size of the dict. Redis: HLEN


93
94
95
# File 'lib/redis/hash_key.rb', line 93

def size
  redis.hlen(key)
end

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

Redis: HSET


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

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


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

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