| 1 | # $Id$ | ||
| 2 | |||
| 3 | package Data::ObjectDriver::Driver::BaseCache; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use base qw( Data::ObjectDriver Class::Accessor::Fast | ||
| 8 | Class::Data::Inheritable ); | ||
| 9 | |||
| 10 | use Carp (); | ||
| 11 | |||
| 12 | __PACKAGE__->mk_accessors(qw( cache fallback txn_buffer)); | ||
| 13 | __PACKAGE__->mk_classdata(qw( Disabled )); | ||
| 14 | |||
| 15 | sub deflate { $_[1] } | ||
| 16 | sub inflate { $_[2] } | ||
| 17 | |||
| 18 | # subclasses must override these: | ||
| 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 init { | ||
| 25 | my $driver = shift; | ||
| 26 | $driver->SUPER::init(@_); | ||
| 27 | my %param = @_; | ||
| 28 | $driver->cache($param{cache}) | ||
| 29 | or Carp::croak("cache is required"); | ||
| 30 | $driver->fallback($param{fallback}) | ||
| 31 | or Carp::croak("fallback is required"); | ||
| 32 | $driver->txn_buffer([]); | ||
| 33 | $driver; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub begin_work { | ||
| 37 | my $driver = shift; | ||
| 38 | my $rv = $driver->fallback->begin_work(@_); | ||
| 39 | $driver->SUPER::begin_work(@_); | ||
| 40 | return $rv; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub commit { | ||
| 44 | my $driver = shift; | ||
| 45 | return unless $driver->txn_active; | ||
| 46 | |||
| 47 | my $rv = $driver->fallback->commit(@_); | ||
| 48 | |||
| 49 | $driver->debug(sprintf("%14s", "COMMIT(" . scalar(@{$driver->txn_buffer}) . ")") . ": driver=$driver"); | ||
| 50 | while (my $cb = shift @{$driver->txn_buffer}) { | ||
| 51 | $cb->(); | ||
| 52 | } | ||
| 53 | $driver->SUPER::commit(@_); | ||
| 54 | |||
| 55 | return $rv; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub rollback { | ||
| 59 | my $driver = shift; | ||
| 60 | return unless $driver->txn_active; | ||
| 61 | my $rv = $driver->fallback->rollback(@_); | ||
| 62 | |||
| 63 | $driver->debug(sprintf("%14s", "ROLLBACK(" . scalar(@{$driver->txn_buffer}) . ")") . ": driver=$driver"); | ||
| 64 | $driver->txn_buffer([]); | ||
| 65 | |||
| 66 | $driver->SUPER::rollback(@_); | ||
| 67 | |||
| 68 | return $rv; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub cache_object { | ||
| 72 | my $driver = shift; | ||
| 73 | my($obj) = @_; | ||
| 74 | return $driver->fallback->cache_object($obj) | ||
| 75 | if $driver->Disabled; | ||
| 76 | ## If it's already cached in this layer, assume it's already cached in | ||
| 77 | ## all layers below this, as well. | ||
| 78 | unless (exists $obj->{__cached} && $obj->{__cached}{ref $driver}) { | ||
| 79 | $driver->modify_cache(sub { | ||
| 80 | $driver->add_to_cache( | ||
| 81 | $driver->cache_key(ref($obj), $obj->primary_key), | ||
| 82 | $driver->deflate($obj) | ||
| 83 | ); | ||
| 84 | }); | ||
| 85 | $driver->fallback->cache_object($obj); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | sub lookup { | ||
| 90 | my $driver = shift; | ||
| 91 | my($class, $id) = @_; | ||
| 92 | return unless defined $id; | ||
| 93 | return $driver->fallback->lookup($class, $id) | ||
| 94 | if $driver->Disabled or $driver->txn_active; | ||
| 95 | my $key = $driver->cache_key($class, $id); | ||
| 96 | my $obj = $driver->get_from_cache($key); | ||
| 97 | if ($obj) { | ||
| 98 | $obj = $driver->inflate($class, $obj); | ||
| 99 | $obj->{__cached}{ref $driver} = 1; | ||
| 100 | } else { | ||
| 101 | $obj = $driver->fallback->lookup($class, $id); | ||
| 102 | } | ||
| 103 | $obj; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub get_multi_from_cache { | ||
| 107 | my $driver = shift; | ||
| 108 | my(@keys) = @_; | ||
| 109 | ## Use driver->get_from_cache to look up each object in the cache. | ||
| 110 | ## We don't fall back here, because we only want to find items that | ||
| 111 | ## are already cached. | ||
| 112 | my %got; | ||
| 113 | for my $key (@keys) { | ||
| 114 | my $obj = $driver->get_from_cache($key) or next; | ||
| 115 | $got{$key} = $obj; | ||
| 116 | } | ||
| 117 | \%got; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub lookup_multi { | ||
| 121 | my $driver = shift; | ||
| 122 | my($class, $ids) = @_; | ||
| 123 | return $driver->fallback->lookup_multi($class, $ids) | ||
| 124 | if $driver->Disabled or $driver->txn_active; | ||
| 125 | |||
| 126 | my %id2key = map { $_ => $driver->cache_key($class, $_) } grep { defined } @$ids; | ||
| 127 | my $got = $driver->get_multi_from_cache(values %id2key); | ||
| 128 | |||
| 129 | ## Otherwise, look through the list of IDs to see what we're missing, | ||
| 130 | ## and fall back to the backend to look up those objects. | ||
| 131 | my($i, @got, @need, %need2got) = (0); | ||
| 132 | for my $id (@$ids) { | ||
| 133 | if (defined $id && (my $obj = $got->{ $id2key{$id} })) { | ||
| 134 | $obj = $driver->inflate($class, $obj); | ||
| 135 | $obj->{__cached}{ref $driver} = 1; | ||
| 136 | push @got, $obj; | ||
| 137 | } else { | ||
| 138 | push @got, undef; | ||
| 139 | push @need, $id; | ||
| 140 | $need2got{$#need} = $i; | ||
| 141 | } | ||
| 142 | $i++; | ||
| 143 | } | ||
| 144 | |||
| 145 | if (@need) { | ||
| 146 | my $more = $driver->fallback->lookup_multi($class, \@need); | ||
| 147 | $i = 0; | ||
| 148 | for my $obj (@$more) { | ||
| 149 | $got[ $need2got{$i++} ] = $obj; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | \@got; | ||
| 154 | } | ||
| 155 | |||
| 156 | ## We fallback by default | ||
| 157 | sub fetch_data { | ||
| 158 | my $driver = shift; | ||
| 159 | my ($obj) = @_; | ||
| 160 | return $driver->fallback->fetch_data($obj); | ||
| 161 | } | ||
| 162 | |||
| 163 | sub search { | ||
| 164 | my $driver = shift; | ||
| 165 | return $driver->fallback->search(@_) | ||
| 166 | if $driver->Disabled; | ||
| 167 | my($class, $terms, $args) = @_; | ||
| 168 | |||
| 169 | ## If the caller has asked only for certain columns, assume that | ||
| 170 | ## he knows what he's doing, and fall back to the backend. | ||
| 171 | return $driver->fallback->search(@_) | ||
| 172 | if $args->{fetchonly}; | ||
| 173 | |||
| 174 | ## Tell the fallback driver to fetch only the primary columns, | ||
| 175 | ## then run the search using the fallback. | ||
| 176 | local $args->{fetchonly} = $class->primary_key_tuple; | ||
| 177 | ## Disable triggers for this load. We don't want the post_load trigger | ||
| 178 | ## being called twice. | ||
| 179 | local $args->{no_triggers} = 1; | ||
| 180 | my @objs = $driver->fallback->search($class, $terms, $args); | ||
| 181 | |||
| 182 | my $windowed = (!wantarray) && $args->{window_size}; | ||
| 183 | |||
| 184 | if ( $windowed ) { | ||
| 185 | my @window; | ||
| 186 | my $window_size = $args->{window_size}; | ||
| 187 | my $iter = sub { | ||
| 188 | my $d = $driver; | ||
| 189 | while ( (!@window) && @objs ) { | ||
| 190 | my $objs = $driver->lookup_multi( | ||
| 191 | $class, | ||
| 192 | [ map { $_->primary_key } | ||
| 193 | splice( @objs, 0, $window_size ) ] | ||
| 194 | ); | ||
| 195 | # A small possibility exists that we may fetch | ||
| 196 | # some IDs here that no longer exist; grep these out | ||
| 197 | @window = grep { defined $_ } @$objs if $objs; | ||
| 198 | } | ||
| 199 | return @window ? shift @window : undef; | ||
| 200 | }; | ||
| 201 | return Data::ObjectDriver::Iterator->new($iter, sub { @objs = (); @window = () }); | ||
| 202 | } else { | ||
| 203 | ## Load all of the objects using a lookup_multi, which is fast from | ||
| 204 | ## cache. | ||
| 205 | my $objs = $driver->lookup_multi($class, [ map { $_->primary_key } @objs ]); | ||
| 206 | return $driver->list_or_iterator($objs); | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | sub update { | ||
| 211 | my $driver = shift; | ||
| 212 | my($obj) = @_; | ||
| 213 | return $driver->fallback->update($obj) | ||
| 214 | if $driver->Disabled; | ||
| 215 | my $ret = $driver->fallback->update(@_); | ||
| 216 | my $key = $driver->cache_key(ref($obj), $obj->primary_key); | ||
| 217 | $driver->modify_cache(sub { | ||
| 218 | $driver->uncache_object($obj); | ||
| 219 | }); | ||
| 220 | return $ret; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub replace { | ||
| 224 | my $driver = shift; | ||
| 225 | my($obj) = @_; | ||
| 226 | return $driver->fallback->replace($obj) | ||
| 227 | if $driver->Disabled; | ||
| 228 | |||
| 229 | # Collect this logic before $obj changes on the next line via 'replace' | ||
| 230 | my $has_pk = ref $obj && $obj->has_primary_key; | ||
| 231 | my $ret = $driver->fallback->replace($obj); | ||
| 232 | if ($has_pk) { | ||
| 233 | my $key = $driver->cache_key(ref($obj), $obj->primary_key); | ||
| 234 | $driver->modify_cache(sub { | ||
| 235 | $driver->update_cache($key, $driver->deflate($obj)); | ||
| 236 | }); | ||
| 237 | } | ||
| 238 | return $ret; | ||
| 239 | } | ||
| 240 | |||
| 241 | sub remove { | ||
| 242 | my $driver = shift; | ||
| 243 | my($obj) = @_; | ||
| 244 | return $driver->fallback->remove(@_) | ||
| 245 | if $driver->Disabled; | ||
| 246 | |||
| 247 | if ($_[2] && $_[2]->{nofetch}) { | ||
| 248 | ## since direct_remove isn't an object method, it can't benefit | ||
| 249 | ## from inheritance, we're forced to keep things a bit obfuscated here | ||
| 250 | ## (I'd rather have a : sub direct_remove { die "unavailable" } in the driver | ||
| 251 | Carp::croak("nofetch option isn't compatible with a cache driver"); | ||
| 252 | } | ||
| 253 | if (ref $obj) { | ||
| 254 | $driver->uncache_object($obj); | ||
| 255 | } | ||
| 256 | $driver->fallback->remove(@_); | ||
| 257 | } | ||
| 258 | |||
| 259 | sub uncache_object { | ||
| 260 | my $driver = shift; | ||
| 261 | my($obj) = @_; | ||
| 262 | my $key = $driver->cache_key(ref($obj), $obj->primary_key); | ||
| 263 | return $driver->modify_cache(sub { | ||
| 264 | delete $obj->{__cached}; | ||
| 265 | $driver->remove_from_cache($key); | ||
| 266 | $driver->fallback->uncache_object($obj); | ||
| 267 | }); | ||
| 268 | } | ||
| 269 | |||
| 270 | sub cache_key { | ||
| 271 | my $driver = shift; | ||
| 272 | my($class, $id) = @_; | ||
| 273 | if ($class->can('cache_class')) { | ||
| 274 | $class = $class->cache_class; | ||
| 275 | } | ||
| 276 | my $key = join ':', $class, ref($id) eq 'ARRAY' ? @$id : $id; | ||
| 277 | if (my $v = $class->can('cache_version')) { | ||
| 278 | $key .= ':' . $v->(); | ||
| 279 | } | ||
| 280 | return $key; | ||
| 281 | } | ||
| 282 | |||
| 283 | # if we're operating within a transaction then we need to buffer CRUD | ||
| 284 | # and only commit to the cache upon commit | ||
| 285 | sub modify_cache { | ||
| 286 | my ($driver, $cb) = @_; | ||
| 287 | |||
| 288 | unless ($driver->txn_active) { | ||
| 289 | return $cb->(); | ||
| 290 | } | ||
| 291 | $driver->debug(sprintf("%14s", "BUFFER(1)") . ": driver=$driver"); | ||
| 292 | push @{$driver->txn_buffer} => $cb; | ||
| 293 | } | ||
| 294 | |||
| 295 | sub DESTROY { } | ||
| 296 | |||
| 297 | sub AUTOLOAD { | ||
| 298 | my $driver = $_[0]; | ||
| 299 | (my $meth = our $AUTOLOAD) =~ s/.+:://; | ||
| 300 | my $fallback = $driver->fallback; | ||
| 301 | ## Check for invalid methods, but make sure we still allow | ||
| 302 | ## chaining 2 caching drivers together. | ||
| 303 | Carp::croak("Cannot call method '$meth' on object '$driver'") | ||
| 304 | unless $fallback->can($meth) || | ||
| 305 | UNIVERSAL::isa($fallback, __PACKAGE__); | ||
| 306 | { | ||
| 307 | no strict 'refs'; ## no critic | ||
| 308 | *$AUTOLOAD = sub { | ||
| 309 | shift->fallback->$meth(@_); | ||
| 310 | }; | ||
| 311 | } | ||
| 312 | goto &$AUTOLOAD; | ||
| 313 | } | ||
| 314 | |||
| 315 | 1; | ||
| 316 | |||
| 317 | __END__ |