Class: Cachy
- Inherits:
-
Object
- Object
- Cachy
- Defined in:
- lib/cachy.rb
Defined Under Namespace
Classes: MemcachedWrapper, MonetaWrapper, RedisWrapper, Wrapper
Constant Summary collapse
- WHILE_RUNNING_TIMEOUT =
seconds
5*60
- KEY_VERSION_TIMEOUT =
seconds
30
- HEALTH_CHECK_KEY =
'cachy_healthy'
- KEY_VERSIONS_KEY =
'cachy_key_versions'
- @@cache_error =
false
- @@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}
- @@locales =
locales
nil
Class Attribute Summary collapse
-
.hash_keys ⇒ Object
Returns the value of attribute hash_keys.
Class Method Summary collapse
-
.cache(*args) ⇒ Object
Cache the result of a block.
- .cache_if(cond, *args, &block) ⇒ Object
- .cache_store ⇒ Object
-
.cache_store=(cache) ⇒ Object
Wrap non ActiveSupport style cache stores, to get the same interface for all.
- .delete_key(key) ⇒ Object
-
.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
- .key_versions_cache_store ⇒ Object
- .key_versions_cache_store=(cache) ⇒ Object
- .locales ⇒ Object
- .locales=(x) ⇒ Object
Class Attribute Details
.hash_keys ⇒ Object
Returns the value of attribute hash_keys.
114 115 116 |
# File 'lib/cachy.rb', line 114 def hash_keys @hash_keys 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() }
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/cachy.rb', line 16 def self.cache(*args) key = key(*args) = (args) 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_if(cond, *args, &block) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/cachy.rb', line 31 def self.cache_if(cond, *args, &block) if cond cache(*args, &block) else block.call end end |
.cache_store ⇒ Object
124 125 126 |
# File 'lib/cachy.rb', line 124 def self.cache_store @cache_store || raise("Use: Cachy.cache_store = your_cache_store") end |
.cache_store=(cache) ⇒ Object
Wrap non ActiveSupport style cache stores, to get the same interface for all
119 120 121 122 |
# File 'lib/cachy.rb', line 119 def self.cache_store=(cache) @cache_store = wrap_cache(cache) @cache_store.write HEALTH_CHECK_KEY, 'yes' end |
.delete_key(key) ⇒ Object
107 108 109 110 111 |
# File 'lib/cachy.rb', line 107 def self.delete_key(key) versions = key_versions.dup versions.delete(key.to_sym) self.key_versions = versions 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/’)
65 66 67 68 69 70 71 72 73 |
# File 'lib/cachy.rb', line 65 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
75 76 77 78 79 |
# File 'lib/cachy.rb', line 75 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
98 99 100 101 102 103 104 105 |
# File 'lib/cachy.rb', line 98 def self.increment_key(key) key = key.to_sym current_versions = read_versions version = current_versions[key] || 0 version += 1 self.key_versions = current_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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/cachy.rb', line 44 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).gsub(' ', '_') end |
.key_versions ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/cachy.rb', line 84 def self.key_versions if key_versions_expired? versions = read_versions @@key_versions = {:versions=>versions, :last_set=>Time.now.to_i} end @@key_versions[:versions] end |
.key_versions=(data) ⇒ Object
92 93 94 95 |
# File 'lib/cachy.rb', line 92 def self.key_versions=(data) @@key_versions[:last_set] = 0 #expire current key write_version(data) end |
.key_versions_cache_store ⇒ Object
132 133 134 |
# File 'lib/cachy.rb', line 132 def self.key_versions_cache_store @key_versions_cache_store || cache_store end |
.key_versions_cache_store=(cache) ⇒ Object
128 129 130 |
# File 'lib/cachy.rb', line 128 def self.key_versions_cache_store=(cache) @key_versions_cache_store = wrap_cache(cache) end |
.locales ⇒ Object
142 143 144 145 146 147 148 149 |
# File 'lib/cachy.rb', line 142 def self.locales return @@locales if @@locales if defined?(I18n) and I18n.respond_to?(:available_locales) I18n.available_locales else [] end end |
.locales=(x) ⇒ Object
138 139 140 |
# File 'lib/cachy.rb', line 138 def self.locales=(x) @@locales = x end |