| 1 | package DBIx::Fast::Cached; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 5 | ▶ | use v5.38; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 5 | ▶ | use Object::Pad 0.807; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 5 | our $VERSION = '0.22'; | |||||||||||||
| 7 | |||||||||||||||
| 8 | # Thin proxy returned by $db->cached(...) that wraps query methods with | ||||||||||||||
| 9 | # a cache lookup. Cache key is derived from (sql, bind params). | ||||||||||||||
| 10 | # | ||||||||||||||
| 11 | # Usage: | ||||||||||||||
| 12 | # my $row = $db->cached(ttl => 60, tag => 'products') | ||||||||||||||
| 13 | # ->hash('SELECT * FROM products WHERE id = ?', $id); | ||||||||||||||
| 14 | # my $rows = $db->cached(ttl => 30)->all('SELECT * FROM categories'); | ||||||||||||||
| 15 | |||||||||||||||
| 16 | class DBIx::Fast::Cached { | ||||||||||||||
| 17 | 0 | ▶ | field $dbix :param :reader; | ||||||||||||
|
Subroutine
dbix: not called CC=1 SCAR=2.6
Pod
undocumented
| |||||||||||||||
| 18 | field $ttl :param = undef; | ||||||||||||||
| 19 | 5 | field $tag :param = undef; # convenience: single tag → tags | |||||||||||||
| 20 | 5 | field $tags :param = undef; | |||||||||||||
| 21 | |||||||||||||||
| 22 | 5 | field $_ns; | |||||||||||||
| 23 | |||||||||||||||
| 24 | ADJUST { | ||||||||||||||
| 25 | # Normalize: tag => 'x' and tags => ['x'] are interchangeable. | ||||||||||||||
| 26 | if (defined $tag && !defined $tags) { | ||||||||||||||
| 27 | $tags = [$tag]; | ||||||||||||||
| 28 | } | ||||||||||||||
| 29 | |||||||||||||||
| 30 | # Connection identity is part of the key: two instances sharing one | ||||||||||||||
| 31 | # external backend (CHI/Redis) but pointing at different databases or | ||||||||||||||
| 32 | # connecting as different DB users must never serve each other's rows. | ||||||||||||||
| 33 | $_ns = $dbix->_cache_ns; | ||||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | 42 | ▶ | method _execute ($method, $sql, @params) { | ||||||||||||
|
Subroutine
_execute: called CC=5 SCAR=16.1
| |||||||||||||||
| 37 | 42 | ▶ | my $cache = $dbix->cache | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | or return $dbix->$method($sql, @params); # cache disabled, passthrough | ||||||||||||||
| 39 | |||||||||||||||
| 40 | # The method is part of the key: the same SQL+params returns different shapes | ||||||||||||||
| 41 | # via all()/hash()/val()/array(), so they must not share a cache entry. | ||||||||||||||
| 42 | 41 | my $key = $cache->build_key("$_ns$method\0$sql", \@params); | |||||||||||||
| 43 | 41 | my $hit = $cache->get($key); | |||||||||||||
| 44 | # Values are wrapped as [$val] so a cached undef (a negative/no-row result) | ||||||||||||||
| 45 | # is still a hit and is not re-queried on every call. | ||||||||||||||
| 46 | 41 | ▶ | return $hit->[0] if $hit; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | |||||||||||||||
| 48 | 27 | my $val = $dbix->$method($sql, @params); | |||||||||||||
| 49 | 27 | ▶ | $cache->set($key, [$val], | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 50 | defined $ttl ? (ttl => $ttl) : (), | ||||||||||||||
| 51 | $tags ? (tags => $tags) : ()); | ||||||||||||||
| 52 | 27 | return $val; | |||||||||||||
| 53 | 5 | } | |||||||||||||
| 54 | |||||||||||||||
| 55 | 14 | ▶ | method hash ($sql, @p) { $self->_execute('hash', $sql, @p) } | ||||||||||||
|
Subroutine
hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 56 | 14 | ▶ | method val ($sql, @p) { $self->_execute('val', $sql, @p) } | ||||||||||||
|
Subroutine
val: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 57 | 14 | ▶ | method all ($sql, @p) { $self->_execute('all', $sql, @p) } | ||||||||||||
|
Subroutine
all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 58 | 0 | ▶ | method array ($sql, @p) { $self->_execute('array', $sql, @p) } | ||||||||||||
|
Subroutine
array: not called CC=1 SCAR=4.9
Pod
documented
| |||||||||||||||
| 59 | |||||||||||||||
| 60 | # flat returns a LIST; cache it as arrayref and re-flatten on hit. | ||||||||||||||
| 61 | 2 | ▶ | method flat ($sql, @p) { | ||||||||||||
|
Subroutine
flat: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||
| 62 | 2 | ▶ | my $cache = $dbix->cache | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | or return $dbix->flat($sql, @p); | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 2 | my $key = $cache->build_key("${_ns}flat\0$sql", \@p); | |||||||||||||
| 66 | 2 | ▶ | if (my $hit = $cache->get($key)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | 1 | return @$hit; | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | |||||||||||||||
| 70 | # \@rows is a fresh per-call lexical, so storing the reference directly | ||||||||||||||
| 71 | # is safe and avoids copying the whole result set. | ||||||||||||||
| 72 | 1 | my @rows = $dbix->flat($sql, @p); | |||||||||||||
| 73 | 1 | ▶ | $cache->set($key, \@rows, | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 74 | defined $ttl ? (ttl => $ttl) : (), | ||||||||||||||
| 75 | $tags ? (tags => $tags) : ()); | ||||||||||||||
| 76 | 1 | return @rows; | |||||||||||||
| 77 | 5 | } | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | 5 | 1; | |||||||||||||
| 81 | |||||||||||||||
| 82 | __END__ | ||||||||||||||