Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix global site settings cache #1517

Merged
merged 5 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/site_settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def update
@setting.update(setting_params)
AuditLog.admin_audit(event_type: 'setting_update', related: @setting, user: current_user,
comment: "from <<SiteSetting #{before}>>\nto <<SiteSetting #{@setting.attributes_print}>>")
Rails.cache.delete "SiteSettings/#{RequestContext.community_id}/#{@setting.name}"
Rails.cache.delete "SiteSettings/#{RequestContext.community_id}/#{@setting.name}", include_community: false
render json: { status: 'OK', setting: @setting&.as_json&.merge(typed: @setting.typed) }
end

Expand Down
12 changes: 6 additions & 6 deletions app/models/site_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ class SiteSetting < ApplicationRecord

def self.[](name)
key = "SiteSettings/#{RequestContext.community_id}/#{name}"
cached = Rails.cache.fetch key do
cached = Rails.cache.fetch key, include_community: false do
SiteSetting.applied_setting(name)&.typed
end

if cached.nil?
Rails.cache.delete key
Rails.cache.delete key, include_community: false
value = SiteSetting.applied_setting(name)&.typed
Rails.cache.write key, value
Rails.cache.write key, value, include_community: false
value
else
cached
end
end

def self.exist?(name)
Rails.cache.exist?("SiteSettings/#{RequestContext.community_id}/#{name}") ||
Rails.cache.exist?("SiteSettings/#{RequestContext.community_id}/#{name}", include_community: false) ||
SiteSetting.where(name: name).count.positive?
end

Expand All @@ -45,14 +45,14 @@ def self.applied_setting(name)
def self.all_communities(name)
communities = Community.all
keys = (communities.map { |c| [c.id, "SiteSetting/#{c.id}/#{name}"] } + [[nil, "SiteSetting//#{name}"]]).to_h
cached = Rails.cache.read_multi(*keys.values)
cached = Rails.cache.read_multi(*keys.values, include_community: false)
missing = keys.reject { |_k, v| cached.include?(v) }.map { |k, _v| k }
settings = if missing.empty?
{}
else
SiteSetting.where(name: name, community_id: missing).to_h { |s| [s.community_id, s] }
end
Rails.cache.write_multi(missing.to_h { |cid| [keys[cid], settings[cid]&.typed] })
Rails.cache.write_multi(missing.to_h { |cid| [keys[cid], settings[cid]&.typed] }, include_community: false)
communities.to_h do |c|
[
c.id,
Expand Down
16 changes: 10 additions & 6 deletions lib/namespaced_env_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ def initialize(underlying)
# These methods need the cache key name updating before we pass it to the underlying cache.
[:decrement, :delete, :exist?, :fetch, :increment, :read, :write, :delete_matched].each do |method|
define_method method do |name, *args, **opts, &block|
@underlying.send(method, construct_ns_key(name, include_community: opts.delete(:include_community) || true),
include_community = opts.delete(:include_community)
@underlying.send(method, construct_ns_key(name, include_community: include_community),
*args, **opts, &block)
end
end

# These methods need a hash of cache keys updating before we pass it to the underlying cache.
[:write_multi].each do |method|
define_method method do |hash, *args, **opts, &block|
hash = hash.map { |k, v| [construct_ns_key(k), v] }.to_h
include_community = opts.delete(:include_community)
hash = hash.map { |k, v| [construct_ns_key(k, include_community: include_community), v] }.to_h
@underlying.send(method, hash, *args, **opts, &block)
end
end
Expand All @@ -29,14 +31,16 @@ def initialize(underlying)
end

def read_multi(*keys, **opts)
keys = keys.map { |k| [construct_ns_key(k), k] }.to_h
results = @underlying.read_multi *keys.keys, **opts
include_community = opts.delete(:include_community)
keys = keys.map { |k| [construct_ns_key(k, include_community: include_community), k] }.to_h
results = @underlying.read_multi(*keys.keys, **opts)
results.map { |k, v| [keys[k], v] }.to_h
end

def fetch_multi(*keys, **opts, &block)
keys = keys.map { |k| construct_ns_key(k) }
@underlying.fetch_multi *keys, **opts, &block
include_community = opts.delete(:include_community)
keys = keys.map { |k| construct_ns_key(k, include_community: include_community) }
@underlying.fetch_multi(*keys, **opts, &block)
end

def persistent(name, **opts, &block)
Expand Down
Loading