class Clear::Model::QueryCache

Overview

The Clear::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.

Defined in:

clear/model/query_cache.cr

Instance Method Summary

Instance Method Detail

def active(relation_name) #

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


[View source]
def 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.


[View source]
def clear #

Empty the cache and flag all relations has unactive


[View source]
def fetch #

[View source]
def 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.


[View source]
def set(relation_name, relation_value, arr : Array(T)) forall T #

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


[View source]
def with_cache(&) #

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


[View source]