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, #rename, #renamenx, #sort, #ttl, #type

Constructor Details

#initialize(key, *args) ⇒ HashKey

Returns a new instance of HashKey.



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

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

#redisObject (readonly)

Returns the value of attribute redis.



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

def redis
  @redis
end

Class Method Details

.[](*args) ⇒ Object

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



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

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

Instance Method Details

#[](field) ⇒ Object

Gets the value of a field



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

def [](field)
  fetch(field)
end

#[]=(field, value) ⇒ Object

Sets a field to value



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

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

#allObject Also known as: clone

Retrieve the entire hash. Redis: HGETALL



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

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



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

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

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

Raises:

  • (ArgumentError)


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

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

#clearObject

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



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

def clear
  redis.del(key)
end

#delete(field) ⇒ Object

Delete field. Redis: HDEL



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

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

#each(&block) ⇒ Object

Enumerate through all fields. Redis: HGETALL



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

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

#each_key(&block) ⇒ Object

Enumerate through each keys. Redis: HKEYS



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

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

#each_value(&block) ⇒ Object

Enumerate through all values. Redis: HVALS



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

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

#empty?Boolean

Returns true if dict is empty

Returns:

  • (Boolean)


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

def empty?
  true if size == 0
end

#fetch(field) ⇒ Object

Redis: HGET



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

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

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

Verify that a field exists. Redis: HEXISTS

Returns:

  • (Boolean)


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

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



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

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



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

def keys
  redis.hkeys(key)
end

#sizeObject Also known as: length, count

Return the size of the dict. Redis: HLEN



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

def size
  redis.hlen(key)
end

#store(field, value) ⇒ Object

Redis: HSET



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

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



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

def values
  from_redis redis.hvals(key)
end