Class: Familia::HashKey
- Defined in:
- lib/familia/data_type/types/hashkey.rb
Instance Attribute Summary collapse
-
#features_enabled ⇒ Object
included
from Features
readonly
Returns the value of attribute features_enabled.
- #logical_database(val = nil) ⇒ Object included from DataType::ClassMethods
-
#parent ⇒ Object
included
from DataType::ClassMethods
Returns the value of attribute parent.
-
#prefix ⇒ Object
included
from DataType::ClassMethods
Returns the value of attribute prefix.
-
#suffix ⇒ Object
included
from DataType::ClassMethods
Returns the value of attribute suffix.
-
#uri(val = nil) ⇒ Object
included
from DataType::ClassMethods
Returns the value of attribute uri.
Instance Method Summary collapse
- #[](field) ⇒ Object (also: #get)
-
#[]=(field, val) ⇒ Object
(also: #put, #store, #add)
+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, #length, #count)
Returns the number of fields in the hash.
- #hgetall ⇒ Object (also: #all)
-
#hsetnx(field, val) ⇒ Integer
Sets field in the hash stored at key to value, only if field does not yet exist.
- #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 Database 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 included from Features::Autoloader
autoload_files, included, normalize_to_config_name
Methods included from DataType::Serialization
#deserialize_value, #deserialize_values, #deserialize_values_with_nil, #serialize_value
Methods included from DataType::Commands
#current_expiration, #delete!, #echo, #exists?, #expire, #expireat, #move, #persist, #rename, #renamenx, #type
Methods included from Base
add_feature, #as_json, #expired?, #expires?, find_feature, #generate_id, #to_json, #to_s, #ttl, #update_expiration, #uuid
Constructor Details
This class inherits a constructor from Familia::DataType
Instance Attribute Details
#features_enabled ⇒ Object (readonly) Originally defined in module Features
Returns the value of attribute features_enabled.
#logical_database(val = nil) ⇒ Object Originally defined in module DataType::ClassMethods
#parent ⇒ Object Originally defined in module DataType::ClassMethods
Returns the value of attribute parent.
#prefix ⇒ Object Originally defined in module DataType::ClassMethods
Returns the value of attribute prefix.
#suffix ⇒ Object Originally defined in module DataType::ClassMethods
Returns the value of attribute suffix.
#uri(val = nil) ⇒ Object Originally defined in module DataType::ClassMethods
Returns the value of attribute uri.
Instance Method Details
#[](field) ⇒ Object Also known as: get
36 37 38 |
# File 'lib/familia/data_type/types/hashkey.rb', line 36 def [](field) deserialize_value dbclient.hget(dbkey, field.to_s) end |
#[]=(field, val) ⇒ Object Also known as: put, store, add
+return+ [Integer] Returns 1 if the field is new and added, 0 if the field already existed and the value was updated.
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/familia/data_type/types/hashkey.rb', line 20 def []=(field, val) ret = dbclient.hset dbkey, field.to_s, serialize_value(val) update_expiration ret rescue TypeError => e Familia.le "[hset]= #{e.}" Familia.ld "[hset]= #{dbkey} #{field}=#{val}" if Familia.debug echo :hset, Familia.pretty_stack(limit: 1) if Familia.debug # logs via echo to the db and back klass = val.class msg = "Cannot store #{field} => #{val.inspect} (#{klass}) in #{dbkey}" raise e.class, msg end |
#decrement(field, by = 1) ⇒ Object Also known as: decr, decrby
106 107 108 |
# File 'lib/familia/data_type/types/hashkey.rb', line 106 def decrement(field, by = 1) increment field, -by end |
#empty? ⇒ Boolean
14 15 16 |
# File 'lib/familia/data_type/types/hashkey.rb', line 14 def empty? field_count.zero? end |
#fetch(field, default = nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/familia/data_type/types/hashkey.rb', line 41 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, length, count
Returns the number of fields in the hash
7 8 9 |
# File 'lib/familia/data_type/types/hashkey.rb', line 7 def field_count dbclient.hlen dbkey end |
#hgetall ⇒ Object Also known as: all
60 61 62 63 64 |
# File 'lib/familia/data_type/types/hashkey.rb', line 60 def hgetall dbclient.hgetall(dbkey).transform_values do |v| deserialize_value v end end |
#hsetnx(field, val) ⇒ Integer
Sets field in the hash stored at key to value, only if field does not yet exist. If field already exists, this operation has no effect.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/familia/data_type/types/hashkey.rb', line 72 def hsetnx(field, val) ret = dbclient.hsetnx dbkey, field.to_s, serialize_value(val) update_expiration if ret == 1 ret rescue TypeError => e Familia.le "[hsetnx] #{e.}" Familia.ld "[hsetnx] #{dbkey} #{field}=#{val}" if Familia.debug echo :hsetnx, Familia.pretty_stack(limit: 1) if Familia.debug # logs via echo to the db and back klass = val.class msg = "Cannot store #{field} => #{val.inspect} (#{klass}) in #{dbkey}" raise e.class, msg end |
#increment(field, by = 1) ⇒ Object Also known as: incr, incrby
100 101 102 |
# File 'lib/familia/data_type/types/hashkey.rb', line 100 def increment(field, by = 1) dbclient.hincrby(dbkey, field.to_s, by).to_i end |
#key?(field) ⇒ Boolean Also known as: has_key?, include?, member?
85 86 87 |
# File 'lib/familia/data_type/types/hashkey.rb', line 85 def key?(field) dbclient.hexists dbkey, field.to_s end |
#keys ⇒ Object
52 53 54 |
# File 'lib/familia/data_type/types/hashkey.rb', line 52 def keys dbclient.hkeys dbkey 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.
180 181 182 183 |
# File 'lib/familia/data_type/types/hashkey.rb', line 180 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 Database Refresh-o-matic 3000 for HashKey!
This method performs a complete refresh of the hash's state from the database. It's like giving your hash a memory transfusion - out with the old state, in with the fresh data straight from Valkey/Redis!
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/familia/data_type/types/hashkey.rb', line 153 def refresh! Familia.trace :REFRESH, nil, uri if Familia.debug? raise Familia::KeyNotFoundError, dbkey unless dbclient.exists(dbkey) fields = hgetall Familia.ld "[refresh!] #{self.class} #{dbkey} #{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
95 96 97 |
# File 'lib/familia/data_type/types/hashkey.rb', line 95 def remove_field(field) dbclient.hdel dbkey, field.to_s end |
#update(hsh = {}) ⇒ Object Also known as: merge!
112 113 114 115 116 117 118 119 120 |
# File 'lib/familia/data_type/types/hashkey.rb', line 112 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 = dbclient.hmset(dbkey, *data) update_expiration ret end |
#values ⇒ Object
56 57 58 |
# File 'lib/familia/data_type/types/hashkey.rb', line 56 def values dbclient.hvals(dbkey).map { |v| deserialize_value v } end |
#values_at(*fields) ⇒ Object
123 124 125 126 127 |
# File 'lib/familia/data_type/types/hashkey.rb', line 123 def values_at *fields string_fields = fields.flatten.compact.map(&:to_s) elements = dbclient.hmget(dbkey, *string_fields) deserialize_values(*elements) end |