Module: Sequel::Plugins::IdentityMap

Defined in:
lib/sequel/plugins/identity_map.rb

Overview

The identity_map plugin allows the user to create temporary identity maps via the with_identity_map method, which takes a block. Inside the block, objects have a 1-1 correspondence with rows in the database.

For example, the following is true, and wouldn’t be true if you weren’t using the identity map:

Sequel::Model.with_identity_map do
  Album.filter{(id > 0) & (id < 2)}.first.object_id == Album.first(:id=>1).object_id
end

In additional to providing a 1-1 correspondence, the identity_map plugin also provides a cached looked up of records in two cases:

  • Model.[] (e.g. Album)

  • Model.many_to_one accessor methods (e.g. album.artist)

If the object you are looking up using one of those two methods is already in the identity map, the record is returned without a database query being issued.

Identity maps are thread-local and only presist for the duration of the block, so they should be should only be considered as a possible performance enhancer.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods