.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35) .\" .\" 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::Object::Cached 3pm" .TH Rose::DB::Object::Cached 3pm "2020-06-21" "perl v5.30.3" "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::Object::Cached \- Memory cached object representation of a single row in a database table. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& package Category; \& \& use base \*(AqRose::DB::Object::Cached\*(Aq; \& \& _\|_PACKAGE_\|_\->meta\->setup \& ( \& table => \*(Aqcategories\*(Aq, \& \& columns => \& [ \& id => { type => \*(Aqint\*(Aq, primary_key => 1 }, \& name => { type => \*(Aqvarchar\*(Aq, length => 255 }, \& description => { type => \*(Aqtext\*(Aq }, \& ], \& \& unique_key => \*(Aqname\*(Aq, \& ); \& \& ... \& \& $cat1 = Category\->new(id => 123, \& name => \*(AqArt\*(Aq); \& \& $cat1\->save or die $category\->error; \& \& \& $cat2 = Category\->new(id => 123); \& \& # This will load from the memory cache, not the database \& $cat2\->load or die $cat2\->error; \& \& # $cat2 is the same object as $cat1 \& print "Yep, cached" if($cat1 eq $cat2); \& \& # No, really, it\*(Aqs the same object \& $cat1\->name(\*(AqBlah\*(Aq); \& print $cat2\->name; # prints "Blah" \& \& # The object cache supports time\-based expiration \& Category\->cached_objects_expire_in(\*(Aq15 minutes\*(Aq); \& \& $cat1 = Category\->new(id => 123); \& $cat1\->save or $cat1\->die; \& \& $cat1\->load; # loaded from cache \& \& $cat2 = Category\->new(id => 123); \& $cat2\->load; # loaded from cache \& \& <15 minutes pass> \& \& $cat3 = Category\->new(id => 123); \& $cat3\->load; # NOT loaded from cache \& \& ... .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\f(CW\*(C`Rose::DB::Object::Cached\*(C'\fR is a subclass of Rose::DB::Object that is backed by a write-through memory cache. Whenever an object is loaded from or saved to the database, it is cached in memory. Any subsequent attempt to load an object of the same class with the same primary key or unique key value(s) will give you the cached object instead of loading from the database. .PP This means that \fImodifications to an object will also modify all other objects in memory that have the same primary key.\fR The synopsis above highlights this fact. .PP This class is most useful for encapsulating \*(L"read-only\*(R" rows, or other data that is updated very infrequently. In the \f(CW\*(C`Category\*(C'\fR example above, it would be inefficient to repeatedly load category information in a long-running process (such as a mod_perl Apache web server) if that information changes infrequently. .PP The memory cache can be cleared for an individual object or all objects of the same class. There is also support for simple time-based cache expiration. See the clear_object_cache and cached_objects_expire_in methods for more information. .PP Only the methods that are overridden or otherwise behaviorally modified are documented here. See the Rose::DB::Object documentation for the rest. .SH "CLASS METHODS" .IX Header "CLASS METHODS" .IP "\fBcached_objects_expire_in [\s-1DURATION\s0]\fR" 4 .IX Item "cached_objects_expire_in [DURATION]" This method controls the expiration of cached objects. .Sp If called with no arguments, the cache expiration limit in seconds is returned. If passed a \s-1DURATION,\s0 the cache expiration is set. Valid formats for \s-1DURATION\s0 are in the form \*(L"\s-1NUMBER UNIT\*(R"\s0 where \s-1NUMBER\s0 is a positive number and \s-1UNIT\s0 is one of the following: .Sp .Vb 6 \& s sec secs second seconds \& m min mins minute minutes \& h hr hrs hour hours \& d day days \& w wk wks week weeks \& y yr yrs year years .Ve .Sp All formats of the \s-1DURATION\s0 argument are converted to seconds. Days are exactly 24 hours, weeks are 7 days, and years are 365 days. .Sp If an object was read from the database the specified number of seconds ago or earlier, it is purged from the cache and reloaded from the database the next time it is loaded. .Sp A cached_objects_expire_in value of undef or zero means that nothing will ever expire from the object cache. This is the default. .IP "\fBclear_object_cache\fR" 4 .IX Item "clear_object_cache" Clear the memory cache for all objects of this class. .SH "OBJECT METHODS" .IX Header "OBJECT METHODS" .IP "\fBdelete [\s-1PARAMS\s0]\fR" 4 .IX Item "delete [PARAMS]" This method works like the delete method from Rose::DB::Object except that it also calls the forget method if the object was deleted successfully or did not exist in the first place. .IP "\fBforget\fR" 4 .IX Item "forget" Delete the current object from the memory cache. .IP "\fBload [\s-1PARAMS\s0]\fR" 4 .IX Item "load [PARAMS]" Load an object based on either a primary key or a unique key. .Sp If the object exists in the memory cache, the current object \*(L"becomes\*(R" the cached object. See the synopsis or description above for more information. .Sp If the object is not in the memory cache, it is loaded from the database. If the load succeeds, it is also written to the memory cache. .Sp \&\s-1PARAMS\s0 are name/value pairs, and are optional. Valid parameters are: .RS 4 .IP "\fBrefresh\fR" 4 .IX Item "refresh" If set to a true value, then the data is always loaded from the database rather than from the memory cache. If the load succeeds, the object replaces whatever was in the cache. If it fails, the cache is not modified. .RE .RS 4 .Sp Returns true if the object was loaded successfully, false if the row could not be loaded or did not exist in the database. The true value returned on success will be the object itself. If the object overloads its boolean value such that it is not true, then a true value will be returned instead of the object itself. .RE .IP "\fBinsert [\s-1PARAMS\s0]\fR" 4 .IX Item "insert [PARAMS]" This method does the same thing as the Rose::DB::Object method of the same name, except that it also saves the object to the memory cache if the insert succeeds. If it fails, the memory cache is not modified. .IP "\fBremember\fR" 4 .IX Item "remember" Save the current object to the memory cache \fIwithout\fR saving it to the database as well. Objects are cached based on their primary key values and all their unique key values. .IP "\fBremember_all [\s-1PARAMS\s0]\fR" 4 .IX Item "remember_all [PARAMS]" Load and remember all objects from this table, optionally filtered by \s-1PARAMS\s0 which can be any valid Rose::DB::Object::Manager\->\fBget_objects()\fR parameters. Remembered objects will replace any previously cached objects with the same keys. .IP "\fBremember_by_primary_key [\s-1PARAMS\s0]\fR" 4 .IX Item "remember_by_primary_key [PARAMS]" Save the current object to the memory cache \fIwithout\fR saving it to the database as well. The object will be cached based on its primary key value \fIonly\fR. This is unlike the remeber method which caches objects based on their primary key values and all their unique key values. .IP "\fBsave [\s-1PARAMS\s0]\fR" 4 .IX Item "save [PARAMS]" This method does the same thing as the Rose::DB::Object method of the same name, except that it also saves the object to the memory cache if the save succeeds. If it fails, the memory cache is not modified. .IP "\fBupdate [\s-1PARAMS\s0]\fR" 4 .IX Item "update [PARAMS]" This method does the same thing as the Rose::DB::Object method of the same name, except that it also saves the object to the memory cache if the update succeeds. If it fails, the memory cache is not modified. .SH "RESERVED METHODS" .IX Header "RESERVED METHODS" In addition to the reserved methods listed in the Rose::DB::Object documentation, the following method names are also reserved for objects that inherit from this class: .PP .Vb 6 \& cached_objects_expire_in \& clear_object_cache \& forget \& remember \& remember_all \& remember_by_primary_key .Ve .PP If you have a column with one of these names, you must alias it. See the Rose::DB::Object documentation for more information on column aliasing and reserved methods. .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.