Module: BusinessFlow::Cacheable::ClassMethods

Defined in:
lib/business_flow/cacheable.rb

Overview

DSL Methods

Defined Under Namespace

Classes: CacheOptions

Instance Method Summary collapse

Instance Method Details

#cache_key(key = nil) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/business_flow/cacheable.rb', line 51

def cache_key(key = nil)
  if key
    @cache_key = Callable.new(key)
  else
    @cache_key ||= Callable.new(:parameter_object)
  end
end

#cache_optionsObject



27
28
29
# File 'lib/business_flow/cacheable.rb', line 27

def cache_options
  @cache_options ||= CacheOptions.new
end

#cache_store(store = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/business_flow/cacheable.rb', line 31

def cache_store(store = nil)
  if store
    @cache_store = store
  else
    @cache_store ||= if defined?(Rails)
                       Rails.cache
                     else
                       ActiveSupport::Cache::MemoryStore.new
                     end
  end
end

#cache_ttl(ttl = nil) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/business_flow/cacheable.rb', line 43

def cache_ttl(ttl = nil)
  if ttl
    cache_options.ttl = ttl
  else
    cache_options.ttl
  end
end

#execute(flow) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/business_flow/cacheable.rb', line 59

def execute(flow)
  cache_store.fetch(flow.cache_key, cache_options.to_store_options) do
    super(flow)
    raise FlowFailedException, flow if flow.errors.any?
    flow
  end
rescue FlowFailedException
  flow
end