A Ruby wrapper for Volatile, a key-value pair API that everyone can use.
More documentation at RDoc.
gem install volatile_wtf
or for a Gemfile:
gem 'volatile_wtf'
bundle install
Storage = Volatile::Storage.new
Storage['user_name'] = 'Alice' # => 'Alice'
If you want to create a pair independent from your Storage:
Storage.set('random_key', 'random_val')
You can use symbols as keys, but it is not recommended.
Storage['user_name'] # => 'Alice'
If you want to retrieve a value by a custom key independent from your Storage, use Storage.get
:
Storage.get('random_key') # => 'random_val'
Volatile allows to get information about when a key-pair was created and modified.
-
Created
Storage.created('user_name') # => 2019-11-12 16:55:45 +0300
-
Modified
Storage.modified('user_name') # => 2019-11-12 17:40:45 +0300
To get modifiers information for non-Storage keys use external: true
parameter:
Storage.modified('user_name', external: true)
Namespace is used for distinguishing keys from different Storages.
By default, Storage is initialized with a namespace equal to SecureRandom.hex[0..5]
and makes keys look like 0123ab_some
instead of just some
. You can pass your own value like this:
Storage = Volatile::Storage.new('my_ns')
Or you can change namespace later:
Storage.namespace = 'my_own_ns'
Warning! You can loose links to previously stored data if you change a namespace.
If you want to get a namespaced key name, you should use Storage#namespaced_key
:
Storage.namespaced_key('nice_key') # => '0123ab_nice_key'
Don't forget that every Volatile::Storage.new
has its own namespace.
By default, #to_h
generates a hash with friendly keys without a namespace:
{
"user_name" => "Alice",
"email" => "[email protected]"
}
To generate a hash with namespaced keys, use use_namespace: true
parameter:
Storage.to_h(use_namespace: true)
This will return the following result:
{
"2365fc_user_name" => "Alice",
"2365fc_email" => "[email protected]"
}