Class: TypeBalancer::Rails::CacheAdapter
- Inherits:
-
Object
- Object
- TypeBalancer::Rails::CacheAdapter
- Defined in:
- lib/type_balancer/rails/cache_adapter.rb
Instance Method Summary collapse
- #clear_cache! ⇒ Object
- #delete(key) ⇒ Object
- #fetch(key, options = {}, &block) ⇒ Object
-
#initialize ⇒ CacheAdapter
constructor
A new instance of CacheAdapter.
- #write(key, value, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ CacheAdapter
Returns a new instance of CacheAdapter.
4 5 6 |
# File 'lib/type_balancer/rails/cache_adapter.rb', line 4 def initialize @memory_cache = {} end |
Instance Method Details
#clear_cache! ⇒ Object
32 33 34 35 |
# File 'lib/type_balancer/rails/cache_adapter.rb', line 32 def clear_cache! ::Rails.cache.clear if defined?(::Rails) && ::Rails.respond_to?(:cache) && ::Rails.cache @memory_cache.clear end |
#delete(key) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/type_balancer/rails/cache_adapter.rb', line 24 def delete(key) if defined?(::Rails) && ::Rails.respond_to?(:cache) && ::Rails.cache ::Rails.cache.delete(key) else @memory_cache.delete(key) end end |
#fetch(key, options = {}, &block) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/type_balancer/rails/cache_adapter.rb', line 8 def fetch(key, = {}, &block) if defined?(::Rails) && ::Rails.respond_to?(:cache) && ::Rails.cache ::Rails.cache.fetch(key, , &block) else @memory_cache[key] ||= block.call end end |
#write(key, value, options = {}) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/type_balancer/rails/cache_adapter.rb', line 16 def write(key, value, = {}) if defined?(::Rails) && ::Rails.respond_to?(:cache) && ::Rails.cache ::Rails.cache.write(key, value, ) else @memory_cache[key] = value end end |