Class: Elephas::Cache
- Inherits:
-
Object
- Object
- Elephas::Cache
- Defined in:
- lib/elephas/cache.rb
Overview
This is the main class of the framework. Use only this class to access the cache.
Class Attribute Summary (collapse)
-
+ (Object) provider
Returns the value of attribute provider.
Class Method Summary (collapse)
-
+ (String) default_prefix
Returns the default prefix for cache entries.
-
+ (Boolean) delete(key)
Deletes a value from the cache.
-
+ (Boolean) exists?(key)
Checks if a key exists in the cache.
-
+ (Object|NilClass) read(key)
Reads a value from the cache.
-
+ (Hash) setup_options(options, key)
Setups options for use into the framework.
-
+ (Object|Entry) use(key, options = {}, &block)
This is the main method of the framework.
-
+ (Object) write(key, value, options = {})
Writes a value to the cache.
Class Attribute Details
+ (Object) provider
Returns the value of attribute provider
13 14 15 |
# File 'lib/elephas/cache.rb', line 13 def provider @provider end |
Class Method Details
+ (String) default_prefix
Returns the default prefix for cache entries.
95 96 97 |
# File 'lib/elephas/cache.rb', line 95 def self.default_prefix "elephas-#{::Elephas::Version::STRING}-cache" end |
+ (Boolean) delete(key)
Deletes a value from the cache.
80 81 82 |
# File 'lib/elephas/cache.rb', line 80 def self.delete(key) @provider.delete(key) end |
+ (Boolean) exists?(key)
Checks if a key exists in the cache.
88 89 90 |
# File 'lib/elephas/cache.rb', line 88 def self.exists?(key) @provider.exists?(key) end |
+ (Object|NilClass) read(key)
Reads a value from the cache.
61 62 63 |
# File 'lib/elephas/cache.rb', line 61 def self.read(key) @provider.read(key) end |
+ (Hash) setup_options(options, key)
Setups options for use into the framework. Valid options are:
- :ttl: The TTL (time to live, in milliseconds) of the entry. It means how long will the value stay in cache. Setting it to 0 or less means never cache the entry.
- :force: Setting it to
true
will always skip the cache. - :key: The key associated to this value. You should never set this option directly.
- :prefix: The prefix used in cache. This is used to avoid conflicts with other caching frameworks.
- :complete_key: The complete key used for computing the hash. By default is concatenation of
:key
and:prefix
options. - :hash: The hash used to store the key in the cache. Should be unique
- :as_entry: In
Elephas::Cache.use
, setting this totrue
will return the entireEntry
object rather than the value only.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/elephas/cache.rb', line 113 def self.(, key) = {} if !.is_a?(::Hash) = {ttl: 1.hour * 1000, force: false, as_entry: false}.merge() # Sanitize options. = (, key) # Wrap the final key to ensure we don't have colliding namespaces. [:complete_key] ||= "#{[:prefix]}[#{[:key]}]" # Compute the hash key used for referencing this value. [:hash] ||= ::Elephas::Entry.hashify_key([:complete_key]) end |
+ (Object|Entry) use(key, options = {}, &block)
This is the main method of the framework.
It tries reading a key from the cache.
If it doesn’t find it, it uses the provided block (which receives options as argument) to compute its value and then store it into the cache for later usages.
```ruby value = Elephas::Cache.use(“KEY”) do |options| “VALUE” end
value # => “VALUE”
value = Elephas::Cache.use(“KEY”) do |options| “ANOTHER VALUE” end
value # => “VALUE” ```
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/elephas/cache.rb', line 43 def self.use(key, = {}, &block) rv = nil # Get options = ::Elephas::Cache.(, key) # Check if the storage has the value (if we don't have to skip the cache) rv = @provider.read([:hash]) if [:force] == false && [:ttl] > 0 rv = compute_value(, &block) if rv.nil? && block # Try to compute the value from the block # Return value [:as_entry] ? rv : rv.value.dup end |
+ (Object) write(key, value, options = {})
Writes a value to the cache.
72 73 74 |
# File 'lib/elephas/cache.rb', line 72 def self.write(key, value, = {}) @provider.write(key, value, ::Elephas::Cache.(, key)) end |