NAME
DBIx::Fast::Cached - Query proxy that caches results
SYNOPSIS
my $row = $db->cached(ttl => 60, tag => 'products')
->hash('SELECT * FROM products WHERE id = ?', $id);
my $rows = $db->cached(ttl => 30, tags => ['categories', 'navigation'])
->all('SELECT * FROM categories WHERE active = 1');
$db->cache->invalidate('products'); # drop cached product queries
DESCRIPTION
Returned by $db->cached(...). Wraps the standard query methods (hash, val, all, array, flat) with a cache lookup keyed on (method, sql, bind params). Cache miss executes the underlying query and stores the result; cache hit returns the stored value.
If the parent DBIx::Fast instance was not constructed with cache, calls pass through to the original methods (no overhead, no caching).
Caveat: a cache hit returns the same reference that is stored in the cache (results are not deep-copied, to keep the cache fast). Do not mutate a returned hashref/arrayref in place - it would corrupt the cached entry for the next reader. Copy it first if you need to modify it.
METHODS
hash / val / all / array
my $row = $db->cached(ttl => 60)->hash($sql, @binds);
my $v = $db->cached->val($sql, @binds);
my $rows = $db->cached->all($sql, @binds);
my $col = $db->cached->array($sql, @binds);
Same signature and return value as the DBIx::Fast method of the same name, with a cache lookup in front. A cached undef (no-row result) is still a hit and is not re-queried.
flat
my @names = $db->cached->flat($sql, @binds);
List-returning variant: the result is cached as an arrayref and re-flattened on every hit.
OPTIONS
ttl-
Time-to-live in seconds. Falls back to cache's
default_ttlwhen omitted. tag-
Single tag attached to the cached entry. Convenience alias for
tags => ['x']. -
Arrayref of tags. Use
$db->cache->invalidate($tag)to drop every entry tagged with$tag.
AUTHOR
SeHarrys
LICENSE
This is free software under the Artistic License 2.0.