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
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
-
#default_cache_key ⇒ Object
:reek:UtilityFunction.
- #execute(flow) ⇒ Object
- #instrument_cache_fetch(flow) ⇒ Object
- #with_cache(flow, &blk) ⇒ Object
Instance Method Details
#add_cache_key_to_result_class ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/business_flow/cacheable.rb', line 102 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 ||= 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 |
#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
69 70 71 72 73 74 75 |
# File 'lib/business_flow/cacheable.rb', line 69 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
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/business_flow/cacheable.rb', line 85 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
77 78 79 80 81 82 83 |
# File 'lib/business_flow/cacheable.rb', line 77 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 |