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

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

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

#bulk_get(*fields) ⇒ Object

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



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

def bulk_get(*fields)
  hsh = {}
  res = redis.hmget(key, *fields.flatten)
  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)


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

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



141
142
143
144
# File 'lib/redis/hash_key.rb', line 141

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

#clearObject

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



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

def clear
  redis.del(key)
end

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

Decrement value by integer at field. Redis: HINCRBY



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

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

#decrbyfloat(field, by = 1.0) ⇒ Object

Decrement value by float at field. Redis: HINCRBYFLOAT



178
179
180
# File 'lib/redis/hash_key.rb', line 178

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



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

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

#each_key(&block) ⇒ Object

Enumerate through each keys. Redis: HKEYS



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

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

#each_value(&block) ⇒ Object

Enumerate through all values. Redis: HVALS



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

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

#empty?Boolean

Returns true if dict is empty

Returns:

  • (Boolean)


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

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)


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

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



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

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



166
167
168
169
170
171
172
173
174
175
# File 'lib/redis/hash_key.rb', line 166

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



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

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