Skip to content

Instantly share code, notes, and snippets.

@listrophy
Last active December 15, 2015 14:09
Show Gist options
  • Save listrophy/5272339 to your computer and use it in GitHub Desktop.
Save listrophy/5272339 to your computer and use it in GitHub Desktop.
Quick, roll-your-own key-boolean caching
module MyApplication
class Application < Rails::Application
config.cache_store = :memory_store, {:size => 4.megabytes}
end
end
Setting.refresh if ActiveRecord::Base.connection.tables.include?('settings')
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