Permalink
Browse files

Caching of DBIC stored value.

  • Loading branch information...
1 parent 75b67d8 commit f74d73f83fa4eb3514195a75d3328574cc6c90e8 @bvr committed Mar 11, 2012
Showing with 28 additions and 0 deletions.
  1. +28 −0 scripts/2012-03-11-return-cache.pl
@@ -0,0 +1,28 @@
+
+package Item;
+use Moose;
+
+sub cached {
+ my ($self, $cache_key, $cb) = @_;
+
+ if(defined(my $data = $self->{$cache_key})) {
+ return $data;
+ }
+
+ # calculate item, store into cache and return it
+ return $self->{$cache_key} = $cb->($self);
+}
+
+sub payload_spare {
+ shift->cached('_payload_spare' => sub {
+ warn "calculation";
+ 145
+ });
+}
+
+package main;
+
+my $item = Item->new();
+
+warn $item->payload_spare;
+warn $item->payload_spare;

0 comments on commit f74d73f

Please sign in to comment.