Class: Redis::HashKey
- Inherits:
-
BaseObject
- Object
- BaseObject
- Redis::HashKey
- 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
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
-
#[](field) ⇒ Object
Gets the value of a field.
-
#[]=(field, value) ⇒ Object
Sets a field to value.
-
#all ⇒ Object
(also: #clone)
Retrieve the entire hash.
-
#bulk_get(*fields) ⇒ Object
Get keys in bulk, takes an array of fields as arguments.
-
#bulk_set(*args) ⇒ Object
Set keys in bulk, takes a hash of field/values => ‘val1’.
-
#clear ⇒ Object
Clears the dict of all keys/values.
-
#delete(field) ⇒ Object
Delete field.
-
#each(&block) ⇒ Object
Enumerate through all fields.
-
#each_key(&block) ⇒ Object
Enumerate through each keys.
-
#each_value(&block) ⇒ Object
Enumerate through all values.
-
#empty? ⇒ Boolean
Returns true if dict is empty.
-
#fetch(field) ⇒ Object
Redis: HGET.
-
#has_key?(field) ⇒ Boolean
(also: #include?, #key?, #member?)
Verify that a field exists.
-
#incrby(field, val = 1) ⇒ Object
(also: #incr)
Increment value by integer at field.
-
#keys ⇒ Object
Return all the keys of the hash.
-
#size ⇒ Object
(also: #length, #count)
Return the size of the dict.
-
#store(field, value) ⇒ Object
Redis: HSET.
-
#values ⇒ Object
(also: #vals)
Return all the values of the hash.
Methods included from Redis::Helpers::Serialize
Methods included from Redis::Helpers::CoreCommands
#exists?, #expire, #expireat, #move, #rename, #renamenx, #type
Methods inherited from BaseObject
Constructor Details
This class inherits a constructor from Redis::BaseObject
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
13 14 15 |
# File 'lib/redis/hash_key.rb', line 13 def key @key end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/redis/hash_key.rb', line 13 def @options end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
13 14 15 |
# File 'lib/redis/hash_key.rb', line 13 def redis @redis end |
Instance Method Details
#[](field) ⇒ Object
Gets the value of a field
21 22 23 |
# File 'lib/redis/hash_key.rb', line 21 def [](field) fetch(field) end |
#[]=(field, value) ⇒ Object
Sets a field to value
16 17 18 |
# File 'lib/redis/hash_key.rb', line 16 def []=(field, value) store(field, to_redis(value)) end |
#all ⇒ Object Also known as: clone
Retrieve the entire hash. Redis: HGETALL
60 61 62 |
# File 'lib/redis/hash_key.rb', line 60 def all from_redis redis.hgetall(key) end |
#bulk_get(*fields) ⇒ Object
Get keys in bulk, takes an array of fields as arguments. Redis: HMGET
104 105 106 107 108 109 110 111 |
# File 'lib/redis/hash_key.rb', line 104 def bulk_get(*fields) hsh = {} res = redis.hmget(key, *fields.flatten) fields.each do |k| hsh[k] = from_redis(res.shift) end hsh end |
#bulk_set(*args) ⇒ Object
Set keys in bulk, takes a hash of field/values => ‘val1’. Redis: HMSET
98 99 100 101 |
# File 'lib/redis/hash_key.rb', line 98 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])] }) end |
#clear ⇒ Object
Clears the dict of all keys/values. Redis: DEL
93 94 95 |
# File 'lib/redis/hash_key.rb', line 93 def clear redis.del(key) end |
#delete(field) ⇒ Object
Delete field. Redis: HDEL
44 45 46 |
# File 'lib/redis/hash_key.rb', line 44 def delete(field) redis.hdel(key, field) end |
#each(&block) ⇒ Object
Enumerate through all fields. Redis: HGETALL
66 67 68 |
# File 'lib/redis/hash_key.rb', line 66 def each(&block) all.each(&block) end |
#each_key(&block) ⇒ Object
Enumerate through each keys. Redis: HKEYS
71 72 73 |
# File 'lib/redis/hash_key.rb', line 71 def each_key(&block) keys.each(&block) end |
#each_value(&block) ⇒ Object
Enumerate through all values. Redis: HVALS
76 77 78 |
# File 'lib/redis/hash_key.rb', line 76 def each_value(&block) values.each(&block) end |
#empty? ⇒ Boolean
Returns true if dict is empty
88 89 90 |
# File 'lib/redis/hash_key.rb', line 88 def empty? true if size == 0 end |
#fetch(field) ⇒ Object
Redis: HGET
31 32 33 |
# File 'lib/redis/hash_key.rb', line 31 def fetch(field) from_redis redis.hget(key, field) end |
#has_key?(field) ⇒ Boolean Also known as: include?, key?, member?
Verify that a field exists. Redis: HEXISTS
36 37 38 |
# File 'lib/redis/hash_key.rb', line 36 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
114 115 116 |
# File 'lib/redis/hash_key.rb', line 114 def incrby(field, val = 1) redis.hincrby(key, field, val).to_i end |
#keys ⇒ Object
Return all the keys of the hash. Redis: HKEYS
49 50 51 |
# File 'lib/redis/hash_key.rb', line 49 def keys redis.hkeys(key) end |
#size ⇒ Object Also known as: length, count
Return the size of the dict. Redis: HLEN
81 82 83 |
# File 'lib/redis/hash_key.rb', line 81 def size redis.hlen(key) end |
#store(field, value) ⇒ Object
Redis: HSET
26 27 28 |
# File 'lib/redis/hash_key.rb', line 26 def store(field, value) redis.hset(key, field, to_redis(value)) end |
#values ⇒ Object Also known as: vals
Return all the values of the hash. Redis: HVALS
54 55 56 |
# File 'lib/redis/hash_key.rb', line 54 def values from_redis redis.hvals(key) end |