Class: Familia::HashKey
- Defined in:
- lib/familia/redistype/types/hashkey.rb
Instance Attribute Summary
Attributes inherited from RedisType
#dump_method, #keystring, #load_method, #opts, #parent
Attributes included from Features
Instance Method Summary collapse
- #[](field) ⇒ Object (also: #get)
-
#[]=(field, val) ⇒ Object
(also: #put, #store)
return[Integer] Returns 1 if the field is new and added, 0 if the field already existed and the value was updated. - #decrement(field, by = 1) ⇒ Object (also: #decr, #decrby)
- #empty? ⇒ Boolean
- #fetch(field, default = nil) ⇒ Object
-
#field_count ⇒ Integer
(also: #size)
Returns the number of fields in the hash.
- #hgetall ⇒ Object (also: #all)
- #increment(field, by = 1) ⇒ Object (also: #incr, #incrby)
- #key?(field) ⇒ Boolean (also: #has_key?, #include?, #member?)
- #keys ⇒ Object
-
#refresh ⇒ self
The friendly neighborhood refresh method!.
-
#refresh! ⇒ void
The Great Redis Refresh-o-matic 3000 for HashKey!.
-
#remove_field(field) ⇒ Integer
(also: #remove)
Removes a field from the hash.
- #update(hsh = {}) ⇒ Object (also: #merge!)
- #values ⇒ Object
- #values_at(*fields) ⇒ Object
Methods inherited from RedisType
#class?, #db, #initialize, #parent?, #parent_class?, #parent_instance?, #redis, #rediskey, #uri
Methods included from Features
Methods included from RedisType::ClassMethods
#db, #has_relations?, #inherited, #register, #uri, #valid_keys_only
Methods included from RedisType::Serialization
#deserialize_value, #deserialize_values, #deserialize_values_with_nil, #serialize_value
Methods included from RedisType::Commands
#delete!, #echo, #exists?, #expire, #expireat, #move, #persist, #realttl, #rename, #renamenx, #type
Methods included from Base
add_feature, #generate_id, #update_expiration, #uuid
Constructor Details
This class inherits a constructor from Familia::RedisType
Instance Method Details
#[](field) ⇒ Object Also known as: get
33 34 35 |
# File 'lib/familia/redistype/types/hashkey.rb', line 33 def [](field) deserialize_value redis.hget(rediskey, field.to_s) end |
#[]=(field, val) ⇒ Object Also known as: put, store
return [Integer] Returns 1 if the field is new and added, 0 if the
field already existed and the value was updated.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/familia/redistype/types/hashkey.rb', line 18 def []=(field, val) ret = redis.hset rediskey, field.to_s, serialize_value(val) update_expiration ret rescue TypeError => e Familia.le "[hset]= #{e.}" Familia.ld "[hset]= #{rediskey} #{field}=#{val}" if Familia.debug echo :hset, caller(1..1).first if Familia.debug # logs via echo to redis and back klass = val.class msg = "Cannot store #{field} => #{val.inspect} (#{klass}) in #{rediskey}" raise e.class, msg end |
#decrement(field, by = 1) ⇒ Object Also known as: decr, decrby
85 86 87 |
# File 'lib/familia/redistype/types/hashkey.rb', line 85 def decrement(field, by = 1) increment field, -by end |
#empty? ⇒ Boolean
12 13 14 |
# File 'lib/familia/redistype/types/hashkey.rb', line 12 def empty? field_count.zero? end |
#fetch(field, default = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/familia/redistype/types/hashkey.rb', line 38 def fetch(field, default = nil) ret = self[field.to_s] if ret.nil? raise IndexError, "No such index for: #{field}" if default.nil? default else ret end end |
#field_count ⇒ Integer Also known as: size
Returns the number of fields in the hash
7 8 9 |
# File 'lib/familia/redistype/types/hashkey.rb', line 7 def field_count redis.hlen rediskey end |
#hgetall ⇒ Object Also known as: all
57 58 59 60 61 |
# File 'lib/familia/redistype/types/hashkey.rb', line 57 def hgetall redis.hgetall(rediskey).each_with_object({}) do |(k,v), ret| ret[k] = deserialize_value v end end |
#increment(field, by = 1) ⇒ Object Also known as: incr, incrby
79 80 81 |
# File 'lib/familia/redistype/types/hashkey.rb', line 79 def increment(field, by = 1) redis.hincrby(rediskey, field.to_s, by).to_i end |
#key?(field) ⇒ Boolean Also known as: has_key?, include?, member?
64 65 66 |
# File 'lib/familia/redistype/types/hashkey.rb', line 64 def key?(field) redis.hexists rediskey, field.to_s end |
#keys ⇒ Object
49 50 51 |
# File 'lib/familia/redistype/types/hashkey.rb', line 49 def keys redis.hkeys rediskey end |
#refresh ⇒ self
The friendly neighborhood refresh method!
This method is like refresh! but with better manners - it returns self so you can chain it with other methods. It’s perfect for when you want to refresh your hash and immediately do something with it.
159 160 161 162 |
# File 'lib/familia/redistype/types/hashkey.rb', line 159 def refresh refresh! self end |
#refresh! ⇒ void
This operation is atomic - it either succeeds completely or fails safely. Any unsaved changes to the hash will be overwritten.
This method returns an undefined value.
The Great Redis Refresh-o-matic 3000 for HashKey!
This method performs a complete refresh of the hash’s state from Redis. It’s like giving your hash a memory transfusion - out with the old state, in with the fresh data straight from Redis!
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/familia/redistype/types/hashkey.rb', line 132 def refresh! Familia.trace :REFRESH, redis, redisuri, caller(1..1) if Familia.debug? raise Familia::KeyNotFoundError, rediskey unless redis.exists(rediskey) fields = hgetall Familia.ld "[refresh!] #{self.class} #{rediskey} #{fields.keys}" # For HashKey, we update by merging the fresh data update(fields) end |
#remove_field(field) ⇒ Integer Also known as: remove
Removes a field from the hash
74 75 76 |
# File 'lib/familia/redistype/types/hashkey.rb', line 74 def remove_field(field) redis.hdel rediskey, field.to_s end |
#update(hsh = {}) ⇒ Object Also known as: merge!
91 92 93 94 95 96 97 98 99 |
# File 'lib/familia/redistype/types/hashkey.rb', line 91 def update(hsh = {}) raise ArgumentError, 'Argument to bulk_set must be a hash' unless hsh.is_a?(Hash) data = hsh.inject([]) { |ret, pair| ret << [pair[0], serialize_value(pair[1])] }.flatten ret = redis.hmset(rediskey, *data) update_expiration ret end |
#values ⇒ Object
53 54 55 |
# File 'lib/familia/redistype/types/hashkey.rb', line 53 def values redis.hvals(rediskey).map { |v| deserialize_value v } end |
#values_at(*fields) ⇒ Object
102 103 104 105 106 |
# File 'lib/familia/redistype/types/hashkey.rb', line 102 def values_at *fields string_fields = fields.flatten.compact.map(&:to_s) elements = redis.hmget(rediskey, *string_fields) deserialize_values(*elements) end |