Module: Frivol
- Defined in:
- lib/frivol.rb
Overview
Frivol
Defined Under Namespace
Modules: ClassMethods, Config, Helpers
Constant Summary collapse
- NEVER_EXPIRE =
Defines a constant to indicate that storage should never expire
nil
Class Method Summary collapse
-
.included(host) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#clear_storage ⇒ Object
Clears the cached values and forces the next retrieve to fetch from Redis.
-
#delete_storage ⇒ Object
Deletes the stored values (and clears the cache).
-
#expire_storage(time, bucket = nil) ⇒ Object
Expire the stored data in
time
seconds. -
#retrieve(keys_and_defaults) ⇒ Object
Retrieve stored values, or defaults.
-
#storage_key(bucket = nil) ⇒ Object
The base key used for storage in Redis.
-
#store(keys_and_values) ⇒ Object
Store a hash of keys and values.
Class Method Details
.included(host) ⇒ Object
:nodoc:
350 351 352 |
# File 'lib/frivol.rb', line 350 def self.included(host) #:nodoc: host.extend(ClassMethods) end |
Instance Method Details
#clear_storage ⇒ Object
Clears the cached values and forces the next retrieve to fetch from Redis.
130 131 132 |
# File 'lib/frivol.rb', line 130 def clear_storage Frivol::Helpers.clear_hash self end |
#delete_storage ⇒ Object
Deletes the stored values (and clears the cache).
125 126 127 |
# File 'lib/frivol.rb', line 125 def delete_storage Frivol::Helpers.delete_hash self end |
#expire_storage(time, bucket = nil) ⇒ Object
Expire the stored data in time
seconds.
135 136 137 138 |
# File 'lib/frivol.rb', line 135 def expire_storage(time, bucket = nil) return if time.nil? Frivol::Config.redis.expire storage_key(bucket), time end |
#retrieve(keys_and_defaults) ⇒ Object
Retrieve stored values, or defaults.
If you retrieve a single key just that value is returned. If you retrieve multiple keys an array of values is returned. You might do:
name = retrieve :name => "Marc Heiligers"
first_name, last_name = retrieve :first_name => "Marc", :last_name => "Heiligers"
If the default is a symbol, Frivol will attempt to get the default from a method named after that symbol. If the class does not respond_to?
a method by that name, the symbol will assumed to be the default.
115 116 117 118 119 120 121 122 |
# File 'lib/frivol.rb', line 115 def retrieve(keys_and_defaults) hash = Frivol::Helpers.retrieve_hash(self) result = keys_and_defaults.map do |key, default| hash[key.to_s] || (default.is_a?(Symbol) && respond_to?(default) && send(default)) || default end return result.first if result.size == 1 result end |
#storage_key(bucket = nil) ⇒ Object
The base key used for storage in Redis.
This method has been implemented for use with ActiveRecord and uses "#{self.class.name}-#{id}"
for the default bucket and "#{self.class.name}-#{id}-#{bucket}"
for a named bucket. If you are not using ActiveRecord, or using classes that don’t respond to id, you should override this method in your class.
NOTE: This method has changed since version 0.1.4, and now has the bucket parameter (default: nil)
148 149 150 151 |
# File 'lib/frivol.rb', line 148 def storage_key(bucket = nil) @frivol_key ||= "#{self.class.name}-#{id}" bucket.nil? ? @frivol_key : "#{@frivol_key}-#{bucket}" end |
#store(keys_and_values) ⇒ Object
Store a hash of keys and values.
The hash need not be the complete hash of all things stored, just those you want to change. For example, you may call store :value1 => 1
and then later call store :value2 => 2
and Frivol will now have stored { :value1 => 1, :value => 2 }
. How Frivol stores or retrieves data is intended to be hidden and while it is true that it currently uses a Hash#to_json
you should not rely on this.
98 99 100 101 102 103 104 |
# File 'lib/frivol.rb', line 98 def store(keys_and_values) hash = Frivol::Helpers.retrieve_hash(self) keys_and_values.each do |key, value| hash[key.to_s] = value end Frivol::Helpers.store_hash(self, hash) end |