| 1 | package DBIx::QuickORM::Manual::Caching; | ||
| 2 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 2 | our $VERSION = '0.000031'; | |
| 6 | |||
| 7 | =pod | ||
| 8 | |||
| 9 | =encoding UTF-8 | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | DBIx::QuickORM::Manual::Caching - The ORM's one canonical copy of each row, by | ||
| 14 | example. | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | The ORM caches one copy of the data of each row it has read or written, per | ||
| 19 | table and primary key: the I<canonical> copy (the code calls it I<canon>). A | ||
| 20 | row is canonical when its data is that copy. That does not mean it matches | ||
| 21 | the database right now. Two reads of one row can share it, and a write | ||
| 22 | through a row object needs it. C<< $row->is_canon >> asks whether a row | ||
| 23 | object is canonical. | ||
| 24 | |||
| 25 | =head1 SCHEMA | ||
| 26 | |||
| 27 | The examples use PostgreSQL and this schema: | ||
| 28 | |||
| 29 | =for highlighter language=sql | ||
| 30 | |||
| 31 | CREATE TABLE users ( | ||
| 32 | id SERIAL PRIMARY KEY, | ||
| 33 | name TEXT NOT NULL, | ||
| 34 | email TEXT UNIQUE, | ||
| 35 | prefs JSON, | ||
| 36 | referred_by INTEGER REFERENCES users (id), | ||
| 37 | created TIMESTAMP NOT NULL DEFAULT now() | ||
| 38 | ); | ||
| 39 | |||
| 40 | CREATE TABLE posts ( | ||
| 41 | id SERIAL PRIMARY KEY, | ||
| 42 | user_id INTEGER NOT NULL REFERENCES users (id), | ||
| 43 | title TEXT NOT NULL, | ||
| 44 | views INTEGER NOT NULL DEFAULT 0 | ||
| 45 | ); | ||
| 46 | |||
| 47 | =head1 ONE COPY PER ROW | ||
| 48 | |||
| 49 | =for highlighter language=perl | ||
| 50 | |||
| 51 | my $one = $orm->find(users => $id); | ||
| 52 | my $two = $orm->handle('users')->where({id => $id})->only; | ||
| 53 | |||
| 54 | # true: the same hash, so two objects share one copy of the data | ||
| 55 | my $shared = $one->row_data == $two->row_data; | ||
| 56 | |||
| 57 | $one->update({name => 'Ann B'}); | ||
| 58 | my $stale = $two->is_canon; # false: $one's write replaced the canonical copy | ||
| 59 | my $name = $two->field('name'); # 'Ann': $two still holds the data it read | ||
| 60 | |||
| 61 | # croaks: $two is not canonical | ||
| 62 | my $ok = eval { $two->update({email => 'b@example.com'}); 1 }; | ||
| 63 | |||
| 64 | $two->refresh; # reads the database: $two is canonical again | ||
| 65 | $two->update({email => 'b@example.com'}); | ||
| 66 | |||
| 67 | Refusing the write is the point: C<$one> changed the row, so C<$two> is stale | ||
| 68 | even for a write to another field, and a write decided on stale data is the | ||
| 69 | bug this catches. C<cas_update> writes a stale row safely by comparing with | ||
| 70 | the database (L<DBIx::QuickORM::Manual::Transactions>). | ||
| 71 | |||
| 72 | =head1 ENFORCE_CANON | ||
| 73 | |||
| 74 | # write by primary key alone, whatever the ORM holds | ||
| 75 | $two->update({email => 'c@example.com'}, enforce_canon => 0); | ||
| 76 | |||
| 77 | my $lax = DBIx::QuickORM->new( | ||
| 78 | credentials => { | ||
| 79 | dsn => $dsn, | ||
| 80 | user => $user, | ||
| 81 | pass => $pass, | ||
| 82 | }, | ||
| 83 | enforce_canon => 0, # for every write outside a transaction | ||
| 84 | ); | ||
| 85 | |||
| 86 | Inside a transaction every C<update> and C<delete> checks, whatever the | ||
| 87 | ORM-wide C<enforce_canon> says, unless the call itself passes | ||
| 88 | C<< enforce_canon => 0 >>. | ||
| 89 | |||
| 90 | =head1 READS FROM THE CANONICAL COPY | ||
| 91 | |||
| 92 | my $ann = $orm->find(users => $id); | ||
| 93 | my $again = $orm->find(users => $id); # no query: the canonical copy has every field asked for | ||
| 94 | |||
| 95 | my $fresh = $orm->find( | ||
| 96 | users => $id, | ||
| 97 | cache => 0, # reads the database (same data, so the canonical copy stays) | ||
| 98 | ); | ||
| 99 | |||
| 100 | When another process has changed the row: | ||
| 101 | |||
| 102 | my $changed = $orm->find( | ||
| 103 | users => $id, | ||
| 104 | cache => 0, # new data: this becomes the canonical copy | ||
| 105 | ); | ||
| 106 | |||
| 107 | my $old = $ann->is_canon; # false | ||
| 108 | $ann->refresh; # true again | ||
| 109 | |||
| 110 | C<find> and C<refresh_row> return the canonical copy when it has every field | ||
| 111 | asked for. C<where> only builds a handle: its first read runs the query, and | ||
| 112 | later reads and every C<iter> reuse its rows until C<clear>. A query's rows | ||
| 113 | keep the canonical copy when the data matches and replace it when it does not. | ||
| 114 | |||
| 115 | C<< $row->refresh >> always reads the database. | ||
| 116 | |||
| 117 | A C<no_cache> handle's queries skip the row cache: their rows are not | ||
| 118 | canonical, and the row cache does not change. C<cache> undoes it on a handle. | ||
| 119 | |||
| 120 | my $apart = $orm->handle('users')->no_cache; | ||
| 121 | my @rows = $apart->all; # not canonical, and the row cache does not change | ||
| 122 | |||
| 123 | # find is a CRUD operation and reads the canonical copy first, but cache => 0 forces a read | ||
| 124 | my $read = $apart->find($id, cache => 0); | ||
| 125 | |||
| 126 | =head1 HOW LONG ROWS ARE HELD | ||
| 127 | |||
| 128 | By default a copy is held only while your code holds a row object (or its | ||
| 129 | data) for it. C<< retain_cache => 1 >> holds every copy until C<clear_cache>. | ||
| 130 | Inside a transaction every copy is held until the transaction ends. | ||
| 131 | |||
| 132 | C<refresh_row> takes the key in any form (L<DBIx::QuickORM::Manual::Keys>). | ||
| 133 | With C<< db => 0 >> it asks the row cache only, without querying | ||
| 134 | the database, so it shows whether a copy is held: | ||
| 135 | |||
| 136 | # the row object is gone at once | ||
| 137 | my $tmp_id = $orm->insert(users => {name => 'Tmp'})->field('id'); | ||
| 138 | my $held = $orm->refresh_row( | ||
| 139 | users => {id => $tmp_id}, | ||
| 140 | db => 0, # undef: nothing holds the row any more | ||
| 141 | ); | ||
| 142 | |||
| 143 | my $keeper = DBIx::QuickORM->new( | ||
| 144 | credentials => { | ||
| 145 | dsn => $dsn, | ||
| 146 | user => $user, | ||
| 147 | pass => $pass, | ||
| 148 | }, | ||
| 149 | retain_cache => 1, | ||
| 150 | ); | ||
| 151 | |||
| 152 | my $kept_id = $keeper->insert(users => {name => 'Kept'})->field('id'); | ||
| 153 | my $kept = $keeper->refresh_row( | ||
| 154 | users => {id => $kept_id}, | ||
| 155 | db => 0, # the row: held until clear_cache | ||
| 156 | ); | ||
| 157 | |||
| 158 | =head1 CLEAR_CACHE | ||
| 159 | |||
| 160 | # one row: a hashref, arrayref, row, or single value, as find takes it | ||
| 161 | $orm->clear_cache(users => $id); | ||
| 162 | $orm->clear_cache('users'); # one table | ||
| 163 | $orm->clear_cache; # everything | ||
| 164 | my $current = $ann->is_canon; # false: refresh makes it canonical again | ||
| 165 | |||
| 166 | my $post = $orm->find(posts => $post_id); | ||
| 167 | |||
| 168 | # a bulk update or delete drops the canonical copies of its table's rows | ||
| 169 | $orm->handle('posts')->where({views => 0})->update({views => 1}); | ||
| 170 | my $dropped = $post->is_canon; # false | ||
| 171 | |||
| 172 | # mark_deleted: the held rows it deleted also say so | ||
| 173 | my $temp = $orm->insert(posts => { | ||
| 174 | user_id => $ann->field('id'), | ||
| 175 | title => 'Temp', | ||
| 176 | }); | ||
| 177 | $orm->handle('posts')->where({title => 'Temp'})->delete(mark_deleted => 1); | ||
| 178 | my $deleted = $temp->was_deleted; # true | ||
| 179 | |||
| 180 | =head1 TRANSACTIONS | ||
| 181 | |||
| 182 | A row read before a transaction is not canonical inside it until it is read | ||
| 183 | again. A commit keeps what the transaction held, and a rollback forgets it. | ||
| 184 | L<DBIx::QuickORM::Manual::Transactions/"THE ROWS YOU HOLD"> shows each case. | ||
| 185 | |||
| 186 | =head1 TYPED KEYS | ||
| 187 | |||
| 188 | A typed primary key finds the canonical copy in any form: | ||
| 189 | L<DBIx::QuickORM::Manual::Keys/"TYPED KEYS">. | ||
| 190 | |||
| 191 | =head1 WHEN TO CARE | ||
| 192 | |||
| 193 | =over 4 | ||
| 194 | |||
| 195 | =item * | ||
| 196 | |||
| 197 | Read a row, use it, drop it: never. | ||
| 198 | |||
| 199 | =item * | ||
| 200 | |||
| 201 | Keep a row object while other code writes the row, or across a transaction: | ||
| 202 | C<refresh> before writing it, or write it with C<cas_update>. | ||
| 203 | |||
| 204 | =item * | ||
| 205 | |||
| 206 | Several processes: each has its own copies. For a write that depends on what | ||
| 207 | was read, use C<cas_update> with C<< cas => [...] >> naming the fields it | ||
| 208 | depends on. A transaction alone does not stop another writer at the default | ||
| 209 | isolation level (L<DBIx::QuickORM::Manual::Transactions/ISOLATION>). | ||
| 210 | |||
| 211 | =back | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | 2 | 1; | |
| 216 | |||
| 217 | __END__ | ||