Class: Redstruct::Hash
- Includes:
- Utils::Iterable
- Defined in:
- lib/redstruct/hash.rb
Overview
Class to manipulate redis hashes, modeled after Ruby's Hash class.
Instance Attribute Summary
Attributes inherited from Struct
Instance Method Summary collapse
-
#[](key) ⇒ nil, String
Returns the value at key.
-
#[]=(key, value) ⇒ Boolean
Sets or updates the value at key.
-
#decrement(key, by: 1) ⇒ Integer, Float
Decrements the value at the given key.
-
#empty? ⇒ Boolean
True if the hash contains no elements.
-
#get(*keys) ⇒ Hash<String, String>
Returns the value at key.
-
#increment(key, by: 1) ⇒ Integer, Float
Increments the value at the given key.
-
#key?(key) ⇒ Boolean
Checks if a key has a value.
-
#keys ⇒ Array<String>
A list of all hash keys with values associated.
-
#remove(*keys) ⇒ Integer
Removes all items for the given keys.
-
#set(key, value, overwrite: true) ⇒ Boolean
Sets or updates the value at key.
-
#size ⇒ Integer
The number of key value pairs stored for this hash.
-
#to_enum(match: '*', count: 10) ⇒ Enumerator
Use redis-rb hscan_each method to iterate over particular keys.
-
#to_h ⇒ Hash<String, String>
Loads all the hash members in memory NOTE: if the hash is expected to be large, use to_enum.
-
#update(hash) ⇒ Boolean
Updates the underlying redis hash using the given Ruby hash's key/value mapping.
-
#values ⇒ Array<Strign>
A list of all hash values.
Methods inherited from Struct
#delete, #dump, #exists?, #expire, #expire_at, #initialize, #inspectable_attributes, #persist, #restore, #ttl, #type
Constructor Details
This class inherits a constructor from Redstruct::Struct
Instance Method Details
#[](key) ⇒ nil, String
Returns the value at key
14 15 16 |
# File 'lib/redstruct/hash.rb', line 14 def [](key) return self.connection.hget(@key, key) end |
#[]=(key, value) ⇒ Boolean
Sets or updates the value at key
31 32 33 |
# File 'lib/redstruct/hash.rb', line 31 def []=(key, value) set(key, value, overwrite: true) end |
#decrement(key, by: 1) ⇒ Integer, Float
Decrements the value at the given key
86 87 88 |
# File 'lib/redstruct/hash.rb', line 86 def decrement(key, by: 1) return increment(key, by: -by) end |
#empty? ⇒ Boolean
Returns true if the hash contains no elements
91 92 93 |
# File 'lib/redstruct/hash.rb', line 91 def empty? return !exists? end |
#get(*keys) ⇒ Hash<String, String>
Returns the value at key
22 23 24 25 |
# File 'lib/redstruct/hash.rb', line 22 def get(*keys) return self.connection.hget(@key, keys.first) if keys.size == 1 return self.connection.mapped_hmget(@key, *keys).reject { |_, v| v.nil? } end |
#increment(key, by: 1) ⇒ Integer, Float
Increments the value at the given key
74 75 76 77 78 79 80 |
# File 'lib/redstruct/hash.rb', line 74 def increment(key, by: 1) if by.is_a?(Float) self.connection.hincrbyfloat(@key, key, by.to_f).to_f else self.connection.hincrby(@key, key, by.to_i).to_i end end |
#key?(key) ⇒ Boolean
Checks if a key has a value
66 67 68 |
# File 'lib/redstruct/hash.rb', line 66 def key?(key) return coerce_bool(self.connection.hexists(@key, key)) end |
#keys ⇒ Array<String>
Returns a list of all hash keys with values associated
103 104 105 |
# File 'lib/redstruct/hash.rb', line 103 def keys return self.connection.hkeys(@key) end |
#remove(*keys) ⇒ Integer
Removes all items for the given keys
59 60 61 |
# File 'lib/redstruct/hash.rb', line 59 def remove(*keys) return self.connection.hdel(@key, keys) end |
#set(key, value, overwrite: true) ⇒ Boolean
Sets or updates the value at key
39 40 41 42 43 44 45 46 47 |
# File 'lib/redstruct/hash.rb', line 39 def set(key, value, overwrite: true) result = if overwrite self.connection.hset(@key, key, value) else self.connection.hsetnx(@key, key, value) end return coerce_bool(result) end |
#size ⇒ Integer
Returns the number of key value pairs stored for this hash
113 114 115 |
# File 'lib/redstruct/hash.rb', line 113 def size return self.connection.hlen(@key) end |
#to_enum(match: '*', count: 10) ⇒ Enumerator
Use redis-rb hscan_each method to iterate over particular keys
119 120 121 |
# File 'lib/redstruct/hash.rb', line 119 def to_enum(match: '*', count: 10) return self.connection.hscan_each(@key, match: match, count: count) end |
#to_h ⇒ Hash<String, String>
Loads all the hash members in memory NOTE: if the hash is expected to be large, use to_enum
98 99 100 |
# File 'lib/redstruct/hash.rb', line 98 def to_h return self.connection.hgetall(@key) end |
#update(hash) ⇒ Boolean
Updates the underlying redis hash using the given Ruby hash's key/value mapping
52 53 54 |
# File 'lib/redstruct/hash.rb', line 52 def update(hash) coerce_bool(self.connection.mapped_hmset(@key, hash)) end |
#values ⇒ Array<Strign>
Returns a list of all hash values
108 109 110 |
# File 'lib/redstruct/hash.rb', line 108 def values return self.connection.hvals(@key) end |