summaryrefslogtreecommitdiff
path: root/app/models/concerns/cacheable_attributes.rb
blob: ee56322cce7c0d1b50b9f8956e59a01ee918d329 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true

module CacheableAttributes
  extend ActiveSupport::Concern

  included do
    after_commit { self.class.expire }

    private_class_method :request_store_cache_key
  end

  class_methods do
    def cache_key
      "#{name}:#{Gitlab::VERSION}:#{Rails.version}"
    end

    # Can be overridden
    def current_without_cache
      last
    end

    # Can be overridden
    def defaults
      {}
    end

    def build_from_defaults(attributes = {})
      final_attributes = defaults
        .merge(attributes)
        .stringify_keys
        .slice(*column_names)

      new(final_attributes)
    end

    def cached
      Gitlab::SafeRequestStore[request_store_cache_key] ||= retrieve_from_cache
    end

    def request_store_cache_key
      :"#{name}_cached_attributes"
    end

    def retrieve_from_cache
      record = cache_backend.read(cache_key)
      ensure_cache_setup if record.present?

      record
    end

    def current
      cached_record = cached
      return cached_record if cached_record.present?

      current_without_cache.tap { |current_record| current_record&.cache! }
    rescue => e
      if Rails.env.production?
        Gitlab::AppLogger.warn("Cached record for #{name} couldn't be loaded, falling back to uncached record: #{e}")
      else
        raise e
      end
      # Fall back to an uncached value if there are any problems (e.g. Redis down)
      current_without_cache
    end

    def expire
      Gitlab::SafeRequestStore.delete(request_store_cache_key)
      cache_backend.delete(cache_key)
    rescue
      # Gracefully handle when Redis is not available. For example,
      # omnibus may fail here during gitlab:assets:compile.
    end

    def ensure_cache_setup
      # This is a workaround for a Rails bug that causes attribute methods not
      # to be loaded when read from cache: https://github.com/rails/rails/issues/27348
      define_attribute_methods
    end

    def cache_backend
      Rails.cache
    end
  end

  def cache!
    self.class.cache_backend.write(self.class.cache_key, self, expires_in: Gitlab.config.gitlab['application_settings_cache_seconds'] || 60)
  end
end