| 1 | package WWW::2ch::Cache; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | use Storable qw(freeze thaw); | ||
| 5 | |||
| 6 | use WWW::2ch::Cache::File; | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my $class = shift; | ||
| 10 | my $cache = shift; | ||
| 11 | |||
| 12 | my $self = bless {}, $class; | ||
| 13 | |||
| 14 | if (ref($cache) =~ /^Cache::/) { | ||
| 15 | $self->{cache} = $cache; | ||
| 16 | } elsif ($cache) { | ||
| 17 | $self->{cache} = WWW::2ch::Cache::File->new($cache); | ||
| 18 | } | ||
| 19 | $self; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub set { | ||
| 23 | my ($self, $key, $data) = @_; | ||
| 24 | return unless $self->{cache}; | ||
| 25 | $self->{cache}->set($key, freeze $data); | ||
| 26 | } | ||
| 27 | |||
| 28 | sub get { | ||
| 29 | my ($self, $key) = @_; | ||
| 30 | return +{} unless $self->{cache}; | ||
| 31 | |||
| 32 | my $data = $self->{cache}->get($key); | ||
| 33 | return +{} unless $data; | ||
| 34 | thaw $data; | ||
| 35 | } | ||
| 36 | |||
| 37 | 1; |