[#780] using a changing cache key to avoid the need for explicit cache invalidation

This should work on all cache stores, since only the basic cache operations fetch and delete are used. The methods clear_cache and check_cache can no longer be supported. To sweep the whole cache Rails.cache.clear may be used. To invalidate the Settings only, you may use Setting.first.try(:touch).
pull/41/head
Gregor Schmidt 13 years ago
parent ee543489f1
commit d76fd8e499
  1. 28
      app/models/setting.rb

@ -97,13 +97,13 @@ class Setting < ActiveRecord::Base
# Returns the value of the setting named name # Returns the value of the setting named name
def self.[](name) def self.[](name)
Marshal.load(Rails.cache.fetch("chiliproject/setting/#{name}") {Marshal.dump(find_or_default(name).value)}) Marshal.load(Rails.cache.fetch(self.cache_key(name)) {Marshal.dump(find_or_default(name).value)})
end end
def self.[]=(name, v) def self.[]=(name, v)
setting = find_or_default(name) setting = find_or_default(name)
setting.value = (v ? v : "") setting.value = (v ? v : "")
Rails.cache.delete "chiliproject/setting/#{name}" Rails.cache.delete self.cache_key(name)
setting.save setting.save
setting.value setting.value
end end
@ -137,23 +137,19 @@ class Setting < ActiveRecord::Base
Object.const_defined?(:OpenID) && self[:openid].to_i > 0 Object.const_defined?(:OpenID) && self[:openid].to_i > 0
end end
# Checks if settings have changed since the values were read # Deprecation Warning: This method is no longer available. There is no
# and clears the cache hash if it's the case # replacement.
# Called once per request
def self.check_cache def self.check_cache
settings_updated_on = Setting.maximum(:updated_on) ActiveSupport::Deprecation.warn "The Setting.check_cache method is " +
cache_cleared_on = Rails.cache.read('chiliproject/setting-cleared_on') "deprecated and will be removed in the future. There should be no " +
cache_cleared_on = cache_cleared_on ? Marshal.load(cache_cleared_on) : Time.now "replacement for this functionality needed."
if settings_updated_on && cache_cleared_on <= settings_updated_on
clear_cache
end
end end
# Clears all of the Setting caches # Clears all of the Setting caches
def self.clear_cache def self.clear_cache
Rails.cache.delete_matched( /^chiliproject\/setting\/.+$/ ) ActiveSupport::Deprecation.warn "The Setting.clear_cache method is " +
Rails.cache.write('chiliproject/setting-cleared_on', Marshal.dump(Time.now)) "deprecated and will be removed in the future. There should be no " +
logger.info 'Settings cache cleared.' if logger "replacement for this functionality needed."
end end
private private
@ -165,4 +161,8 @@ private
setting = find_by_name(name) setting = find_by_name(name)
setting ||= new(:name => name, :value => @@available_settings[name]['default']) if @@available_settings.has_key? name setting ||= new(:name => name, :value => @@available_settings[name]['default']) if @@available_settings.has_key? name
end end
def self.cache_key(name)
"chiliproject/setting/#{Setting.maximum(:updated_on).to_i}/#{name}"
end
end end

Loading…
Cancel
Save