Skip to content

Commit

Permalink
Add Middleware for only enable RequestCache in Rails request context. #…
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Sep 14, 2021
1 parent edbf1e9 commit 0849bb2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/rails-settings-cached.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative "rails-settings/base"
require_relative "rails-settings/request_cache"
require_relative "rails-settings/middleware"
require_relative "rails-settings/railtie"
require_relative "rails-settings/version"

Expand Down
2 changes: 1 addition & 1 deletion lib/rails-settings/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def rails_initialized?
end

def _all_settings
RequestCache.settings ||= Rails.cache.fetch(cache_key, expires_in: 1.week) do
RequestCache.all_settings ||= Rails.cache.fetch(cache_key, expires_in: 1.week) do
vars = unscoped.select("var, value")
result = {}
vars.each { |record| result[record.var] = record.value }
Expand Down
13 changes: 13 additions & 0 deletions lib/rails-settings/middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module RailsSettings
class Middleware
def initialize(app)
@app = app
end

def call(env)
RailsSettings::RequestCache.enable!
@app.call(env)
RailsSettings::RequestCache.disable!
end
end
end
4 changes: 4 additions & 0 deletions lib/rails-settings/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ class Railtie < Rails::Railtie
initializer "rails_settings.active_record.initialization" do
RailsSettings::Base.after_commit :clear_cache, on: %i[create update destroy]
end

initializer "rails_settings.configure_rails_initialization" do |app|
app.middleware.use RailsSettings::Middleware
end
end
end
29 changes: 29 additions & 0 deletions lib/rails-settings/request_cache.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
module RailsSettings
module RequestCacheGetter
extend ActiveSupport::Concern

class_methods do
def enable!
Thread.current[:rails_settings_request_cache_enable] = true
end

def disable!
Thread.current[:rails_settings_request_cache_enable] = nil
end

def enabled?
Thread.current[:rails_settings_request_cache_enable]
end

def all_settings
enabled? ? settings : nil
end

def all_settings=(val)
self.settings = val
end
end
end

if defined? ActiveSupport::CurrentAttributes
# For storage all settings in Current, it will reset after per request completed.
# Base on ActiveSupport::CurrentAttributes
# https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html
class RequestCache < ActiveSupport::CurrentAttributes
include RequestCacheGetter
attribute :settings
end
else
Expand All @@ -12,6 +39,8 @@ class RequestCache < ActiveSupport::CurrentAttributes
require "request_store"

class RequestCache
include RequestCacheGetter

class << self
def reset
self.settings = nil
Expand Down

0 comments on commit 0849bb2

Please sign in to comment.