Rose::DB::Cache(3pm)
π Rose::DB::Cache π
okay bestie, welcome to the manual page for Rose::DB::Cache (that's the <pink>Rose::DB::Cache(3pm)</pink> situation) β a mod_perl-aware cache for Rose::DB objects. she's lowkey iconic, let's get into it π«Ά
β¨ SYNOPSIS β¨
# Usage
package My::DB;
use base 'Rose::DB';
...
$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 { ... }
...
πΈ DESCRIPTION πΈ
Rose::DB::Cache hands you both an API and a default implementation of a caching system for Rose::DB objects. Every Rose::DB-derived class points at a Rose::DB::Cache-derived object, and that object is who gets all the cache-related errands delegated to it. wanna see it in action? peek at the new_or_cached method for an example. π
The default implementation caches and returns Rose::DB objects using the combo of their type and domain as the cache key. That's it. No cache expiration, no other cache cleaning. it's giving minimalist, and honestly? refreshing.
The only sophistication in the default implementation is that it's mod_perl- and Apache::DBI-aware. When you're running under mod_perl, with or without Apache::DBI, the dbh attribute of each cached Rose::DB object gets set to undef at the end of each request. Plus, any db connections made in a pre-fork parent apache process are not cached. mmkay? π¦
When running under Apache::DBI, that behavior described above will make sure Apache::DBI's "ping" and rollback features work exactly as expected, keeping the DBI database handles contained within each Rose::DB object connected and alive. slay. β¨
When running under mod_perl without Apache::DBI, that same behavior uses a single DBI database connection per cached Rose::DB object per request, but it'll discard those connections at the end of each request. periodt.
Both mod_perl 1.x and 2.x are supported. Under mod_perl 2.x, you should load Rose::DB on server startup (like, in your "startup.pl" file). If that's not possible, then you must explicitly tell Rose::DB::Cache that apache has already started up by setting apache_has_started to a true value. no skipping this bestie, it matters π
Subclasses can override any and all methods described below to implement their own caching strategy. go off, customize your little heart out π
π CLASS METHODS π
apache_has_started [BOOL]β get or set a boolean value saying whether apache has completed its startup process or not. If you don't set this value explicitly, you'll get back a best guess as the answer. it's doing its best π₯Ίbuild_cache_key PARAMSβ given the name/value pairs in PARAMS, return a string representing the corresponding cache key. Calls to this method from insideRose::DB::Cachewill include at least the"type"and"domain"parameters, but bestie you may pass any parameters if you override all the methods that call this method in your subclass. your house, your rules πdefault_use_cache_during_apache_startup [BOOL]β get or set a boolean value that determines the default value of theuse_cache_during_apache_startupobject attribute. The default value is false. See theuse_cache_during_apache_startupdocumentation for more info. πentry_class [CLASS]β get or set the name of theRose::DB::Cache::Entry-derived class used to store cachedRose::DBobjects on behalf of this class. The default value isRose::DB::Cache::Entry. she's the default for a reason π
π§ CONSTRUCTORS π§
new PARAMSβ constructs a brand newRose::DB::Cacheobject based on PARAMS, where PARAMS are name/value pairs. Any object method is a valid parameter name. yes, any of them. build her however you like π
π OBJECT METHODS π
clearβ clear the cache entirely. poof. gone. πdb_cache_entriesβ returns a list (in list context) or a reference to an array (in scalar context) of cache entries for each cached db object.db_cache_keysβ returns a list (in list context) or a reference to an array (in scalar context) of keys for each cache entry (Rose::DB::Cache::Entry).get_db [PARAMS]β return the cachedRose::DB-derived object corresponding to the name/value pairs passed in PARAMS. Those PARAMS get passed straight to thebuild_cache_keymethod, and the key that comes back is what's used to look up the cached object.
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. Then the cached db object is returned. π«Ά
If no such object exists in the cache, undef is returned. sad girl hours π
prepare_for_apache_forkβ prepares the cache for the initial fork of the apache parent process bydisconnect()ing all database handles and deleting all cache entries that were created during apache startup. This call is only necessary if you're running under mod_perl anduse_cache_during_apache_startupis set to true. See theuse_cache_during_apache_startupdocumentation for more. π§Ήprepare_db [DB, ENTRY]β prepare the cachedRose::DB-derived object DB for usage. The cached db object'sRose::DB::Cache::Entryobject, ENTRY, also gets passed along.
When NOT running under mod_perl, this method does nothing. literally nothing. it's a vibe, not a worker.
When running under mod_perl (version 1.x or 2.x), this method will do the following:
- Any DBI database handle created inside a
Rose::DBobject 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 theuse_cache_during_apache_startupandprepare_for_apache_forkmethods for more info. π - All DBI database handles contained in cached
Rose::DBobjects will be cleared at the end of each request using a"PerlCleanupHandler". This causesDBI->connectto be called the next time adbhis requested from a cachedRose::DBobject, which in turn triggersApache::DBI's ping mechanism to make sure the database handle is fresh. β¨
Putting all the pieces together, this 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 unshared everywhere else:
package My::DB::Object;
use base 'Rose::DB::Object';
use My::DB; # isa Rose::DB
...
BEGIN:
{
if($ENV{'MOD_PERL'})
{
*init_db = sub { My::DB->new_or_cached };
}
else # act "normally" when not under mod_perl
{
*init_db = sub { My::DB->new };
}
}
set_db DBβ add theRose::DB-derived object DB to the cache. The DB's domain, type, and the db object itself (under the parameter name"db") all get passed to thebuild_cache_keymethod, and the DB object is stored under the key that comes back. ποΈ
If you're running under mod_perl and the apache server is starting up and use_cache_during_apache_startup is set to true, then the DB object is not added to the cache β it's merely returned. no storage, just vibes.
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 thedefault_use_cache_during_apache_startupclass method.
DBI database handles created in the parent apache process cannot be used in child apache processes. Furthermore, in the case of at least one DBI driver class, you must also ensure that any database handles created in the apache parent process during server startup are properly disconnect()ed before you fork off the first apache child. Failure to do so may cause segmentation faults(!) in child apache processes. bestie I am not joking, segfaults are the opposite of slay π
So the upshot is: 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 "startup.pl" file. Assuming "My::DB" is your Rose::DB-derived class:
My::DB->db_cache->prepare_for_apache_fork();
There's a convenience method in Rose::DB too, which simply translates into the call shown above:
My::DB->prepare_cache_for_apache_fork();
π AUTHOR π
John C. Siracusa (siracusa@gmail.com)
π 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.
and that's the whole page bestie, you ate it up π this was perl v5.40.1, dated 2026-07-31, and the manpage itself is Rose::DB::Cache(3pm) β go forth and cache responsibly, ily π
π¦ where this page comes from
- ubuntu stonking: librose-db-perl 0.788-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms