Class: Ramaze::Cache
Overview
This is the wrapper of all caches, providing mechanism for switching caching from one adapter to another.
Constant Summary collapse
- CACHES =
Holds all the instances of Cache that are being added.
{}
Instance Attribute Summary collapse
-
#cache ⇒ Object
Returns the value of attribute cache.
Class Method Summary collapse
-
.add(*keys) ⇒ Object
This will define a method to access a new cache directly over singleton-methods on Cache.
-
.add_on(key, cache_class) ⇒ Object
Define a new cache available by Cache::key.
-
.startup(options) ⇒ Object
Initializes the Cache for the general caches Ramaze uses.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Gets the value for the given key, or
nil
if not found. -
#[]=(key, value) ⇒ Object
Sets key to value with an infinite time to live.
-
#clear ⇒ Object
Empties this cache.
-
#delete(*args) ⇒ Object
Deletes each passed key from this cache.
-
#fetch(key, default = nil) ⇒ Object
(also: #get)
Gets the value of the given key, or default if not found.
-
#initialize(cache = Global.cache) ⇒ Cache
constructor
Initializes the cache, defined by Global.cache.
-
#store(key, value, opts = {}) ⇒ Object
(also: #set)
Sets key to value.
-
#values_at(*keys) ⇒ Object
Answers with value for each key.
Constructor Details
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
20 21 22 |
# File 'lib/ramaze/cache.rb', line 20 def cache @cache end |
Class Method Details
.add(*keys) ⇒ Object
This will define a method to access a new cache directly over singleton-methods on Cache.
The @cache_name is internally used for caches which do not save different caches in different namespaces, for example memcached. +++
40 41 42 43 44 45 46 |
# File 'lib/ramaze/cache.rb', line 40 def add *keys keys.each{|key| klass = Global.cache_alternative.fetch(key, Global.cache) add_on(key, klass) } Log.dev("Added caches for: #{keys.join(', ')}") end |
.add_on(key, cache_class) ⇒ Object
Define a new cache available by Cache::key
49 50 51 52 53 |
# File 'lib/ramaze/cache.rb', line 49 def add_on(key, cache_class) CACHES[key] = new(cache_class) CACHES[key].instance_variable_set("@cache_name", key) eval("def self.%s; CACHES[%p]; end" % [key, key]) end |
.startup(options) ⇒ Object
Initializes the Cache for the general caches Ramaze uses. Cache#startup is called by Ramaze#startup, when initializing the Ramaze.trait(:essentials).
28 29 30 31 |
# File 'lib/ramaze/cache.rb', line 28 def startup() Cache.add :compiled, :actions, :patterns, :resolved, :shield, :action_methods end |
Instance Method Details
#[](key) ⇒ Object
Gets the value for the given key, or nil
if not found.
63 64 65 |
# File 'lib/ramaze/cache.rb', line 63 def [](key) fetch(key) end |
#[]=(key, value) ⇒ Object
Sets key to value with an infinite time to live.
68 69 70 |
# File 'lib/ramaze/cache.rb', line 68 def []=(key, value) store(key, value) end |
#clear ⇒ Object
Empties this cache.
73 74 75 |
# File 'lib/ramaze/cache.rb', line 73 def clear @cache.clear end |
#delete(*args) ⇒ Object
Deletes each passed key from this cache.
78 79 80 81 82 |
# File 'lib/ramaze/cache.rb', line 78 def delete(*args) args.each do |arg| @cache.delete("#{@cache_name}:#{arg}") end end |
#fetch(key, default = nil) ⇒ Object Also known as: get
Gets the value of the given key, or default if not found.
85 86 87 88 89 90 |
# File 'lib/ramaze/cache.rb', line 85 def fetch(key, default = nil) return default unless entry = @cache["#{@cache_name}:#{key}"] return entry[:value] if entry[:expires].nil? || entry[:expires] > Time.now @cache.delete("#{@cache_name}:#{key}") default end |
#store(key, value, opts = {}) ⇒ Object Also known as: set
Sets key to value. Supports the following options:
[+:ttl+] time to live in seconds
96 97 98 99 100 101 102 103 |
# File 'lib/ramaze/cache.rb', line 96 def store(key, value, opts = {}) opts = {:ttl => opts} if opts.is_a?(Integer) @cache["#{@cache_name}:#{key}"] = { :expires => opts[:ttl] ? Time.now + opts[:ttl].to_i : nil, :value => value } value end |
#values_at(*keys) ⇒ Object
Answers with value for each key.
108 109 110 111 112 |
# File 'lib/ramaze/cache.rb', line 108 def values_at(*keys) values = [] keys.each {|key| values << fetch(key) } values end |