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) ⇒ Object
- #cache_options ⇒ Object
- #cache_store(store = nil) ⇒ Object
- #cache_ttl(ttl = nil) ⇒ Object
- #execute(flow) ⇒ Object
- #instrument_cache_fetch(flow) ⇒ Object
- #with_cache(flow, &blk) ⇒ Object
Instance Method Details
#add_cache_key_to_result_class ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/business_flow/cacheable.rb', line 94 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) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/business_flow/cacheable.rb', line 53 def cache_key(key = nil) if key @cache_key = Callable.new(key) else @cache_key ||= Callable.new(:parameter_object) end end |
#cache_options ⇒ Object
27 28 29 |
# File 'lib/business_flow/cacheable.rb', line 27 def ||= 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 50 51 |
# File 'lib/business_flow/cacheable.rb', line 43 def cache_ttl(ttl = nil) if ttl.is_a?(Numeric) .ttl = proc { ttl } elsif ttl .ttl = Callable.new(ttl) else .ttl end end |
#execute(flow) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/business_flow/cacheable.rb', line 61 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
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/business_flow/cacheable.rb', line 77 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
69 70 71 72 73 74 75 |
# File 'lib/business_flow/cacheable.rb', line 69 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 |