Module: Frivol
- Defined in:
- lib/frivol.rb,
lib/frivol/config.rb,
lib/frivol/functor.rb,
lib/frivol/helpers.rb,
lib/frivol/class_methods.rb
Overview
Frivol
Defined Under Namespace
Modules: ClassMethods, Config, Helpers Classes: Functor
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:
75 76 77 |
# File 'lib/frivol.rb', line 75 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.
52 53 54 |
# File 'lib/frivol.rb', line 52 def clear_storage Frivol::Helpers.clear_hash self end |
#delete_storage ⇒ Object
Deletes the stored values (and clears the cache).
47 48 49 |
# File 'lib/frivol.rb', line 47 def delete_storage Frivol::Helpers.delete_hash self end |
#expire_storage(time, bucket = nil) ⇒ Object
Expire the stored data in time
seconds.
57 58 59 60 |
# File 'lib/frivol.rb', line 57 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.
37 38 39 40 41 42 43 44 |
# File 'lib/frivol.rb', line 37 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)
70 71 72 73 |
# File 'lib/frivol.rb', line 70 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.
20 21 22 23 24 25 26 |
# File 'lib/frivol.rb', line 20 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 |