| 1 | package Data::Model::Driver::Cache; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Data::Model::Driver'; | ||
| 5 | |||
| 6 | use Carp (); | ||
| 7 | $Carp::Internal{(__PACKAGE__)}++; | ||
| 8 | |||
| 9 | use Storable (); | ||
| 10 | |||
| 11 | sub fallback { shift->{fallback} } | ||
| 12 | sub cache { shift->{cache} } | ||
| 13 | |||
| 14 | sub _as_sql_hook { | ||
| 15 | my $self = shift; | ||
| 16 | $self->{fallback}->_as_sql_hook(@_); | ||
| 17 | } | ||
| 18 | |||
| 19 | sub add_to_cache { Carp::croak("NOT IMPLEMENTED") } | ||
| 20 | sub update_cache { Carp::croak("NOT IMPLEMENTED") } | ||
| 21 | sub remove_from_cache { Carp::croak("NOT IMPLEMENTED") } | ||
| 22 | sub get_from_cache { Carp::croak("NOT IMPLEMENTED") } | ||
| 23 | |||
| 24 | sub get_multi_from_cache { | ||
| 25 | my($self, $keys) = @_; | ||
| 26 | |||
| 27 | my %got; | ||
| 28 | while (my($key, $id) = each %{ $keys }) { | ||
| 29 | my $obj = $self->get_from_cache($id->[1]) or next; | ||
| 30 | $got{$key} = $obj; | ||
| 31 | } | ||
| 32 | \%got; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub remove_multi_from_cache { | ||
| 36 | my($self, $keys) = @_; | ||
| 37 | $self->remove_from_cache($_) for @{ $keys }; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub init { | ||
| 41 | my $self = shift; | ||
| 42 | my %param = @_; | ||
| 43 | $self->SUPER::init(@_); | ||
| 44 | # $self->cache($param{cache}) | ||
| 45 | # or Carp::croak("cache is required"); | ||
| 46 | $self->fallback($param{fallback}) | ||
| 47 | or Carp::croak("fallback is required"); | ||
| 48 | $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | # lookupは真面目にキャッシュする | ||
| 52 | sub lookup { | ||
| 53 | my $self = shift; | ||
| 54 | return $self->{fallback}->lookup(@_) if $self->{active_transaction}; | ||
| 55 | my($schema, $id) = @_; | ||
| 56 | |||
| 57 | my $cache_key = $self->cache_key($schema, $id); | ||
| 58 | my $ret = $self->get_from_cache($cache_key); | ||
| 59 | unless ($ret) { | ||
| 60 | $ret = $self->{fallback}->lookup(@_); | ||
| 61 | return unless defined $ret; | ||
| 62 | $self->add_to_cache($cache_key, $ret); | ||
| 63 | } | ||
| 64 | return $ret; | ||
| 65 | } | ||
| 66 | |||
| 67 | # 先に get_multi でキャッシュデータを全部取ってきて、キャッシュから取って来れなければfallbackして取得 | ||
| 68 | sub lookup_multi { | ||
| 69 | my $self = shift; | ||
| 70 | return $self->{fallback}->lookup_multi(@_) if $self->{active_transaction}; | ||
| 71 | my($schema, $ids) = @_; | ||
| 72 | |||
| 73 | my %cache_keys = map { join("\0", @{ $_ }) => [ $_, $self->cache_key($schema, $_) ] } @{ $ids }; | ||
| 74 | my $results = $self->get_multi_from_cache(\%cache_keys); | ||
| 75 | if (scalar(keys %cache_keys) == scalar(keys %{ $results })) { | ||
| 76 | return $results; | ||
| 77 | } | ||
| 78 | |||
| 79 | # make lookup id list | ||
| 80 | my @lookup_keys; | ||
| 81 | while (my($key, $id) = each %cache_keys) { | ||
| 82 | next if $results->{$key}; | ||
| 83 | push @lookup_keys, $id->[0]; | ||
| 84 | } | ||
| 85 | |||
| 86 | my $fallback_results = $self->{fallback}->lookup_multi($schema, \@lookup_keys); | ||
| 87 | return unless scalar(%{ $results }) || scalar(%{ $fallback_results }); | ||
| 88 | return $results unless scalar(%{ $fallback_results }); | ||
| 89 | |||
| 90 | while (my($key, $val) = each %{ $fallback_results }) { | ||
| 91 | $self->add_to_cache($cache_keys{$key}->[1], $val) if defined $val; | ||
| 92 | $results->{$key} = $val; | ||
| 93 | } | ||
| 94 | |||
| 95 | $results; | ||
| 96 | } | ||
| 97 | |||
| 98 | # key 指定の検索でないならキャッシュ処理しない (未実装) | ||
| 99 | sub get { | ||
| 100 | my $self = shift; | ||
| 101 | return $self->{fallback}->get(@_) if $self->{active_transaction}; | ||
| 102 | return $self->{fallback}->get(@_); | ||
| 103 | my($schema, $key, $columns, %args) = @_; | ||
| 104 | |||
| 105 | return $self->{fallback}->get(@_) unless $key && !$columns; | ||
| 106 | |||
| 107 | my $cache_key = $self->cache_key($schema, $key); | ||
| 108 | my $ret = $self->get_from_cache($cache_key); | ||
| 109 | return $self->{fallback}->get(@_) unless $ret; | ||
| 110 | return $ret; | ||
| 111 | } | ||
| 112 | |||
| 113 | # insertはキャッシュ処理を通さない | ||
| 114 | sub set { shift->{fallback}->set(@_) } | ||
| 115 | |||
| 116 | # key で cache を delete するのみ | ||
| 117 | sub replace { | ||
| 118 | my $self = shift; | ||
| 119 | my($schema, $key, $columns, %args) = @_; | ||
| 120 | |||
| 121 | if (scalar(@{ $key }) == scalar(@{ $schema->key })) { | ||
| 122 | my $cache_key = $self->cache_key($schema, $key); | ||
| 123 | $self->remove_cache($cache_key); | ||
| 124 | } | ||
| 125 | $self->{fallback}->replace(@_); | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | # delete / update は key を指定した処理を主なターゲットとして | ||
| 130 | # udate_all / delete_all 的なのとかのkeyが判らない物は、いったんその条件でgetしてから、個別のobjectを処理する | ||
| 131 | # なので、直接keyを指定しないと、ここの処理のパフォーマンスはキャッシュ無しのがさらに早くなる | ||
| 132 | sub update { | ||
| 133 | my $self = shift; | ||
| 134 | my($schema, $old_key, $key, $old_columns, $columns, $changed_columns, %args) = @_; | ||
| 135 | |||
| 136 | if (scalar(@{ $old_key }) == scalar(@{ $schema->key })) { | ||
| 137 | my $cache_key = $self->cache_key($schema, $old_key); | ||
| 138 | $self->remove_cache($cache_key); | ||
| 139 | } | ||
| 140 | |||
| 141 | $self->{fallback}->update(@_); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub _delete_cache { | ||
| 145 | my($self, $schema, $key, $columns, %args) = @_; | ||
| 146 | |||
| 147 | my($it, $it_opt) = $self->{fallback}->get($schema, $key, $columns ? Storable::dclone($columns) : $columns, %args); | ||
| 148 | if ($it) { | ||
| 149 | while (my $row = $it->()) { | ||
| 150 | my $key = $schema->get_key_array_by_hash($row); | ||
| 151 | my $cache_key = $self->cache_key($schema, $key); | ||
| 152 | $self->remove_cache($cache_key); | ||
| 153 | } | ||
| 154 | $it_opt->{end}->() if exists $it_opt->{end} && ref($it_opt->{end}) eq 'CODE'; | ||
| 155 | } | ||
| 156 | return 1; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub update_direct { | ||
| 160 | my $self = shift; | ||
| 161 | my($schema, $key, $query, $columns, %args) = @_; | ||
| 162 | |||
| 163 | if ($key && !$columns && scalar(@{ $key }) == scalar(@{ $schema->key })) { | ||
| 164 | my $cache_key = $self->cache_key($schema, $key); | ||
| 165 | $self->remove_cache($cache_key); | ||
| 166 | } else { | ||
| 167 | return unless $self->_delete_cache($schema, $key, $query, %args); | ||
| 168 | } | ||
| 169 | $self->{fallback}->update_direct(@_); | ||
| 170 | } | ||
| 171 | |||
| 172 | sub delete { | ||
| 173 | my $self = shift; | ||
| 174 | my($schema, $key, $columns, %args) = @_; | ||
| 175 | |||
| 176 | if ($key && !$columns && scalar(@{ $key }) == scalar(@{ $schema->key })) { | ||
| 177 | my $cache_key = $self->cache_key($schema, $key); | ||
| 178 | $self->remove_cache($cache_key); | ||
| 179 | } else { | ||
| 180 | return unless $self->_delete_cache(@_); | ||
| 181 | } | ||
| 182 | $self->{fallback}->delete($schema, $key, $columns, %args); | ||
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | sub remove_cache { | ||
| 187 | my($self, $cache_key) = @_; | ||
| 188 | if ($self->{active_transaction}) { | ||
| 189 | push @{ $self->{transaction_delete_queue} }, $cache_key; | ||
| 190 | } else { | ||
| 191 | $self->remove_from_cache($cache_key); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | # for transactions | ||
| 196 | sub txn_begin { | ||
| 197 | my $self = shift; | ||
| 198 | $self->{active_transaction} = 1; | ||
| 199 | $self->{transaction_delete_queue} = []; | ||
| 200 | $self->{fallback}->txn_begin; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub txn_rollback { | ||
| 204 | my $self = shift; | ||
| 205 | return unless $self->{active_transaction}; | ||
| 206 | $self->{fallback}->txn_rollback; | ||
| 207 | |||
| 208 | $self->{transaction_delete_queue} = []; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub txn_commit { | ||
| 212 | my $self = shift; | ||
| 213 | return unless $self->{active_transaction}; | ||
| 214 | $self->{fallback}->txn_commit; | ||
| 215 | |||
| 216 | # apply delete queue | ||
| 217 | $self->remove_multi_from_cache($self->{transaction_delete_queue}); | ||
| 218 | |||
| 219 | $self->{transaction_delete_queue} = []; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub txn_end { | ||
| 223 | my $self = shift; | ||
| 224 | $self->{fallback}->txn_end; | ||
| 225 | $self->{active_transaction} = 0; | ||
| 226 | $self->{transaction_delete_queue} = []; | ||
| 227 | } | ||
| 228 | |||
| 229 | 1; |