Class: Redis::HashKey
- Inherits:
-
EnumerableObject
- Object
- BaseObject
- EnumerableObject
- Redis::HashKey
- Defined in:
- lib/redis/hash_key.rb
Overview
Class representing a Redis hash.
Instance Attribute Summary
Attributes inherited from BaseObject
Instance Method Summary collapse
-
#all ⇒ Object
(also: #clone, #value)
Retrieve the entire hash.
-
#bulk_get(*fields) ⇒ Object
Get keys in bulk, takes an array of fields as arguments.
-
#bulk_set(*args) ⇒ Object
(also: #update)
Set keys in bulk, takes a hash of field/values => ‘val1’.
-
#bulk_values(*keys) ⇒ Object
Get values in bulk, takes an array of keys as arguments.
-
#decrby(field, by = 1) ⇒ Object
(also: #decr)
Decrement value by integer at field.
-
#decrbyfloat(field, by = 1.0) ⇒ Object
Decrement value by float at field.
-
#delete(*field) ⇒ Object
Delete 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, *args, &block) ⇒ Object
Fetch a key in a way similar to Ruby’s Hash#fetch.
-
#fill(pairs = {}) ⇒ Object
Set keys in bulk if they do not exist.
-
#has_key?(field) ⇒ Boolean
(also: #include?, #key?, #member?)
Verify that a field exists.
-
#hget(field) ⇒ Object
(also: #get, #[])
Redis: HGET.
-
#incrby(field, by = 1) ⇒ Object
(also: #incr)
Increment value by integer at field.
-
#incrbyfloat(field, by = 1.0) ⇒ Object
Increment value by float at field.
-
#initialize(key, *args) ⇒ HashKey
constructor
A new instance of HashKey.
-
#keys ⇒ Object
Return all the keys of the hash.
-
#size ⇒ Object
(also: #length, #count)
Return the size of the dict.
-
#store(field, value) ⇒ Object
(also: #[]=)
Redis: HSET.
-
#values ⇒ Object
(also: #vals)
Return all the values of the hash.
Methods inherited from EnumerableObject
Methods inherited from BaseObject
#allow_expiration, #as_json, #redis, #set_expiration, #to_hash, #to_json
Methods included from Redis::Helpers::CoreCommands
#exists?, #expire, #expireat, #marshal, #move, #persist, #rename, #renamenx, #serializer, #ttl, #type, #unmarshal
Constructor Details
#initialize(key, *args) ⇒ HashKey
Returns a new instance of HashKey.
8 9 10 11 |
# File 'lib/redis/hash_key.rb', line 8 def initialize(key, *args) super @options[:marshal_keys] ||= {} end |
Instance Method Details
#all ⇒ Object Also known as: clone, value
Retrieve the entire hash. Redis: HGETALL
63 64 65 66 67 |
# File 'lib/redis/hash_key.rb', line 63 def all h = redis.hgetall(key) || {} h.each{|k,v| h[k] = unmarshal(v, [:marshal_keys][k]) } h end |
#bulk_get(*fields) ⇒ Object
Get keys in bulk, takes an array of fields as arguments. Redis: HMGET
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/redis/hash_key.rb', line 115 def bulk_get(*fields) hsh = {} get_fields = *fields.flatten return hsh if get_fields.empty? res = redis.hmget(key, get_fields) get_fields.each do |k| hsh[k] = unmarshal(res.shift, [: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
94 95 96 97 98 99 100 101 |
# File 'lib/redis/hash_key.rb', line 94 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], [: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
128 129 130 131 132 133 |
# File 'lib/redis/hash_key.rb', line 128 def bulk_values(*keys) get_keys = *keys.flatten return [] if get_keys.empty? res = redis.hmget(key, get_keys) get_keys.inject([]){|collection, k| collection << unmarshal(res.shift, [:marshal_keys][k])} end |
#decrby(field, by = 1) ⇒ Object Also known as: decr
Decrement value by integer at field. Redis: HINCRBY
149 150 151 |
# File 'lib/redis/hash_key.rb', line 149 def decrby(field, by=1) incrby(field, -by) end |
#decrbyfloat(field, by = 1.0) ⇒ Object
Decrement value by float at field. Redis: HINCRBYFLOAT
167 168 169 |
# File 'lib/redis/hash_key.rb', line 167 def decrbyfloat(field, by=1.0) incrbyfloat(field, -by) end |
#delete(*field) ⇒ Object
Delete fields. Redis: HDEL
37 38 39 |
# File 'lib/redis/hash_key.rb', line 37 def delete(*field) redis.hdel(key, field) end |
#each_key(&block) ⇒ Object
Enumerate through each keys. Redis: HKEYS
72 73 74 |
# File 'lib/redis/hash_key.rb', line 72 def each_key(&block) keys.each(&block) end |
#each_value(&block) ⇒ Object
Enumerate through all values. Redis: HVALS
77 78 79 |
# File 'lib/redis/hash_key.rb', line 77 def each_value(&block) values.each(&block) end |
#empty? ⇒ Boolean
Returns true if dict is empty
89 90 91 |
# File 'lib/redis/hash_key.rb', line 89 def empty? true if size == 0 end |
#fetch(field, *args, &block) ⇒ Object
Fetch a key in a way similar to Ruby’s Hash#fetch
42 43 44 45 46 47 48 49 |
# File 'lib/redis/hash_key.rb', line 42 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
105 106 107 108 109 110 111 112 |
# File 'lib/redis/hash_key.rb', line 105 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, [:marshal_keys][field])) end end end |
#has_key?(field) ⇒ Boolean Also known as: include?, key?, member?
Verify that a field exists. Redis: HEXISTS
29 30 31 |
# File 'lib/redis/hash_key.rb', line 29 def has_key?(field) redis.hexists(key, field) end |
#hget(field) ⇒ Object Also known as: get, []
Redis: HGET
22 23 24 |
# File 'lib/redis/hash_key.rb', line 22 def hget(field) unmarshal redis.hget(key, field), [:marshal_keys][field] end |
#incrby(field, by = 1) ⇒ Object Also known as: incr
Increment value by integer at field. Redis: HINCRBY
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/redis/hash_key.rb', line 136 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
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/redis/hash_key.rb', line 155 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 |
#keys ⇒ Object
Return all the keys of the hash. Redis: HKEYS
52 53 54 |
# File 'lib/redis/hash_key.rb', line 52 def keys redis.hkeys(key) end |
#size ⇒ Object Also known as: length, count
Return the size of the dict. Redis: HLEN
82 83 84 |
# File 'lib/redis/hash_key.rb', line 82 def size redis.hlen(key) end |
#store(field, value) ⇒ Object Also known as: []=
Redis: HSET
14 15 16 17 18 |
# File 'lib/redis/hash_key.rb', line 14 def store(field, value) allow_expiration do redis.hset(key, field, marshal(value, [:marshal_keys][field])) end end |
#values ⇒ Object Also known as: vals
Return all the values of the hash. Redis: HVALS
57 58 59 |
# File 'lib/redis/hash_key.rb', line 57 def values redis.hvals(key).map{|v| unmarshal(v) } end |