Class: Redstruct::Hash

Inherits:
Struct show all
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

#key

Attributes inherited from Factory::Object

#factory

Instance Method Summary collapse

Methods included from Utils::Iterable

bound_enumerator, #each

Methods inherited from Struct

#delete, #dump, #exists?, #expire, #expire_at, #initialize, #inspectable_attributes, #persist, #restore, #ttl, #type

Methods included from Utils::Coercion

coerce_array, coerce_bool

Methods inherited from Factory::Object

#connection, #initialize, #inspectable_attributes

Methods included from Utils::Inspectable

#inspect, #inspectable_attributes

Constructor Details

This class inherits a constructor from Redstruct::Struct

Instance Method Details

#[](key) ⇒ nil, String

Returns the value at key

Parameters:

  • key (#to_s)

    the hash key

Returns:

  • (nil, String)

    the value at key, or nil if nothing



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

Parameters:

  • key (#to_s)

    the hash key

  • value (#to_s)

    the new value to set

Returns:

  • (Boolean)

    true if the field was set (not updated!), false otherwise



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

Parameters:

  • key (#to_s)

    the hash key

  • by (Integer, Float) (defaults to: 1)

    defaults to 1

Returns:

  • (Integer, Float)

    returns the decremented value



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.

Returns:

  • (Boolean)

    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

Parameters:

  • keys (Array<#to_s>)

    a list of keys to fetch; can be only one

Returns:

  • (Hash<String, String>)

    if only one key was passed, then return the value for it; otherwise returns a Ruby hash where each key in the ‘keys` is mapped to the value returned by redis



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

Parameters:

  • key (#to_s)

    the hash key

  • by (Integer, Float) (defaults to: 1)

    defaults to 1

Returns:

  • (Integer, Float)

    returns the incremented value



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

Parameters:

  • key (#to_s)

    the key to check for

Returns:

  • (Boolean)

    true if the key has a value associated, false otherwise



66
67
68
# File 'lib/redstruct/hash.rb', line 66

def key?(key)
  return coerce_bool(self.connection.hexists(@key, key))
end

#keysArray<String>

Returns a list of all hash keys with values associated.

Returns:

  • (Array<String>)

    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

Parameters:

  • keys (Array<#to_s>)

    the list of keys to remove

Returns:

  • (Integer)

    the number of keys removed



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

Parameters:

  • key (#to_s)

    the hash key

  • value (#to_s)

    the new value to set

Returns:

  • (Boolean)

    true if the field was set (not updated!), false otherwise



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

#sizeInteger

Returns the number of key value pairs stored for this hash.

Returns:

  • (Integer)

    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

Returns:

  • (Enumerator)

    base enumerator to iterate of the namespaced 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_hHash<String, String>

Loads all the hash members in memory NOTE: if the hash is expected to be large, use to_enum

Returns:



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

Parameters:

  • hash (Hash)

    the key/value mapping to use

Returns:

  • (Boolean)

    true if updated, false otherwise



52
53
54
# File 'lib/redstruct/hash.rb', line 52

def update(hash)
  coerce_bool(self.connection.mapped_hmset(@key, hash))
end

#valuesArray<Strign>

Returns a list of all hash values.

Returns:

  • (Array<Strign>)

    a list of all hash values



108
109
110
# File 'lib/redstruct/hash.rb', line 108

def values
  return self.connection.hvals(@key)
end