struct Wraith::HashStore(K, V)
- Wraith::HashStore(K, V)
- Struct
- Value
- Object
Included Modules
Defined in:
wraithdb/stores/hash_store.crConstructors
Instance Method Summary
-
#[](key : K)
An alias for #get ```
-
#[]=(key : K, val : V)
Sets the value for the key.
-
#delete(key : K)
Deletes the key-value pair and returns the value, otherwise returns nil.
- #dup(*args, **options)
- #dup(*args, **options, &)
-
#get(key : K)
Returns the value of the key.
-
#get_with_expiration(key : K) : Tuple(V, Int64 | Nil) | Nil
Returns the value of the key along with the remaining TTL, in ms, as a tuple.
-
#inc(key : K, magnitude = 1) : V
Increments the value of a specified key.
-
#keys : Array(K)
Returns an array of all keys in a hash.
-
#set(key : K, val : V, ttl = nil)
Sets the value for the key.
- #store
-
#ttl(key : K, ttl = nil) : Int64 | Nil
Returns the remaining time to live of a key.
-
#values : Array(V)
Returns an array of all values in a hash.
Constructor Detail
Instance Method Detail
Sets the value for the key. Keys set with the #[]=
syntax do not support
an expiration.
Return value: The value of the key.
Example:
hash_store["k"] = 1
Returns the value of the key. If the key does not exist or has expired, nil is returned.
Return value: The value for the key.
Example:
hash_store.set("a", 1)
hash_store.get("a") => 1
hash_store.get("b") => nil
Returns the value of the key along with the remaining TTL, in ms, as a tuple. If the key does not have a TTL, the TTL portion of the tuple will be nil. If the key does not exist or has expired, nil is returned.
Return value: A tuple containing the value and expiration in ms.
Example:
hash_store.set("a", 1, ttl: 10.milliseconds)
hash_store.set("b", 2)
hash_store.get_with_expiration("a") => {1, 10}
hash_store.get_with_expiration("b") => {2, nil}
hash_store.get_with_expiration("c") => nil
Increments the value of a specified key. The class of the value needs
to either support +
or have that method defined. If a magnitude is not
is not specified, it defaults to 1. If a key does not exist, the magnitude
will be set as the key's value and the key will not have an expiration.
Return value: The incremented value.
Examples:
hash_store.set("foo", 3) => 3
hash_store.inc("foo") => 4
hash_store.inc("foo", 3) => 7
hash_store.inc("foo", -2) => 5
hash_store.inc("bar") => 1
Returns an array of all keys in a hash. If a key is expired, it will not be returned.
Sets the value for the key. Accepts a (time-to-live) #ttl
Time::Span
value. Keys that have exceeded their TTL will be purged.
Return value: The value of the key.
Example:
hash_store.set("key", 1)
hash_store.set(key: "key", val: 1)
hash_store.set("key", 1, ttl: 500.milliseconds)
hash_store.set(key: "key", val: 1, ttl: 2.days)
Returns the remaining time to live of a key. If a #ttl
is set
it will update the TTL for the key to the specified value.
Return value: Integer: TTL in seconds, or Nil to indicate the key does not exist.
Examples:
hash_store.ttl("foo", 3) => 3
hash_store.ttl("bar", 0) => 0
hash_store.ttl("bar") # => nil
Returns an array of all values in a hash. If a key is expired, its value will not be returned.