2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-16 23:09:50 -05:00
|
|
|
# The ReactiveCaching concern is used to fetch some data in the background and
|
|
|
|
# store it in the Rails cache, keeping it up-to-date for as long as it is being
|
|
|
|
# requested. If the data hasn't been requested for +reactive_cache_lifetime+,
|
|
|
|
# it stop being refreshed, and then be removed.
|
|
|
|
#
|
|
|
|
# Example of use:
|
|
|
|
#
|
2019-03-28 09:17:42 -04:00
|
|
|
# class Foo < ApplicationRecord
|
2016-12-16 23:09:50 -05:00
|
|
|
# include ReactiveCaching
|
|
|
|
#
|
|
|
|
# after_save :clear_reactive_cache!
|
|
|
|
#
|
|
|
|
# def calculate_reactive_cache
|
|
|
|
# # Expensive operation here. The return value of this method is cached
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def result
|
|
|
|
# with_reactive_cache do |data|
|
|
|
|
# # ...
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# In this example, the first time `#result` is called, it will return `nil`.
|
|
|
|
# However, it will enqueue a background worker to call `#calculate_reactive_cache`
|
|
|
|
# and set an initial cache lifetime of ten minutes.
|
|
|
|
#
|
2019-04-04 13:27:29 -04:00
|
|
|
# The background worker needs to find or generate the object on which
|
|
|
|
# `with_reactive_cache` was called.
|
|
|
|
# The default behaviour can be overridden by defining a custom
|
|
|
|
# `reactive_cache_worker_finder`.
|
|
|
|
# Otherwise the background worker will use the class name and primary key to get
|
|
|
|
# the object using the ActiveRecord find_by method.
|
|
|
|
#
|
|
|
|
# class Bar
|
|
|
|
# include ReactiveCaching
|
|
|
|
#
|
|
|
|
# self.reactive_cache_key = ->() { ["bar", "thing"] }
|
|
|
|
# self.reactive_cache_worker_finder = ->(_id, *args) { from_cache(*args) }
|
|
|
|
#
|
|
|
|
# def self.from_cache(var1, var2)
|
|
|
|
# # This method will be called by the background worker with "bar1" and
|
|
|
|
# # "bar2" as arguments.
|
|
|
|
# new(var1, var2)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def initialize(var1, var2)
|
|
|
|
# # ...
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def calculate_reactive_cache
|
|
|
|
# # Expensive operation here. The return value of this method is cached
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def result
|
|
|
|
# with_reactive_cache("bar1", "bar2") do |data|
|
|
|
|
# # ...
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2016-12-16 23:09:50 -05:00
|
|
|
# Each time the background job completes, it stores the return value of
|
|
|
|
# `#calculate_reactive_cache`. It is also re-enqueued to run again after
|
|
|
|
# `reactive_cache_refresh_interval`, so keeping the stored value up to date.
|
|
|
|
# Calculations are never run concurrently.
|
|
|
|
#
|
|
|
|
# Calling `#result` while a value is in the cache will call the block given to
|
|
|
|
# `#with_reactive_cache`, yielding the cached value. It will also extend the
|
|
|
|
# lifetime by `reactive_cache_lifetime`.
|
|
|
|
#
|
|
|
|
# Once the lifetime has expired, no more background jobs will be enqueued and
|
|
|
|
# calling `#result` will again return `nil` - starting the process all over
|
|
|
|
# again
|
|
|
|
module ReactiveCaching
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2018-08-07 10:02:57 -04:00
|
|
|
InvalidateReactiveCache = Class.new(StandardError)
|
|
|
|
|
2016-12-16 23:09:50 -05:00
|
|
|
included do
|
|
|
|
class_attribute :reactive_cache_lease_timeout
|
|
|
|
|
|
|
|
class_attribute :reactive_cache_key
|
|
|
|
class_attribute :reactive_cache_lifetime
|
|
|
|
class_attribute :reactive_cache_refresh_interval
|
2019-04-04 13:27:29 -04:00
|
|
|
class_attribute :reactive_cache_worker_finder
|
2016-12-16 23:09:50 -05:00
|
|
|
|
|
|
|
# defaults
|
2019-06-07 03:17:33 -04:00
|
|
|
self.reactive_cache_key = -> (record) { [model_name.singular, record.id] }
|
|
|
|
|
2016-12-16 23:09:50 -05:00
|
|
|
self.reactive_cache_lease_timeout = 2.minutes
|
|
|
|
|
|
|
|
self.reactive_cache_refresh_interval = 1.minute
|
|
|
|
self.reactive_cache_lifetime = 10.minutes
|
|
|
|
|
2019-04-04 13:27:29 -04:00
|
|
|
self.reactive_cache_worker_finder = ->(id, *_args) do
|
|
|
|
find_by(primary_key => id)
|
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
def calculate_reactive_cache(*args)
|
2016-11-22 14:55:56 -05:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2018-07-25 06:03:18 -04:00
|
|
|
def reactive_cache_updated(*args)
|
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
def with_reactive_cache(*args, &blk)
|
2018-08-07 10:02:57 -04:00
|
|
|
unless within_reactive_cache_lifetime?(*args)
|
|
|
|
refresh_reactive_cache!(*args)
|
2019-02-08 07:19:53 -05:00
|
|
|
return
|
2018-08-07 10:02:57 -04:00
|
|
|
end
|
2018-05-08 02:07:48 -04:00
|
|
|
|
2018-08-07 10:02:57 -04:00
|
|
|
keep_alive_reactive_cache!(*args)
|
|
|
|
|
|
|
|
begin
|
2017-01-12 17:31:02 -05:00
|
|
|
data = Rails.cache.read(full_reactive_cache_key(*args))
|
2019-02-18 06:24:00 -05:00
|
|
|
yield data unless data.nil?
|
2018-08-07 10:02:57 -04:00
|
|
|
rescue InvalidateReactiveCache
|
|
|
|
refresh_reactive_cache!(*args)
|
|
|
|
nil
|
2016-12-16 23:09:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
def clear_reactive_cache!(*args)
|
|
|
|
Rails.cache.delete(full_reactive_cache_key(*args))
|
2018-06-01 09:15:13 -04:00
|
|
|
Rails.cache.delete(alive_reactive_cache_key(*args))
|
2016-12-16 23:09:50 -05:00
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
def exclusively_update_reactive_cache!(*args)
|
|
|
|
locking_reactive_cache(*args) do
|
2018-05-08 02:07:48 -04:00
|
|
|
if within_reactive_cache_lifetime?(*args)
|
2017-01-12 17:31:02 -05:00
|
|
|
enqueuing_update(*args) do
|
2018-07-25 06:03:18 -04:00
|
|
|
key = full_reactive_cache_key(*args)
|
|
|
|
new_value = calculate_reactive_cache(*args)
|
|
|
|
old_value = Rails.cache.read(key)
|
|
|
|
Rails.cache.write(key, new_value)
|
|
|
|
reactive_cache_updated(*args) if new_value != old_value
|
2016-12-16 23:09:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-08-07 10:02:57 -04:00
|
|
|
def refresh_reactive_cache!(*args)
|
|
|
|
clear_reactive_cache!(*args)
|
|
|
|
keep_alive_reactive_cache!(*args)
|
|
|
|
ReactiveCachingWorker.perform_async(self.class, id, *args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def keep_alive_reactive_cache!(*args)
|
|
|
|
Rails.cache.write(alive_reactive_cache_key(*args), true, expires_in: self.class.reactive_cache_lifetime)
|
|
|
|
end
|
|
|
|
|
2016-12-16 23:09:50 -05:00
|
|
|
def full_reactive_cache_key(*qualifiers)
|
|
|
|
prefix = self.class.reactive_cache_key
|
|
|
|
prefix = prefix.call(self) if prefix.respond_to?(:call)
|
|
|
|
|
|
|
|
([prefix].flatten + qualifiers).join(':')
|
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
def alive_reactive_cache_key(*qualifiers)
|
|
|
|
full_reactive_cache_key(*(qualifiers + ['alive']))
|
|
|
|
end
|
|
|
|
|
|
|
|
def locking_reactive_cache(*args)
|
|
|
|
lease = Gitlab::ExclusiveLease.new(full_reactive_cache_key(*args), timeout: reactive_cache_lease_timeout)
|
2016-12-16 23:09:50 -05:00
|
|
|
uuid = lease.try_obtain
|
|
|
|
yield if uuid
|
|
|
|
ensure
|
2017-01-12 17:31:02 -05:00
|
|
|
Gitlab::ExclusiveLease.cancel(full_reactive_cache_key(*args), uuid)
|
2016-12-16 23:09:50 -05:00
|
|
|
end
|
|
|
|
|
2018-05-08 02:07:48 -04:00
|
|
|
def within_reactive_cache_lifetime?(*args)
|
|
|
|
!!Rails.cache.read(alive_reactive_cache_key(*args))
|
2016-12-16 23:09:50 -05:00
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
def enqueuing_update(*args)
|
2016-12-16 23:09:50 -05:00
|
|
|
yield
|
|
|
|
ensure
|
2017-01-12 17:31:02 -05:00
|
|
|
ReactiveCachingWorker.perform_in(self.class.reactive_cache_refresh_interval, self.class, id, *args)
|
2016-12-16 23:09:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|