summaryrefslogtreecommitdiff
path: root/app/models/concerns/redis_cacheable.rb
blob: b54252951309cbbe667587d8196d61c258d6574a (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
module RedisCacheable
  extend ActiveSupport::Concern
  include Gitlab::Utils::StrongMemoize

  CACHED_ATTRIBUTES_EXPIRY_TIME = 24.hours

  class_methods do
    def cached_attr_reader(*attributes)
      attributes.each do |attribute|
        define_method(attribute) do
          unless self.has_attribute?(attribute)
            raise ArgumentError, "`cached_attr_reader` requires the #{self.class.name}\##{attribute} attribute to have a database column"
          end

          cached_attribute(attribute) || read_attribute(attribute)
        end
      end
    end
  end

  def cached_attribute(attribute)
    cached_value = (cached_attributes || {})[attribute]
    cast_value_from_cache(attribute, cached_value) if cached_value
  end

  def cache_attributes(values)
    Gitlab::Redis::SharedState.with do |redis|
      redis.set(cache_attribute_key, values.to_json, ex: CACHED_ATTRIBUTES_EXPIRY_TIME)
    end

    clear_memoization(:cached_attributes)
  end

  private

  def cache_attribute_key
    "cache:#{self.class.name}:#{self.id}:attributes"
  end

  def cached_attributes
    strong_memoize(:cached_attributes) do
      Gitlab::Redis::SharedState.with do |redis|
        data = redis.get(cache_attribute_key)
        JSON.parse(data, symbolize_names: true) if data
      end
    end
  end

  def cast_value_from_cache(attribute, value)
    if Gitlab.rails5?
      self.class.type_for_attribute(attribute).cast(value)
    else
      self.class.column_for_attribute(attribute).type_cast_from_database(value)
    end
  end
end