Remove ActiveRecord::Base.find(_all)?_cached

Just use Rails.cache.fetch directly.  This fixes some problems that
find_cached wasn't fully serializing the object.

Also, be sure to change cache keys so that we don't get data back
using the old format.

Change-Id: I8beea2f2ba446a97249a495789b25c3a71de420e
Reviewed-on: https://gerrit.instructure.com/5857
Tested-by: Hudson <hudson@instructure.com>
Reviewed-by: Brian Palmer <brianp@instructure.com>
This commit is contained in:
Cody Cutrer 2011-09-27 14:36:33 -06:00 committed by Brian Palmer
parent 316dfa3ead
commit 5c08799256
4 changed files with 4 additions and 40 deletions

View File

@ -311,7 +311,7 @@ class Account < ActiveRecord::Base
end end
def self.account_lookup_cache_key(id) def self.account_lookup_cache_key(id)
['_account_lookup', id].cache_key ['_account_lookup2', id].cache_key
end end
def find_user_by_unique_id(unique_id) def find_user_by_unique_id(unique_id)

View File

@ -27,7 +27,7 @@ class AccountNotification < ActiveRecord::Base
closed_ids = (user && user.preferences[:closed_notifications]) || [] closed_ids = (user && user.preferences[:closed_notifications]) || []
now = Time.now.utc now = Time.now.utc
# Refreshes every 10 minutes at the longest # Refreshes every 10 minutes at the longest
current = AccountNotification.find_all_cached(['account_notifications', account, (now.to_i / 600).to_s].cache_key) do current = Rails.cache.fetch(['account_notifications2', account].cache_key, :expires_in => 10.minutes) do
AccountNotification.find(:all, :conditions => ['account_id IN (?,?) AND start_at < ? AND end_at > ?', Account.site_admin.id, account.id, now, now], :order => 'start_at DESC') AccountNotification.find(:all, :conditions => ['account_id IN (?,?) AND start_at < ? AND end_at > ?', Account.site_admin.id, account.id, now, now], :order => 'start_at DESC')
end end
current ||= [] current ||= []

View File

@ -477,7 +477,7 @@ class User < ActiveRecord::Base
end end
def self.user_lookup_cache_key(id) def self.user_lookup_cache_key(id)
['_user_lookup', id].cache_key ['_user_lookup2', id].cache_key
end end
def self.invalidate_cache(id) def self.invalidate_cache(id)
@ -527,7 +527,7 @@ class User < ActiveRecord::Base
def self.cached_name(id) def self.cached_name(id)
key = user_lookup_cache_key(id) key = user_lookup_cache_key(id)
user = find_cached(key) do user = Rails.cache.fetch(key) do
User.find_by_id(id) User.find_by_id(id)
end end
user && user.name user && user.name

View File

@ -57,42 +57,6 @@ class ActiveRecord::Base
res res
end end
def self.find_cached(key, opts = nil, &block)
attrs = Rails.cache.read(key)
if !attrs || attrs.empty? || attrs.is_a?(String) || attrs[:assigned_cache_key] != key
obj = block.call rescue nil
attrs = obj && obj.is_a?(self) ? obj.attributes : nil
attrs[:assigned_cache_key] = key if attrs
Rails.cache.write(key, attrs, opts) if attrs
end
return nil if !attrs || attrs.empty?
obj = self.new
attrs = attrs.dup if attrs.frozen?
attrs.delete(:assigned_cache_key)
obj.instance_variable_set("@attributes", attrs)
obj.instance_variable_set("@new_record", false)
obj
end
def self.find_all_cached(key, opts = nil, &block)
attrs_list = Rails.cache.read(key)
if !attrs_list || attrs_list.empty? || !attrs_list.is_a?(Array) || attrs_list.any?{|attr| attr[:assigned_cache_key] != key }
list = block.call.to_a rescue nil
attrs_list = list.map{|obj| obj && obj.is_a?(self) ? obj.attributes : nil }.compact
attrs_list.each{|attrs| attrs[:assigned_cache_key] = key }
Rails.cache.write(key, attrs_list, opts)
end
return [] if !attrs_list || attrs_list.empty?
attrs_list.map do |attrs|
obj = self.new
attrs = attrs.dup if attrs.frozen?
attrs.delete(:assigned_cache_key)
obj.instance_variable_set("@attributes", attrs)
obj.instance_variable_set("@new_record", false)
obj
end
end
def asset_string def asset_string
@asset_string ||= "#{self.class.base_ar_class.name.underscore}_#{id.to_s}" @asset_string ||= "#{self.class.base_ar_class.name.underscore}_#{id.to_s}"
end end