class

Lustra::Model::QueryCache

Inherits Reference < Object

The Lustra::Model::QueryCache is a fire-and-forget cache used when caching associations and preventing N+1 queries anti-pattern.

This is not a global cache: One cache instance exists per collection, and the cache disappear at the same time the Collection is unreferenced.

Each cache can references multiples relations at the same time. This cache use an underlying hash to access to the references keys.

Instance methods

active(relation_name)

Tell this cache than we active the cache over a specific relation name. Returns self

Source
active?(relation_name)

Check whether the cache is active on a certain association. Returns true if relation_name is flagged as encached, or false otherwise.

Source
clear

Empty the cache and flag all relations has unactive

Source
fetch
Source
hit(relation_name, relation_value, klass : T.class) : Array(T) forall T

Try to hit the cache. If an array is found, it will be returned. Otherwise, empty array is returned.

This methods do not check if a relation flagged as is actively cached or not. Therefore, hitting a non-cached relation will return always an empty-array.

Source
set(relation_name, relation_value, arr : Array(T)) forall T

Set the cached array for a specific key {relation_name,relation_value}

Source
with_cache

Perform some operations with the cache then eventually clear the cache.

Source