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
- #add_cache_key_to_result_class ⇒ Object
- #cache_key(key = nil, &blk) ⇒ Object
- #cache_options ⇒ Object
- #cache_store(store = nil) ⇒ Object
- #cache_ttl(ttl = nil) ⇒ Object
- #clear_cache(parameter_object = {}) ⇒ Object
- #clear_cache!(parameter_object = {}) ⇒ Object
-
#default_cache_key ⇒ Object
:reek:UtilityFunction.
- #execute(flow) ⇒ Object
- #instrument_cache_fetch(flow) ⇒ Object
- #with_cache(flow, &blk) ⇒ Object
Class Method Details
.clear_cache(flow) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/business_flow/cacheable.rb', line 82 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_class ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'lib/business_flow/cacheable.rb', line 125 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_options ⇒ Object
29 30 31 |
# File 'lib/business_flow/cacheable.rb', line 29 def @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) .ttl = proc { ttl } elsif ttl .ttl = Callable.new(ttl) else .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 => e e.flow end |
#clear_cache!(parameter_object = {}) ⇒ Object
75 76 77 78 79 80 |
# 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_key ⇒ Object
: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
91 92 93 94 95 96 97 |
# File 'lib/business_flow/cacheable.rb', line 91 def execute(flow) with_cache(flow) do super(flow)._business_flow_cacheable_finalize(flow.cache_key) end rescue FlowFailedException => e e.flow end |
#instrument_cache_fetch(flow) ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/business_flow/cacheable.rb', line 107 def instrument_cache_fetch(flow) instrument(:cache, flow) do |payload| payload[:cache_hit] = true if payload cache_store.fetch(flow.cache_key, .(flow)) do payload[:cache_hit] = false if payload yield end end end |
#with_cache(flow, &blk) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/business_flow/cacheable.rb', line 99 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 |