Module: ActsAsStaticRecord::ClassMethods
- Defined in:
- lib/acts_as_static_record.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#acts_as_static_record(options = {}) ⇒ Object
acts_as_static_record Permanently caches subclasses of ActiveRecord that contains data that changes rarely.
Instance Method Details
#acts_as_static_record(options = {}) ⇒ Object
acts_as_static_record
Permanently caches subclasses of ActiveRecord that contains data that changes rarely.
Includes support for:
-
Find by Id, all, first (association lookups)
-
Cached caluculations:
Neighborhood.count
s, sum, max, min -
Convenience lookups:
Neighborhood[:seattle]
-
Additional support for column name lookups:
Neighborhood.find_by_name 'Seattle'
Install
script/plugin install git://github.com/blythedunham/static_record_cache.git
Usage
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)
Options
-
:key
- a method or column of the instance used to specify a cache key. This should be unique. -
:find
an additional find scope to specify:conditions
,:joins
,:select
, <tt>:joins</ff> etc -
:find_by_attribute_support
- set to true to add additional functionality for finders such asfind_by_id_and_name
to use a cache search. This option is probably best for Rails 2.3 -
:lookup_key
- access the record from the class by a key name likeUser[:snowgiraffe]
.:lookup_key
is the column on which do do the lookup.
Examples
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')
Calculation Support
Now with calculate
support for sum
, min
, and max
for integer columns and count
for all columns Cache hits do not occur if options other than distinct
are used.
Cache hits:
Neighborhood.count
Neighborhood.count :name, :distinct => true
Neighborhood.sum :id
Neighborhood.max :id
Not cache hits:
Neighborhood.max :name
Neighborhood.count :zip_code, :conditions => ['name = ?', 'Seattle']
Convenience lookup
Similar to acts_as_enumeration model returns the record where the acts_as_static_record :key
option matches lookup
If no key is specified, the primary_id column is used
class User < ActiveRecord::Base
acts_as_static_record :key => :user_name
end
Then later we can reference the objects by the user_name
User[:blythe]
User['snowgiraffe']
The key used will be the underscore version of the name. Punctuation marks are removed. The following are equivalent:
User[:blythe_snowgiraffeawesome]
User['blythe-SNOWGIRaffe AWESome']
user = User.first
User[user.user_name] == user
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/acts_as_static_record.rb', line 214 def acts_as_static_record(={}) .update() if if [:find_by_attribute_support] extend ActsAsStaticRecord::DefineFinderMethods end extend ActsAsStaticRecord::SingletonMethods include ActsAsStaticRecord::InstanceMethods unless respond_to?(:find_without_static_record) klass = class << self; self; end klass.class_eval "alias_method_chain :find, :static_record" klass.class_eval "alias_method_chain :calculate, :static_record" end define_static_cache_key_finder class_eval do before_save {|record| record.class.clear_static_record_cache } before_destroy {|record| record.class.clear_static_record_cache } end end |