From 55872fd8048745f35fb1f5f48aae2bda20e324ca Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 16 Jun 2014 13:51:48 +0200 Subject: [PATCH] =?utf8?q?SL::Request:=20Funktion=20zum=20Cachen=20von=20O?= =?utf8?q?bjekten=20f=C3=BCr=20Dauer=20des=20Requests?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Request.pm | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/SL/Request.pm b/SL/Request.pm index 26be5f79b..47f163ca9 100644 --- a/SL/Request.pm +++ b/SL/Request.pm @@ -41,6 +41,17 @@ sub init_type { return 'html'; } +sub cache { + my ($self, $topic, $default) = @_; + + $topic = '::' . (caller(0))[0] . "::$topic" unless $topic =~ m{^::}; + + $self->{_cache} //= {}; + $self->{_cache}->{$topic} //= ($default // {}); + + return $self->{_cache}->{$topic}; +} + sub _store_value { my ($target, $key, $value) = @_; my @tokens = split /((?:\[\+?\])?(?:\.)|(?:\[\+?\]))/, $key; @@ -533,6 +544,21 @@ of L. Defaults to an isntance of L. For more information about layouts, see L. +=item C + +Caches an item for the duration of the request. C<$topic> must be an +index name referring to the thing to cache. It is used for retrieving +it later on. If C<$topic> doesn't start with C<::> then the caller's +package name is prepended to the topic. For example, if the a from +package C calls with topic = C then the +actual key will be C<::SL::StuffedStuff::get_stuff>. + +If no item exists in the cache for C<$topic> then it is created and +its initial value is set to C<$default>. If C<$default> is not given +(undefined) then a new, empty hash reference is created. + +Returns the cached item. + =back =head1 SPECIAL FUNCTIONS -- 2.20.1