Class: Cachy
- Inherits:
-
Object
- Object
- Cachy
- Defined in:
- lib/cachy.rb
Defined Under Namespace
Classes: MemcachedWrapper, MonetaWrapper, Wrapper
Constant Summary collapse
- WHILE_RUNNING_TMEOUT =
seconds
5*60
- KEY_VERSION_TIMEOUT =
seconds
30- @@key_versions =
Fetch key_versions from cache every KEY_VERSION_TIMEOUT seconds, otherwise every .key call would result in an cache request
{:versions=>{}, :last_set=>0}
Class Attribute Summary collapse
-
.hash_keys ⇒ Object
Returns the value of attribute hash_keys.
-
.locales ⇒ Object
Returns the value of attribute locales.
Class Method Summary collapse
-
.cache(*args) ⇒ Object
Cache the result of a block.
- .cache_store ⇒ Object
-
.cache_store=(x) ⇒ Object
Wrap non ActiveSupport style cache stores, to get the same interface for all.
-
.expire(*args) ⇒ Object
Expire all possible locales of a cache, use the same arguments as with cache.
- .expire_view(*args) ⇒ Object
-
.increment_key(key) ⇒ Object
Expires all caches that use this key.
-
.key(*args) ⇒ Object
Constructs a cache-key (first argument must be a String/Symbol).
- .key_versions ⇒ Object
- .key_versions=(data) ⇒ Object
Class Attribute Details
.hash_keys ⇒ Object
Returns the value of attribute hash_keys.
96 97 98 |
# File 'lib/cachy.rb', line 96 def hash_keys @hash_keys end |
.locales ⇒ Object
Returns the value of attribute locales.
123 124 125 |
# File 'lib/cachy.rb', line 123 def locales @locales end |
Class Method Details
.cache(*args) ⇒ Object
Cache the result of a block
Cachy.cache(:my_key){ expensive() } Cachy.cache(:my_key, :expires_in=>1.hour){ expensive() } Cachy.cache(:my_key, :keys=>){ expensive() } Cachy.cache(:my_key, :without_locale=>true){ expensive() } Cachy.cache(:my_key, :hash_key=>true){ expensive() }
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cachy.rb', line 12 def self.cache(*args) key = key(*args) = (args) # Cached result? result = cache_store.read(key) return result unless result == nil # Calculate result! set_while_running(key, ) result = yield cache_store.write key, result, result end |
.cache_store ⇒ Object
115 116 117 |
# File 'lib/cachy.rb', line 115 def self.cache_store @cache_store || raise("Use: Cachy.cache_store = your_cache_store") end |
.cache_store=(x) ⇒ Object
Wrap non ActiveSupport style cache stores, to get the same interface for all
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/cachy.rb', line 101 def self.cache_store=(x) if x.respond_to? :read and x.respond_to? :write @cache_store=x elsif x.respond_to? "[]" and x.respond_to? :set require 'cachy/memcached_wrapper' @cache_store = MemcachedWrapper.new(x) elsif x.respond_to? "[]" and x.respond_to? :store require 'cachy/moneta_wrapper' @cache_store = MonetaWrapper.new(x) else raise "This cache_store type is not usable for Cachy!" end end |
.expire(*args) ⇒ Object
Expire all possible locales of a cache, use the same arguments as with cache
Cachy.expire(:my_key, User.first) Cachy.expire(:my_key, User.first, :keys=>) Cachy.expire(:my_key, :prefix=>‘views/’)
54 55 56 57 58 59 60 61 62 |
# File 'lib/cachy.rb', line 54 def self.expire(*args) = (args) (locales+[false]).each do |locale| without_locale = (locale==false) args_with_locale = args + [.merge(:locale=>locale, :without_locale=>without_locale)] cache_store.delete key(*args_with_locale) end end |
.expire_view(*args) ⇒ Object
64 65 66 67 68 |
# File 'lib/cachy.rb', line 64 def self.expire_view(*args) = (args) args = args + [.merge(:prefix=>'views/')] expire(*args) end |
.increment_key(key) ⇒ Object
Expires all caches that use this key
87 88 89 90 91 92 93 |
# File 'lib/cachy.rb', line 87 def self.increment_key(key) key = key.to_sym version = key_versions[key] || 0 version += 1 self.key_versions = key_versions.merge(key => version) version end |
.key(*args) ⇒ Object
Constructs a cache-key (first argument must be a String/Symbol)
Cachy.key :my_key Cachy.key :my_key, User.first, :locale=>:de Cachy.key :my_key, User.first, :without_locale=>true, :hash_key=>true
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cachy.rb', line 33 def self.key(*args) = (args) ensure_valid_keys key = (args + (args.first, )).compact.map do |part| if part.respond_to? :cache_key part.cache_key else part end end * "_" key = ([:hash_key] || hash_keys) ? hash(key) : key [:prefix].to_s + key + [:suffix].to_s end |
.key_versions ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/cachy.rb', line 73 def self.key_versions if key_versions_expired? versions = cache_store.read("cachy_key_versions") || {} @@key_versions = {:versions=>versions, :last_set=>Time.now.to_i} end @@key_versions[:versions] end |
.key_versions=(data) ⇒ Object
81 82 83 84 |
# File 'lib/cachy.rb', line 81 def self.key_versions=(data) @@key_versions[:last_set] = 0 #expire current key cache_store.write("cachy_key_versions", data) end |