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

expiration_filter, #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



67
68
69
70
71
# File 'lib/redis/hash_key.rb', line 67

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



124
125
126
127
128
129
130
131
# File 'lib/redis/hash_key.rb', line 124

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)


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

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



135
136
137
138
# File 'lib/redis/hash_key.rb', line 135

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



102
103
104
# File 'lib/redis/hash_key.rb', line 102

def clear
  redis.del(key)
end

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

Decrement value by integer at field. Redis: HINCRBY



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

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

#decrbyfloat(field, by = 1.0) ⇒ Object

Decrement value by float at field. Redis: HINCRBYFLOAT



168
169
170
# File 'lib/redis/hash_key.rb', line 168

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

#delete(field) ⇒ Object

Delete field. Redis: HDEL



41
42
43
# File 'lib/redis/hash_key.rb', line 41

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

#each(&block) ⇒ Object

Enumerate through all fields. Redis: HGETALL



75
76
77
# File 'lib/redis/hash_key.rb', line 75

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

#each_key(&block) ⇒ Object

Enumerate through each keys. Redis: HKEYS



80
81
82
# File 'lib/redis/hash_key.rb', line 80

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

#each_value(&block) ⇒ Object

Enumerate through all values. Redis: HVALS



85
86
87
# File 'lib/redis/hash_key.rb', line 85

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

#empty?Boolean

Returns true if dict is empty

Returns:

  • (Boolean)


97
98
99
# File 'lib/redis/hash_key.rb', line 97

def empty?
  true if size == 0
end

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

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



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

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)


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

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

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

Verify that a field exists. Redis: HEXISTS

Returns:

  • (Boolean)


33
34
35
# File 'lib/redis/hash_key.rb', line 33

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

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

Redis: HGET



26
27
28
# File 'lib/redis/hash_key.rb', line 26

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



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

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

#incrbyfloat(field, by = 1.0) ⇒ Object

Increment value by float at field. Redis: HINCRBYFLOAT



158
159
160
161
162
163
164
165
# File 'lib/redis/hash_key.rb', line 158

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

#keysObject

Return all the keys of the hash. Redis: HKEYS



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

def keys
  redis.hkeys(key)
end

#sizeObject Also known as: length, count

Return the size of the dict. Redis: HLEN



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

def size
  redis.hlen(key)
end

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

Redis: HSET



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

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

#valuesObject Also known as: vals

Return all the values of the hash. Redis: HVALS



61
62
63
# File 'lib/redis/hash_key.rb', line 61

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