| Path: | acts_as_static_record.rb |
| Last Update: | Thu Mar 26 17:49:56 -0600 2009 |
Permanently caches subclasses of ActiveRecord that contains data that changes rarely Calls to find on boths IDs and individual fields are cache hits rather than queries
class SomeMostlyStaticClass < ActiveRecord::Base
acts_as_static_record
end
Any finds that do not contain additional conditions, joins, and other arguments become a cache call. One advantage over the query cache is that the static cache is searched eliminating the need for ActiveRecord to generate SQL
When a cache key is specified with option :key, additional finder methods for ids and fields such as find_by_id and find_by_name_and_mother are overwritten to search the cache when no arguments (conditions) are specified. If the cache key is not a column, then a finder method will be defined.
acts_as_static_record :key => :some_instance_method
Will define find_by_some_instance_method(value)
Caches on Id and telephone carrier name
class TelephoneCarrier < ActiveRecord::Base acts_as_static_method :key => :name end
Caches the WhiteList on phone_number_digits (in addition to ID)
create_table :sms_white_list, :force => true do |t|
t.column :phone_number_id, :integer, :null => false
t.column :notes, :string, :length => 100, :default => nil
end
class SmsWhiteList < ActiveRecord::Base
belongs_to :phone_number
acts_as_static_record :key => :phone_number_digits,
:find => :select => 'carriers.*, phone_number.number as phone_number_digits'
:joins => 'inner join phone_numbers on phone_numbers.carrier_id = carriers.id'
def phone_number_digits
self['phone_number_digits']||self.phone_number.number
end
end
Direct cache hits
SmsWhiteList.find_by_phone_number_digits('12065551234')
SmsWhiteList.find_by_id(5)
SmsWhiteList.find :all
Searched cache hits
SmsWhiteList.find_by_notes('Some note')