Module: BusinessFlow::Cacheable::ClassMethods

Defined in:
lib/business_flow/cacheable.rb

Overview

DSL Methods

Defined Under Namespace

Classes: CacheOptions

Constant Summary collapse

RESULT_FINALIZE =
proc do |cache_key|
  @cache_key = cache_key
  raise FlowFailedException, self if errors?
  self
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_cache(flow) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/business_flow/cacheable.rb', line 81

def self.clear_cache(flow)
  klass = flow.class
  klass.instrument(:clear_cache, flow) do |payload|
    ret = klass.cache_store.delete(flow.cache_key)
    payload[:cache_clear] = ret if payload
    ret
  end
end

Instance Method Details

#add_cache_key_to_result_classObject



123
124
125
126
127
128
129
130
# File 'lib/business_flow/cacheable.rb', line 123

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, &blk) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/business_flow/cacheable.rb', line 55

def cache_key(key = nil, &blk)
  key ||= blk
  if key
    @cache_key = Callable.new(key)
  else
    @cache_key ||= default_cache_key
  end
end

#cache_optionsObject



29
30
31
# File 'lib/business_flow/cacheable.rb', line 29

def cache_options
  @cache_options ||= CacheOptions.new
end

#cache_store(store = nil) ⇒ Object



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

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



45
46
47
48
49
50
51
52
53
# File 'lib/business_flow/cacheable.rb', line 45

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

#clear_cache(parameter_object = {}) ⇒ Object



69
70
71
72
73
# File 'lib/business_flow/cacheable.rb', line 69

def clear_cache(parameter_object = {})
  clear_cache!(parameter_object)
rescue FlowFailedException => exc
  exc.flow
end

#clear_cache!(parameter_object = {}) ⇒ Object



75
76
77
78
79
# File 'lib/business_flow/cacheable.rb', line 75

def clear_cache!(parameter_object = {})
  flow = build(parameter_object)
  raise FlowFailedException, result_from(flow) if flow.errors?
  ClassMethods.clear_cache(flow)
end

#default_cache_keyObject

:reek:UtilityFunction



65
66
67
# File 'lib/business_flow/cacheable.rb', line 65

def default_cache_key
  Callable.new(:_business_flow_dsl_parameters)
end

#execute(flow) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/business_flow/cacheable.rb', line 90

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) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/business_flow/cacheable.rb', line 106

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) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/business_flow/cacheable.rb', line 98

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