NoBrainer caches data in three places:
- Criteria instances only cache data when enumerating items. Whenever a list of documents is
retrieved on a criteria (e.g.
map), the list is stored in the criteria’s cache, and all subsequent calls to
eachhit the cache instead of the database. However, the cache is killed when using
delete_allon the criteria instance.
- Associations are cached. Once the target of the association is retrieved, subsequent
calls will return the same documents without hitting the database again. Note
has_manyassociation behaves like a criteria, and thus the rule #1 applies.
- Eager loading associations will prepopulate the corresponding association caches.
These are the only places where NoBrainer will cache documents. NoBrainer does not use an identity map.
Let’s see an example:
Chaining a criteria always returns a new instance. For example
criteria.limit(10000) will return a new criteria instance with an empty cache.
eager_load() will carry the cache over when present,
and will eager load missing documents on top of the existing caches.
count repeatedly on the same criteria without having
enumerated items beforehand will always trigger a database call.
If you do not want to use the cache, you can chain
without_cache in the
criteria. This will ensure to never use the cache on that criteria. You may
with_cache to turn it back on.
When iterating a criteria, its cache can be automatically turned off when it
grows too big, which may happen when iterating very large tables.
When a cache reaches 10,000 elements (configurable with
config.criteria_cache_max_entries), the cache is flushed and disabled to
prevent out of memory issues.