Class: Redis::HashKey

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

Overview

Class representing a Redis hash.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Redis::Helpers::Serialize

#from_redis, #to_redis

Methods included from Redis::Helpers::CoreCommands

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

Methods inherited from BaseObject

#redis

Constructor Details

#initialize(key, *args) ⇒ HashKey



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

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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.[](*args) ⇒ Object

Needed since Redis::Hash masks bare Hash in redis.rb



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

def self.[](*args)
  ::Hash[*args]
end

Instance Method Details

#[](field) ⇒ Object

Gets the value of a field



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

def [](field)
  fetch(field)
end

#[]=(field, value) ⇒ Object

Sets a field to value



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

def []=(field, value)
  store(field, to_redis(value))
end

#allObject Also known as: clone

Retrieve the entire hash. Redis: HGETALL



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

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

#bulk_get(*fields) ⇒ Object

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



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

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


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

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], to_redis(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



139
140
141
142
# File 'lib/redis/hash_key.rb', line 139

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

#clearObject

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



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

def clear
  redis.del(key)
end

#delete(field) ⇒ Object

Delete field. Redis: HDEL



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

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

#each(&block) ⇒ Object

Enumerate through all fields. Redis: HGETALL



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

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

#each_key(&block) ⇒ Object

Enumerate through each keys. Redis: HKEYS



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

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

#each_value(&block) ⇒ Object

Enumerate through all values. Redis: HVALS



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

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

#empty?Boolean

Returns true if dict is empty



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

def empty?
  true if size == 0
end

#fetch(field) ⇒ Object

Redis: HGET



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

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

#fill(pairs = {}) ⇒ Object

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

Raises:

  • (ArgumentErorr)


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

def fill(pairs={})
  raise ArgumentErorr, "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, to_redis(value, options[:marshal_keys][field]))
  end
end

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

Verify that a field exists. Redis: HEXISTS



47
48
49
# File 'lib/redis/hash_key.rb', line 47

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

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

Increment value by integer at field. Redis: HINCRBY



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

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

#keysObject

Return all the keys of the hash. Redis: HKEYS



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

def keys
  redis.hkeys(key)
end

#sizeObject Also known as: length, count

Return the size of the dict. Redis: HLEN



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

def size
  redis.hlen(key)
end

#store(field, value) ⇒ Object

Redis: HSET



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

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

#valuesObject Also known as: vals

Return all the values of the hash. Redis: HVALS



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

def values
  from_redis redis.hvals(key)
end