Module: Cachy
- Defined in:
- lib/cachy.rb,
lib/cachy/version.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'
- Version =
VERSION = '0.3.0'
- @@cache_error =
false
- @@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
- .get(*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.
113 114 115 |
# File 'lib/cachy.rb', line 113 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() }
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/cachy.rb', line 18 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
33 34 35 36 37 38 39 |
# File 'lib/cachy.rb', line 33 def self.cache_if(cond, *args, &block) if cond cache(*args, &block) else block.call end end |
.cache_store ⇒ Object
125 126 127 |
# File 'lib/cachy.rb', line 125 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
118 119 120 121 122 123 |
# File 'lib/cachy.rb', line 118 def self.cache_store=(cache) @cache_store = wrap_cache(cache) @cache_store.write HEALTH_CHECK_KEY, 'yes' @key_versions_cache_store = nil memory_store.clear end |
.delete_key(key) ⇒ Object
106 107 108 109 110 |
# File 'lib/cachy.rb', line 106 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/’)
71 72 73 74 75 76 77 78 79 |
# File 'lib/cachy.rb', line 71 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
81 82 83 84 85 |
# File 'lib/cachy.rb', line 81 def self.expire_view(*args) = (args) args = args + [.merge(:prefix=>'views/')] expire(*args) end |
.get(*args) ⇒ Object
41 42 43 |
# File 'lib/cachy.rb', line 41 def self.get(*args) cache_store.read(key(*args)) end |
.increment_key(key) ⇒ Object
Expires all caches that use this key
97 98 99 100 101 102 103 104 |
# File 'lib/cachy.rb', line 97 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
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cachy.rb', line 50 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
87 88 89 |
# File 'lib/cachy.rb', line 87 def self.key_versions memory_store.cache{ read_versions } end |
.key_versions=(data) ⇒ Object
91 92 93 94 |
# File 'lib/cachy.rb', line 91 def self.key_versions=(data) memory_store.clear write_version(data) end |
.key_versions_cache_store ⇒ Object
133 134 135 |
# File 'lib/cachy.rb', line 133 def self.key_versions_cache_store @key_versions_cache_store || cache_store end |
.key_versions_cache_store=(cache) ⇒ Object
129 130 131 |
# File 'lib/cachy.rb', line 129 def self.key_versions_cache_store=(cache) @key_versions_cache_store = wrap_cache(cache) end |
.locales ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/cachy.rb', line 143 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
139 140 141 |
# File 'lib/cachy.rb', line 139 def self.locales=(x) @@locales = x end |