summaryrefslogtreecommitdiff
path: root/lib/MON/Cache.pm
blob: 21b59f55933db5f5089736c5c0efd277503a573b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package MON::Cache;

use strict;
use warnings;
use v5.10;
use autodie;

use Data::Dumper;

use MON::Display;
use MON::Config;
use MON::Utils;

our @ISA = ('MON::Display');

sub new {
  my ( $class, %opts ) = @_;

  my $self = bless \%opts, $class;

  $self->init();

  return $self;
}

sub init {
  my ($self) = @_;

  $self->clear();

  return undef;
}

sub clear {
  my ($self) = @_;

  $self->{cache} = {};

  return undef;
}

sub magic {
  my ( $self, $key, $sub ) = @_;

  my $cache = $self->{cache};

  if ( exists $cache->{$key} ) {
    $self->verbose("Delivering '$key' from cache");
    return $cache->{$key};
  }

  return $cache->{$key} = $sub->();
}

1;