Created
February 24, 2012 17:45
-
-
Save dustMason/1902335 to your computer and use it in GitHub Desktop.
CachedFinder Extension for ActiveRecord
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
require 'digest/sha1' | |
module Extensions | |
module CachedFinder | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def cached(options = {}) | |
options_hash = Digest::SHA1.hexdigest(options.to_s) | |
Rails.cache.fetch [self.class.to_s.underscore, options_hash].join('/'), :expires_in => 1.hour, :race_condition_ttl => 10 do | |
all options | |
end | |
end | |
def clear_cache(options = {}) | |
options_hash = Digest::SHA1.hexdigest(options.to_s) | |
Rails.cache.delete [self.class.to_s.underscore, options_hash].join('/') | |
end | |
end | |
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
class Configurator < ActiveRecord::Base | |
include Extensions::CachedFinder | |
after_update :clear_cache | |
def self.get(label) | |
cached(:conditions => ['label = ?',label]).first.value | |
end | |
private | |
def clear_cache | |
self.class.clear_cache(:conditions => ['label = ?',label]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment