Module: ArtirixDataModels::CacheService

Defined in:
lib/artirix_data_models/cache_service.rb

Class Method Summary collapse

Class Method Details

.digest_element(element) ⇒ Object



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

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



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

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



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

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



32
33
34
35
36
37
38
39
# File 'lib/artirix_data_models/cache_service.rb', line 32

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



41
42
43
# File 'lib/artirix_data_models/cache_service.rb', line 41

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

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/artirix_data_models/cache_service.rb', line 70

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



45
46
47
# File 'lib/artirix_data_models/cache_service.rb', line 45

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

.options?(options_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.reload_serviceObject



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

def self.reload_service
  @service = nil
  service
end

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

Returns:

  • (Boolean)


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

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/artirix_data_models/cache_service.rb', line 7

def self.service
  @service ||= ArtirixCacheService::Service.new.tap do |service|
    prefix = ArtirixDataModels.configuration.try(:cache_app_prefix)
    if prefix
      service.register_key_prefix "#{prefix}__"
    end

    options = ArtirixDataModels.configuration.try(:cache_options)
    if options
      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
end