Module: Legion::Cache::Cacheable

Extended by:
Logging::Helper
Defined in:
lib/legion/cache/cacheable.rb

Constant Summary collapse

LOCAL_CACHE_MISS =
Object.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_cache_key(mod_name, method_name, exclude:, **kwargs) ⇒ Object



53
54
55
56
57
# File 'lib/legion/cache/cacheable.rb', line 53

def self.build_cache_key(mod_name, method_name, exclude:, **kwargs)
  filtered = kwargs.except(*exclude)
  args_hash = Digest::MD5.hexdigest(filtered.sort.to_s)
  "#{mod_name}.#{method_name}.#{args_hash}"
end

.cache_read(key, scope:) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/legion/cache/cacheable.rb', line 59

def self.cache_read(key, scope:)
  case scope
  when :global
    return Legion::Cache.get(key) if global_cache_available?

    memory_read(key)
  else
    result = local_cache_read(key)
    result.equal?(LOCAL_CACHE_MISS) ? memory_read(key) : result
  end
end

.cache_write(key, value, ttl:, scope:) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/legion/cache/cacheable.rb', line 71

def self.cache_write(key, value, ttl:, scope:)
  case scope
  when :global
    if global_cache_available?
      Legion::Cache.set(key, value, ttl: ttl, async: false)
    else
      memory_write(key, value, ttl)
    end
  else
    if local_cache_available?
      result = local_cache_write(key, value, ttl: ttl)
      memory_write(key, value, ttl) unless result
    else
      memory_write(key, value, ttl)
    end
  end
end

.extended(base) ⇒ Object



13
14
15
# File 'lib/legion/cache/cacheable.rb', line 13

def self.extended(base)
  base.instance_variable_set(:@cached_methods, {})
end

.global_cache_available?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/legion/cache/cacheable.rb', line 89

def self.global_cache_available?
  defined?(Legion::Cache) && Legion::Cache.respond_to?(:connected?) && Legion::Cache.connected?
end

.local_cache_available?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/legion/cache/cacheable.rb', line 93

def self.local_cache_available?
  defined?(Legion::Cache::Local) && Legion::Cache::Local.respond_to?(:connected?) && Legion::Cache::Local.connected?
end

.local_cache_read(key) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/legion/cache/cacheable.rb', line 97

def self.local_cache_read(key)
  return LOCAL_CACHE_MISS unless local_cache_available?

  result = Legion::Cache::Local.get(key)
  result.nil? ? LOCAL_CACHE_MISS : result
rescue StandardError => e
  handle_exception(e, level: :warn, operation: :local_cache_read, key: key)
  LOCAL_CACHE_MISS
end

.local_cache_write(key, value, ttl:) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/legion/cache/cacheable.rb', line 107

def self.local_cache_write(key, value, ttl:)
  return unless local_cache_available?

  Legion::Cache::Local.set(key, value, ttl: ttl, async: false)
rescue StandardError => e
  handle_exception(e, level: :warn, operation: :local_cache_write, key: key, ttl: ttl)
  nil
end

.memory_clear!Object



133
134
135
# File 'lib/legion/cache/cacheable.rb', line 133

def self.memory_clear!
  @memory_store = {}
end

.memory_read(key) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/legion/cache/cacheable.rb', line 121

def self.memory_read(key)
  entry = memory_store[key]
  return nil unless entry
  return nil if Time.now.utc > entry[:expires_at]

  entry[:value]
end

.memory_storeObject

In-memory fallback store (class-level, process-wide)



117
118
119
# File 'lib/legion/cache/cacheable.rb', line 117

def self.memory_store
  @memory_store ||= {}
end

.memory_write(key, value, ttl) ⇒ Object



129
130
131
# File 'lib/legion/cache/cacheable.rb', line 129

def self.memory_write(key, value, ttl)
  memory_store[key] = { value: value, expires_at: Time.now.utc + ttl }
end

Instance Method Details

#cache_method(method_name, ttl:, scope: :local, exclude_from_key: []) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/legion/cache/cacheable.rb', line 21

def cache_method(method_name, ttl:, scope: :local, exclude_from_key: [])
  exclude_from_key |= %i[token bypass_local_method_cache]
  cached_methods[method_name] = { ttl: ttl, scope: scope, exclude_from_key: exclude_from_key }

  mod_name = name || 'Anonymous'
  config = cached_methods[method_name]

  wrapper = Module.new do
    define_method(method_name) do |bypass_local_method_cache: false, **kwargs|
      key = Legion::Cache::Cacheable.build_cache_key(
        mod_name, method_name, exclude: config[:exclude_from_key], **kwargs
      )

      unless bypass_local_method_cache
        cached = Legion::Cache::Cacheable.cache_read(key, scope: config[:scope])
        if cached.nil?
          Legion::Cache::Cacheable.log.debug { "[cacheable] miss key=#{key}" }
        else
          Legion::Cache::Cacheable.log.debug { "[cacheable] hit key=#{key}" }
          return cached
        end
      end

      result = super(**kwargs)
      Legion::Cache::Cacheable.cache_write(key, result, ttl: config[:ttl], scope: config[:scope])
      result
    end
  end

  prepend wrapper
end

#cached_methodsObject



17
18
19
# File 'lib/legion/cache/cacheable.rb', line 17

def cached_methods
  @cached_methods ||= {}
end