module BusinessFlow::Cacheable::ClassMethods

DSL Methods

Constants

CacheOptions

Responsible for converting our DSL options into cache store options

RESULT_FINALIZE

Public Instance Methods

add_cache_key_to_result_class() click to toggle source
# File lib/business_flow/cacheable.rb, line 99
def add_cache_key_to_result_class
  return if @cache_key_added
  result_class = const_get(:Result)
  DSL::PublicField.new(:cache_key).add_to(result_class)
  result_class.send(:define_method, :_business_flow_cacheable_finalize,
                    RESULT_FINALIZE)
  @cache_key_added = true
end
cache_key(key = nil) click to toggle source
# File lib/business_flow/cacheable.rb, line 53
def cache_key(key = nil)
  if key
    @cache_key = Callable.new(key)
  else
    @cache_key ||= default_cache_key
  end
end
cache_options() click to toggle source
# File lib/business_flow/cacheable.rb, line 27
def cache_options
  @cache_options ||= CacheOptions.new
end
cache_store(store = nil) click to toggle source
# 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) click to toggle source
# File lib/business_flow/cacheable.rb, line 43
def cache_ttl(ttl = nil)
  if ttl.is_a?(Numeric)
    cache_options.ttl = proc { ttl }
  elsif ttl
    cache_options.ttl = Callable.new(ttl)
  else
    cache_options.ttl
  end
end
default_cache_key() click to toggle source

:reek: UtilityFunction

# File lib/business_flow/cacheable.rb, line 62
def default_cache_key
  Callable.new(:_business_flow_dsl_parameters)
end
execute(flow) click to toggle source
Calls superclass method
# File lib/business_flow/cacheable.rb, line 66
def execute(flow)
  with_cache(flow) do
    super(flow)._business_flow_cacheable_finalize(flow.cache_key)
  end
rescue FlowFailedException => exc
  exc.flow
end
instrument_cache_fetch(flow) { || ... } click to toggle source
# File lib/business_flow/cacheable.rb, line 82
def instrument_cache_fetch(flow)
  instrument(:cache, flow) do |payload|
    payload[:cache_hit] = true if payload
    cache_store.fetch(flow.cache_key,
                      cache_options.to_store_options(flow)) do
      payload[:cache_hit] = false if payload
      yield
    end
  end
end
with_cache(flow, &blk) click to toggle source
# File lib/business_flow/cacheable.rb, line 74
def with_cache(flow, &blk)
  add_cache_key_to_result_class
  catch(:halt_step) do
    return instrument_cache_fetch(flow, &blk)
  end
  raise FlowFailedException, flow
end