Class: LcpRuby::DataSource::CachedWrapper

Inherits:
Base
  • Object
show all
Defined in:
lib/lcp_ruby/data_source/cached_wrapper.rb

Overview

Decorator that wraps any DataSource::Base with Rails.cache caching. Supports separate TTL for individual records vs. list/search results.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#destroy, #save

Constructor Details

#initialize(inner, model_name:, ttl: 300, list_ttl: 60, stale_on_error: true) ⇒ CachedWrapper

Returns a new instance of CachedWrapper.



8
9
10
11
12
13
14
# File 'lib/lcp_ruby/data_source/cached_wrapper.rb', line 8

def initialize(inner, model_name:, ttl: 300, list_ttl: 60, stale_on_error: true)
  @inner = inner
  @model_name = model_name
  @ttl = ttl
  @list_ttl = list_ttl
  @stale_on_error = stale_on_error
end

Instance Attribute Details

#innerObject (readonly)

Returns the value of attribute inner.



6
7
8
# File 'lib/lcp_ruby/data_source/cached_wrapper.rb', line 6

def inner
  @inner
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



6
7
8
# File 'lib/lcp_ruby/data_source/cached_wrapper.rb', line 6

def model_name
  @model_name
end

Instance Method Details

#count(params = {}) ⇒ Object



44
45
46
# File 'lib/lcp_ruby/data_source/cached_wrapper.rb', line 44

def count(params = {})
  @inner.count(params)
end

#find(id) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/lcp_ruby/data_source/cached_wrapper.rb', line 16

def find(id)
  cache_key = record_cache_key(id)
  cached_fetch(cache_key, ttl: @ttl) { @inner.find(id) }
rescue ConnectionError
  return stale_record(id, cache_key) if @stale_on_error
  raise
end

#find_many(ids) ⇒ Object



24
25
26
# File 'lib/lcp_ruby/data_source/cached_wrapper.rb', line 24

def find_many(ids)
  @inner.find_many(ids)
end

#search(params = {}, sort: nil, page: 1, per: 25) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/lcp_ruby/data_source/cached_wrapper.rb', line 28

def search(params = {}, sort: nil, page: 1, per: 25)
  cache_key = search_cache_key(params, sort, page, per)
  cached_fetch(cache_key, ttl: @list_ttl) { @inner.search(params, sort: sort, page: page, per: per) }
rescue ConnectionError
  return stale_search(cache_key) if @stale_on_error
  raise
end

#select_options(search: nil, filter: {}, sort: nil, label_method: "to_label", limit: 200) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/lcp_ruby/data_source/cached_wrapper.rb', line 36

def select_options(search: nil, filter: {}, sort: nil, label_method: "to_label", limit: 200)
  cache_key = select_options_cache_key(search, filter, sort, label_method, limit)
  cached_fetch(cache_key, ttl: @list_ttl) { @inner.select_options(search: search, filter: filter, sort: sort, label_method: label_method, limit: limit) }
rescue ConnectionError
  return [] if @stale_on_error
  raise
end

#supported_operatorsObject



52
53
54
# File 'lib/lcp_ruby/data_source/cached_wrapper.rb', line 52

def supported_operators
  @inner.supported_operators
end

#writable?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/lcp_ruby/data_source/cached_wrapper.rb', line 48

def writable?
  @inner.writable?
end