base cache key on sql and binds plus dbname and username
[dbsrgits/DBIx-Class-Cursor-Cached.git] / lib / DBIx / Class / Cursor / Cached.pm
index 5eaf6b0..758cf5d 100644 (file)
@@ -48,7 +48,24 @@ sub reset {
 
 sub _build_cache_key {
   my ($class, $storage, $args, $attrs) = @_;
-  return Digest::SHA1::sha1_hex(Storable::nfreeze([ $args, $attrs ]));
+  # compose the query and bind values, like as_query(),
+  # so the cache key is only affected by what the database sees
+  # and not any other cruft in $attrs
+  my $ref = $storage->_select_args_to_query(@{$args}[0..2], $attrs);
+  my $connect_info = $storage->_dbi_connect_info;
+  my ($dbname, $username);
+  if (ref($connect_info->[0]) eq 'CODE') {
+    my $dbh = $connect_info->[0]->();
+    $dbname = $dbh->{Name};
+    $username = $dbh->{Username} || '';
+  } else {
+    $dbname = $connect_info->[0];
+    $username = $connect_info->[1] || '';
+  }
+  
+  local $Storable::canonical = 1;
+  return Digest::SHA1::sha1_hex(Storable::nfreeze( [ $ref, $dbname, $username ] ));
+
 }
 
 sub _fill_data {