Module: Innate::Cache::API
- Included in:
- DRb, Marshal, Memory, YAML
- Defined in:
- /home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache/api.rb
Overview
This is the API every Cache has to conform to.
The default behaviour is tailored for the Memory cache, override any behaviour as you need.
+key+ may be a String or Symbol +value+ is a Hash of serializable (as according to Marshal) objects
Every cache instance has to respond to:
::new() #cache_setup(hostname, username, appname, cachename) #cache_clear() #cache_delete(*keys) #cache_fetch(key, default = nil) #cache_store(key, value, options = {})
We are prefixing cache_ to make the intent clear and implementation easier, as there may be existing behaviour associated with the non-prefixed version.
Also note that we create one instance per cache name-space.
Instance Method Summary (collapse)
-
- (Object) cache_clear
Remove all key/value pairs from the cache.
-
- (Object Array nil) cache_delete(key, *keys)
Remove the corresponding key/value pair for each key passed.
-
- (Object) cache_fetch(key, default = nil)
Answer with the value associated with the +key+, +nil+ if not found or expired.
-
- (Object) cache_setup(hostname, username, appname, cachename)
Executed after #initialize and before any other method.
-
- (Object) cache_store(key, value, options = {}) {|key, value_hash| ... }
Set +key+ to +value+.
Instance Method Details
- (Object) cache_clear
Remove all key/value pairs from the cache. Should behave as if #delete had been called with all +keys+ as argument.
44 45 46 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache/api.rb', line 44 def cache_clear clear end |
- (Object Array nil) cache_delete(key, *keys)
Remove the corresponding key/value pair for each key passed. If removing is not an option it should set the corresponding value to nil.
If only one key was deleted, answer with the corresponding value. If multiple keys were deleted, answer with an Array containing the values.
NOTE: Due to differences in the underlying implementation in the caches, some may not return the deleted value as it would mean another lookup before deletion. This is the case for caches on memcached or any database system.
63 64 65 66 67 68 69 70 71 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache/api.rb', line 63 def cache_delete(key, *keys) if keys.empty? if value = yield(key) value[:value] end else [key, *keys].map{|element| cache_delete(element) } end end |
- (Object) cache_fetch(key, default = nil)
Answer with the value associated with the +key+, +nil+ if not found or expired.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache/api.rb', line 81 def cache_fetch(key, default = nil) value = default if entry = yield(key) if expires = entry[:expires] if expires > Time.now value = entry[:value] else cache_delete(key) end else value = entry[:value] end end return value end |
- (Object) cache_setup(hostname, username, appname, cachename)
Executed after #initialize and before any other method.
Some parameters identifying the current process will be passed so caches that act in one global name-space can use them as a prefix.
37 38 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache/api.rb', line 37 def cache_setup(hostname, username, appname, cachename) end |
- (Object) cache_store(key, value, options = {}) {|key, value_hash| ... }
Set +key+ to +value+.
+options+ may be one of: :ttl => time to live in seconds if given in Numeric infinite or maximum if not given
Usage: Cache.value.store(:num, 3, :ttl => 20) Cache.value.fetch(:num) # => 3 sleep 21 Cache.value.fetch(:num) # => nil
116 117 118 119 120 121 122 123 124 125 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache/api.rb', line 116 def cache_store(key, value, = {}) ttl = [:ttl] value_hash = {:value => value} value_hash[:expires] = Time.now + ttl if ttl yield(key, value_hash) return value end |