Module: ArtirixDataModels::CacheService

Defined in:
lib/artirix_data_models/cache_service.rb

Defined Under Namespace

Classes: InvalidServiceError

Class Method Summary collapse

Class Method Details

.build_serviceObject



20
21
22
23
24
25
# File 'lib/artirix_data_models/cache_service.rb', line 20

def self.build_service
  ArtirixCacheService::Service.new.tap do |service|
    set_key_prefix_from_config(service)
    set_options_from_config(service)
  end
end

.digest_element(element) ⇒ Object



48
49
50
# File 'lib/artirix_data_models/cache_service.rb', line 48

def self.digest_element(element)
  service.digest element
end

.expire_cache(pattern = nil, add_wildcard: true, add_prefix: true) ⇒ Object

we use ‘delete_matched` method -> it will work fine with Redis but it seems that it won’t with Memcached



74
75
76
77
78
79
80
# File 'lib/artirix_data_models/cache_service.rb', line 74

def self.expire_cache(pattern = nil, add_wildcard: true, add_prefix: true)
  return false unless ArtirixDataModels.cache.present?

  p = final_pattern(pattern, add_wildcard: add_wildcard, add_prefix: add_prefix)

  ArtirixDataModels.cache.delete_matched p
end

.final_pattern(pattern, add_wildcard: true, add_prefix: true) ⇒ Object



82
83
84
85
86
87
# File 'lib/artirix_data_models/cache_service.rb', line 82

def self.final_pattern(pattern, add_wildcard: true, add_prefix: true)
  p = pattern
  p = p.present? ? "#{p}*" : '' if add_wildcard
  p = "*#{service.key_prefix}*#{p}" if add_prefix
  p
end

.first_options(*options, return_if_missing: :default, **opts) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/artirix_data_models/cache_service.rb', line 52

def self.first_options(*options, return_if_missing: :default, **opts)
  if opts.key? :return_if_none
    ActiveSupport::Deprecation.warn('use `return_if_missing` instead of `return_if_none`')
    return_if_missing = opts[:return_if_none]
  end

  service.options *options, return_if_missing: return_if_missing
end

.key(*given_args) ⇒ Object



61
62
63
# File 'lib/artirix_data_models/cache_service.rb', line 61

def self.key(*given_args)
  service.key *given_args
end

.method_missing(m, *args, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/artirix_data_models/cache_service.rb', line 90

def self.method_missing(m, *args, &block)
  method = m.to_s

  if method.end_with? '_key'
    ActiveSupport::Deprecation.warn('using method_missing with `service.some_key("1", "2")` is deprecated, use this instead: `service.key(:some, "1", "2")`')
    key = method.gsub(/_key$/, '')
    self.key key, *args

  elsif method.end_with? '_options'
    ActiveSupport::Deprecation.warn('using method_missing with `service.some_options` is deprecated, use this instead: `service.options(:some)`')
    options_name = method.gsub(/_options$/, '')
    self.options options_name

  else
    super
  end
end

.options(options_name) ⇒ Object



65
66
67
# File 'lib/artirix_data_models/cache_service.rb', line 65

def self.options(options_name)
  service.registered_options options_name
end

.options?(options_name) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/artirix_data_models/cache_service.rb', line 69

def self.options?(options_name)
  service.registered_options? options_name
end

.reload_serviceObject



15
16
17
18
# File 'lib/artirix_data_models/cache_service.rb', line 15

def self.reload_service
  @service = nil
  service
end

.respond_to_missing?(m, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/artirix_data_models/cache_service.rb', line 108

def self.respond_to_missing?(m, include_all = false)
  method = m.to_s

  if method.end_with? '_key'
    ActiveSupport::Deprecation.warn('using method_missing with `service.some_key("1", "2")` is deprecated, use this instead: `service.key(:some, "1", "2")`')
    true

  elsif method.end_with? '_options'
    ActiveSupport::Deprecation.warn('using method_missing with `service.some_options` is deprecated, use this instead: `service.options(:some)`')
    options_name = method.gsub(/_options$/, '')
    self.options options_name

  else
    super
  end
end

.serviceObject



3
4
5
# File 'lib/artirix_data_models/cache_service.rb', line 3

def self.service
  @service ||= build_service
end

.set_key_prefix_from_config(service) ⇒ Object



27
28
29
30
31
32
# File 'lib/artirix_data_models/cache_service.rb', line 27

def self.set_key_prefix_from_config(service)
  prefix = ArtirixDataModels.configuration.try(:cache_app_prefix)
  if prefix
    service.register_key_prefix "#{prefix}__"
  end
end

.set_options_from_config(service) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/artirix_data_models/cache_service.rb', line 34

def self.set_options_from_config(service)
  options = ArtirixDataModels.configuration.try(:cache_options)
  if options
    options = options.to_hash if options.respond_to?(:to_hash) && !options.respond_to?(:each)
    options.each do |name, opts|
      if name.to_s == 'default_options'
        service.register_default_options opts
      else
        service.register_options name, opts
      end
    end
  end
end

.use_cache_service(artirix_cache_service) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/artirix_data_models/cache_service.rb', line 7

def self.use_cache_service(artirix_cache_service)
  unless artirix_cache_service.kind_of? ArtirixCacheService::Service
    raise InvalidServiceError, "expected ArtirixCacheService::Service, given #{artirix_cache_service.inspect}"
  end

  @service = artirix_cache_service
end