Last active
December 15, 2015 14:09
-
-
Save listrophy/5272339 to your computer and use it in GitHub Desktop.
Quick, roll-your-own key-boolean caching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyApplication | |
class Application < Rails::Application | |
config.cache_store = :memory_store, {:size => 4.megabytes} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Setting.refresh if ActiveRecord::Base.connection.tables.include?('settings') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Setting < ActiveRecord::Base | |
attr_accessible :enabled, :name | |
after_save :cache | |
class << self | |
def refresh | |
Rails.cache.clear | |
all.each(&:cache) | |
end | |
def [] name | |
return Rails.cache.read(name) if Rails.cache.exist?(name) | |
if setting = where(name: name).first | |
setting.cache | |
setting.enabled | |
else | |
false | |
end | |
end | |
end | |
def cache | |
Rails.cache.write name, enabled | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment