active record context, , , , , , , " />

Introducing … Static Record Cache plugin

March 31, 2009

Static Record Cache permanently caches ActiveRecord data for classes which contain small amounts of static data (data that rarely changes). In addition to caching queries on Ids, methods like find_by_name will use cache hits rather than new queries to retrieve data. Static Record Cache is designed to work with or without the built inĀ  query cache, but by contrast permanently stores the actual record across all contexts and avoids the overhead of instantiating new ActiveRecord objects.

Install

script/plugin install git://github.com/blythedunham/static_record_cache.git

Documentation

class SomeStaticClass < ActiveRecord::Base
  acts_as_static_record
end

#cache hits
SomeStaticClass.find_by_name('blah')
SomeStaticClass.find_by_id(5)
SomeStaticClass.find :all
assoc_class.some_static_class

One can specify the cache key for faster lookups and also customize the finder SQL for the class. More documentation can be found in the rdocs.

Another, simpler method of caching can be used by folks using the active record context plugin by technoweenie. Extending StaticRecordCache will permanently cache data for the class regardless of context. This however, only caches by id.

class TelephoneCarriers < ActiveRecord::Base
  extend StaticActiveRecordContext
end

Be sure to install active_record_cache plugin:

script/plugin install http://svn.techno-weenie.net/projects/plugins/active_record_context/

Leave a Reply