.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Rose::DB::Cache 3pm" .TH Rose::DB::Cache 3pm "2023-03-04" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Rose::DB::Cache \- A mod_perl\-aware cache for Rose::DB objects. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& # Usage \& package My::DB; \& \& use base \*(AqRose::DB\*(Aq; \& ... \& \& $cache = My::DB\->db_cache; \& \& $db = $cache\->get_db(...); \& \& $cache\->set_db($db); \& \& $cache\->clear; \& \& \& # Subclassing \& package My::DB::Cache; \& \& use Rose::DB::Cache; \& our @ISA = qw(Rose::DB::Cache); \& \& # Override methods as desired \& sub get_db { ... } \& sub set_db { ... } \& sub prepare_db { ... } \& sub build_cache_key { ... } \& sub clear { ... } \& ... .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Rose::DB::Cache provides both an \s-1API\s0 and a default implementation of a caching system for Rose::DB objects. Each Rose::DB\-derived class references a Rose::DB::Cache\-derived object to which it delegates cache-related activities. See the new_or_cached method for an example. .PP The default implementation caches and returns Rose::DB objects using the combination of their type and domain as the cache key. There is no cache expiration or other cache cleaning. .PP The only sophistication in the default implementation is that it is mod_perl\- and Apache::DBI\-aware. When running under mod_perl, with or without Apache::DBI, the dbh attribute of each cached Rose::DB object is set to \f(CW\*(C`undef\*(C'\fR at the end of each request. Additionally, any db connections made in a pre-fork parent apache process are not cached. .PP When running under Apache::DBI, the behavior described above will ensure that Apache::DBI's \*(L"ping\*(R" and rollback features work as expected, keeping the \s-1DBI\s0 database handles contained within each Rose::DB object connected and alive. .PP When running under mod_perl \fIwithout\fR Apache::DBI, the behavior described above will use a single \s-1DBI\s0 database connection per cached Rose::DB object per request, but will discard these connections at the end of each request. .PP Both mod_perl 1.x and 2.x are supported. Under mod_perl 2.x, you should load Rose::DB on server startup (e.g., in your \f(CW\*(C`startup.pl\*(C'\fR file). If this is not possible, then you must explicitly tell Rose::DB::Cache that apache has started up already by setting apache_has_started to a true value. .PP Subclasses can override any and all methods described below in order to implement their own caching strategy. .SH "CLASS METHODS" .IX Header "CLASS METHODS" .IP "\fBapache_has_started [\s-1BOOL\s0]\fR" 4 .IX Item "apache_has_started [BOOL]" Get or set a boolean value indicating whether or not apache has completed its startup process. If this value is not set explicitly, a best guess as to the answer will be returned. .IP "\fBbuild_cache_key \s-1PARAMS\s0\fR" 4 .IX Item "build_cache_key PARAMS" Given the name/value pairs \s-1PARAMS,\s0 return a string representing the corresponding cache key. Calls to this method from within Rose::DB::Cache will include at least \f(CW\*(C`type\*(C'\fR and \f(CW\*(C`domain\*(C'\fR parameters, but you may pass any parameters if you override all methods that call this method in your subclass. .IP "\fBdefault_use_cache_during_apache_startup [\s-1BOOL\s0]\fR" 4 .IX Item "default_use_cache_during_apache_startup [BOOL]" Get or set a boolean value that determines the default value of the use_cache_during_apache_startup object attribute. The default value is false. See the use_cache_during_apache_startup documentation for more information. .IP "\fBentry_class [\s-1CLASS\s0]\fR" 4 .IX Item "entry_class [CLASS]" Get or set the name of the Rose::DB::Cache::Entry\-derived class used to store cached Rose::DB objects on behalf of this class. The default value is Rose::DB::Cache::Entry. .SH "CONSTRUCTORS" .IX Header "CONSTRUCTORS" .IP "\fBnew \s-1PARAMS\s0\fR" 4 .IX Item "new PARAMS" Constructs a new Rose::DB::Cache object based on \s-1PARAMS,\s0 where \s-1PARAMS\s0 are name/value pairs. Any object method is a valid parameter name. .SH "OBJECT METHODS" .IX Header "OBJECT METHODS" .IP "\fBclear\fR" 4 .IX Item "clear" Clear the cache entirely. .IP "\fBdb_cache_entries\fR" 4 .IX Item "db_cache_entries" Returns a list (in list context) or reference to an array (in scalar context) of cache entries for each cached db object. .IP "\fBdb_cache_keys\fR" 4 .IX Item "db_cache_keys" Returns a list (in list context) or reference to an array (in scalar context) of keys for each L . .IP "\fBget_db [\s-1PARAMS\s0]\fR" 4 .IX Item "get_db [PARAMS]" Return the cached Rose::DB\-derived object corresponding to the name/value pairs passed in \s-1PARAMS.\s0 \s-1PARAMS\s0 are passed to the build_cache_key method, and the key returned is used to look up the cached object. .Sp If a cached object is found, the prepare_db method is called, passing the cached db object and its corresponding Rose::DB::Cache::Entry object as arguments. The cached db object is then returned. .Sp If no such object exists in the cache, undef is returned. .IP "\fBprepare_for_apache_fork\fR" 4 .IX Item "prepare_for_apache_fork" Prepares the cache for the initial fork of the apache parent process by \fBdisconnect()\fRing all database handles and deleting all cache entries that were created during apache startup. This call is only necessary if running under mod_perl \fIand\fR use_cache_during_apache_startup set set to true. See the use_cache_during_apache_startup documentation for more information. .IP "\fBprepare_db [\s-1DB, ENTRY\s0]\fR" 4 .IX Item "prepare_db [DB, ENTRY]" Prepare the cached Rose::DB\-derived object \s-1DB\s0 for usage. The cached's db object's Rose::DB::Cache::Entry object, \s-1ENTRY,\s0 is also passed. .Sp When \fI\s-1NOT\s0\fR running under mod_perl, this method does nothing. .Sp When running under mod_perl (version 1.x or 2.x), this method will do the following: .RS 4 .IP "\(bu" 4 Any \s-1DBI\s0 database handle created inside a Rose::DB object during apache server startup will be marked as such. Any attempt to use such an object after the apache startup process has completed (i.e., in a child apache process) will cause it to be discarded and replaced. Note that you usually don't want it to come to this. It's better to cleanly disconnect all such database handles before the first apache child forks off. See the documentation for the use_cache_during_apache_startup and prepare_for_apache_fork methods for more information. .IP "\(bu" 4 All \s-1DBI\s0 database handles contained in cached Rose::DB objects will be cleared at the end of each request using a \f(CW\*(C`PerlCleanupHandler\*(C'\fR. This will cause \s-1DBI\-\s0>connect to be called the next time a dbh is requested from a cached Rose::DB object, which in turn will trigger Apache::DBI's ping mechanism to ensure that the database handle is fresh. .RE .RS 4 .Sp Putting all the pieces together, the following implementation of the init_db method in your Rose::DB::Object\-derived common base class will ensure that database connections are shared and fresh under mod_perl and (optionally) Apache::DBI, but \fIunshared\fR elsewhere: .Sp .Vb 1 \& package My::DB::Object; \& \& use base \*(AqRose::DB::Object\*(Aq; \& \& use My::DB; # isa Rose::DB \& ... \& \& BEGIN: \& { \& if($ENV{\*(AqMOD_PERL\*(Aq}) \& { \& *init_db = sub { My::DB\->new_or_cached }; \& } \& else # act "normally" when not under mod_perl \& { \& *init_db = sub { My::DB\->new }; \& } \& } .Ve .RE .IP "\fBset_db \s-1DB\s0\fR" 4 .IX Item "set_db DB" Add the Rose::DB\-derived object \s-1DB\s0 to the cache. The \s-1DB\s0's domain, type, and the db object itself (under the parameter name \f(CW\*(C`db\*(C'\fR) are all are passed to the build_cache_key method and the \s-1DB\s0 object is stored under the key returned. .Sp If running under mod_perl \fIand\fR the apache server is starting up \fIand\fR use_cache_during_apache_startup is set to true, then the \s-1DB\s0 object is \fInot\fR added to the cache, but merely returned. .IP "\fBuse_cache_during_apache_startup [\s-1BOOL\s0]\fR" 4 .IX Item "use_cache_during_apache_startup [BOOL]" Get or set a boolean value that determines whether or not to cache database objects during the apache server startup process. The default value is determined by the default_use_cache_during_apache_startup class method. .Sp \&\s-1DBI\s0 database handles created in the parent apache process cannot be used in child apache processes. Furthermore, in the case of at least one one \s-1DBI\s0 driver class, you must \fIalso\fR ensure that any database handles created in the apache parent process during server startup are properly \fBdisconnect()\fRed \fIbefore\fR you fork off the first apache child. Failure to do so may cause segmentation faults(!) in child apache processes. .Sp The upshot is that if use_cache_during_apache_startup is set to true, you should call prepare_for_apache_fork at the very end of the apache startup process (i.e., once all other Perl modules have been loaded and all other Perl code has run). This is usually done by placing a call at the bottom of the traditional \f(CW\*(C`startup.pl\*(C'\fR file. Assuming \f(CW\*(C`My::DB\*(C'\fR is your Rose::DB\-derived class: .Sp .Vb 1 \& My::DB\->db_cache\->prepare_for_apache_fork(); .Ve .Sp A convenience method exists in Rose::DB as well, which simply translates into call shown above: .Sp .Vb 1 \& My::DB\->prepare_cache_for_apache_fork(); .Ve .SH "AUTHOR" .IX Header "AUTHOR" John C. Siracusa (siracusa@gmail.com) .SH "LICENSE" .IX Header "LICENSE" Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.