Module: CacheStoreApi::ClassMethods
- Included in:
- CacheStoreApi
- Defined in:
- lib/cache-store-api/class_methods.rb
Instance Attribute Summary collapse
-
#cache_lambda ⇒ Object
readonly
Returns the value of attribute cache_lambda.
Instance Method Summary collapse
- #cache ⇒ Object
- #caching? ⇒ Boolean
- #data ⇒ Object
- #disable_cache! ⇒ Object
- #enable_cache! ⇒ Object
- #expire(key) ⇒ Object
- #lazy_cache(key, expiration = 3600) ⇒ Object
- #set_cache(&block) ⇒ Object
- #set_perform_caching(&block) ⇒ Object
Instance Attribute Details
#cache_lambda ⇒ Object (readonly)
Returns the value of attribute cache_lambda.
3 4 5 |
# File 'lib/cache-store-api/class_methods.rb', line 3 def cache_lambda @cache_lambda end |
Instance Method Details
#cache ⇒ Object
27 28 29 |
# File 'lib/cache-store-api/class_methods.rb', line 27 def cache cache_lambda.call end |
#caching? ⇒ Boolean
35 36 37 |
# File 'lib/cache-store-api/class_methods.rb', line 35 def caching? !!perform_caching_lambda.call end |
#data ⇒ Object
23 24 25 |
# File 'lib/cache-store-api/class_methods.rb', line 23 def data cache.instance_variable_get(:@data) end |
#disable_cache! ⇒ Object
49 50 51 52 53 |
# File 'lib/cache-store-api/class_methods.rb', line 49 def disable_cache! set_perform_caching do false end end |
#enable_cache! ⇒ Object
43 44 45 46 47 |
# File 'lib/cache-store-api/class_methods.rb', line 43 def enable_cache! set_perform_caching do true end end |
#expire(key) ⇒ Object
19 20 21 |
# File 'lib/cache-store-api/class_methods.rb', line 19 def expire(key) cache.delete(key) end |
#lazy_cache(key, expiration = 3600) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/cache-store-api/class_methods.rb', line 5 def lazy_cache(key, expiration=3600) return cache.read(key) unless block_given? if caching? return cache.read(key) if cache.exist?(key) data = yield cache.write(key, data, :expires_in => expiration) data else yield end end |
#set_cache(&block) ⇒ Object
31 32 33 |
# File 'lib/cache-store-api/class_methods.rb', line 31 def set_cache(&block) @cache_lambda = block end |
#set_perform_caching(&block) ⇒ Object
39 40 41 |
# File 'lib/cache-store-api/class_methods.rb', line 39 def set_perform_caching(&block) @perform_caching_lambda = block end |