Class: TypeBalancer::Rails::CacheAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/type_balancer/rails/cache_adapter.rb

Instance Method Summary collapse

Constructor Details

#initializeCacheAdapter

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, options = {}, &block)
  if defined?(::Rails) && ::Rails.respond_to?(:cache) && ::Rails.cache
    ::Rails.cache.fetch(key, options, &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, options = {})
  if defined?(::Rails) && ::Rails.respond_to?(:cache) && ::Rails.cache
    ::Rails.cache.write(key, value, options)
  else
    @memory_cache[key] = value
  end
end