Module: Invoicing::CachedRecord

Extended by:
Defined in:


Aggressive ActiveRecord cache

This module implements a cache of ActiveRecord objects. It is suitable for database tables with a small number of rows (no more than a few dozen is recommended) which change very infrequently. The contents of the table is loaded into memory when the class is first created; to clear the cache you must call clear_cache or restart the Ruby interpreter. It is recommended that if you need to change the data in this table, you do so in a database migration, and apply that migration as part of a release deployment.

The cache works as a simple identity map: it has a hash where the key is the primary key of each model object and the value is the model object itself. ActiveRecord methods are overridden so that if find is called with one or more IDs, the object(s) are returned from cache; if find is called with more complex conditions, the usual database mechanisms are used and the cache is ignored. Note that this does not guarantee that the same ID value will always map to the same model object instance; it just reduces the number of database queries.

To activate CachedRecord, call acts_as_cached_record in the scope of an ActiveRecord::Base class.

Defined Under Namespace

Modules: ActMethods, ClassMethods Classes: ClassInfo