John Siracusa wrote on 10/27/07 8:43 PM:
> On 10/27/07, Peter Karman <[EMAIL PROTECTED]> wrote:
>> I don't really see the point of caching Rose::DB objects myself. They're 
>> cheap
>> to create, compared to DBI handles, and the only use I can see in caching 
>> them
>> is to make use of the DBI handle they hold. In which case, why not just use 
>> the
>> DBI caching mechanism instead.
> 
> Well, cheap is relative.  Some people consider it wasteful to keep
> creating new, essentially "identical" objects.

I agree. Cheap is relative, especially compared to DBI connections. If by
'identical' in this case we mean 'have the same type and domain' then the
attached patch implements that.

> 
>> Perrin originally suggested connect_cached() as a solution to a transaction 
>> bug
>> w.r.t Apache::DBI:
> 
> Caching the whole Rose::DB object should have a similar effect, right?
> 

Seems to. The attached patch passes the same test the other 2 did. For my
purposes (re-using the same DBI connection), it's 6 or a half-dozen.

-- 
Peter Karman  .  http://peknet.com/  .  [EMAIL PROTECTED]
Index: t/dbh_cache.t
===================================================================
--- t/dbh_cache.t       (revision 0)
+++ t/dbh_cache.t       (revision 0)
@@ -0,0 +1,40 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+BEGIN {
+    require Test::More;
+    eval { require DBD::SQLite };
+
+    if ( $@ || $DBD::SQLite::VERSION < 1.08 || $ENV{'RDBO_NO_SQLITE'} ) {
+        Test::More->import(
+            skip_all => $ENV{'RDBO_NO_SQLITE'}
+            ? 'SQLite tests disabled'
+            : 'Missing DBD::SQLite 1.08+'
+        );
+    }
+    elsif ( $DBD::SQLite::VERSION == 1.13 )
+    {
+        Test::More->import( skip_all => 'DBD::SQLite 1.13 is broken' );
+    }
+    else {
+        Test::More->import( tests => 5 );
+    }
+}
+
+BEGIN {
+    require 't/test-lib.pl';
+    use_ok('Rose::DB');
+}
+
+Rose::DB->default_domain('test');
+Rose::DB->default_type('sqlite_admin');
+
+ok( my $db = Rose::DB->new_or_cached(), "new_or_cached db" );
+
+ok( ref $db && $db->isa('Rose::DB'), 'new()' );
+
+ok( my $db2 = Rose::DB->new_or_cached(), "new_or_cached db2" );
+
+is( $db->dbh, $db2->dbh, "same DBI handle used" );
+
Index: lib/Rose/DB.pm
===================================================================
--- lib/Rose/DB.pm      (revision 1470)
+++ lib/Rose/DB.pm      (working copy)
@@ -53,6 +53,15 @@
   ],
 );
 
+use Rose::Class::MakeMethods::Generic 
+(
+  hash   => 
+  [
+    db_cache        => { interface => 'get_set_all' },
+    clear_db_cache  => { interface => 'clear', hash_key => 'db_cache' },
+  ],
+);
+
 __PACKAGE__->default_domain('default');
 __PACKAGE__->default_type('default');
 
@@ -366,6 +375,35 @@
   return $self;
 }
 
+sub new_or_cached
+{
+  my($class) = shift;
+
+  @_ = (type => $_[0])  if(@_ == 1);
+
+  my %args = @_;
+
+  my $domain = 
+    exists $args{'domain'} ? delete $args{'domain'} : $class->default_domain;
+
+  my $type = 
+    exists $args{'type'} ? delete $args{'type'} : $class->default_type;
+    
+  my $key = join('.', $domain, $type);
+  
+  $class->db_cache({}) unless $class->db_cache;
+  
+  if (exists $class->db_cache->{$key})
+  {
+    return $class->db_cache->{$key};
+  }
+  
+  my $db = $class->new(%args);
+  $class->db_cache->{$key} = $db;
+  
+  return $db;
+}
+
 sub class 
 {
   my($self) = shift;
@@ -2759,6 +2797,11 @@
 
 You can change this mapping with the L<driver_class|/driver_class> class 
method.
 
+=item B<new_or_cached PARAMS>
+
+Acts like new() but will return a cached Rose::DB object if called before with 
the same
+type() and domain().
+
 =back
 
 =head1 OBJECT METHODS
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to