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.4.2'
- @@cache_error =
false
- @@locales =
locales
nil
Class Attribute Summary collapse
-
.hash_keys ⇒ Object
Returns the value of attribute hash_keys.
-
.prefix ⇒ Object
Returns the value of attribute prefix.
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.
114 115 116 |
# File 'lib/cachy.rb', line 114 def hash_keys @hash_keys end |
.prefix ⇒ Object
Returns the value of attribute prefix.
115 116 117 |
# File 'lib/cachy.rb', line 115 def prefix @prefix 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
127 128 129 |
# File 'lib/cachy.rb', line 127 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
120 121 122 123 124 125 |
# File 'lib/cachy.rb', line 120 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
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/’)
72 73 74 75 76 77 78 79 80 |
# File 'lib/cachy.rb', line 72 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
82 83 84 85 86 |
# File 'lib/cachy.rb', line 82 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
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
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# 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 my_prefix = ([:prefix] || prefix).to_s (my_prefix + key + [:suffix].to_s).gsub(' ', '_') end |
.key_versions ⇒ Object
88 89 90 |
# File 'lib/cachy.rb', line 88 def self.key_versions memory_store.cache{ read_versions } end |
.key_versions=(data) ⇒ Object
92 93 94 95 |
# File 'lib/cachy.rb', line 92 def self.key_versions=(data) memory_store.clear write_version(data) end |
.key_versions_cache_store ⇒ Object
135 136 137 |
# File 'lib/cachy.rb', line 135 def self.key_versions_cache_store @key_versions_cache_store || cache_store end |
.key_versions_cache_store=(cache) ⇒ Object
131 132 133 |
# File 'lib/cachy.rb', line 131 def self.key_versions_cache_store=(cache) @key_versions_cache_store = wrap_cache(cache) end |
.locales ⇒ Object
145 146 147 148 149 150 151 152 |
# File 'lib/cachy.rb', line 145 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
141 142 143 |
# File 'lib/cachy.rb', line 141 def self.locales=(x) @@locales = x end |