Lyrics::Fetcher::Cache - implement caching of lyrics


Lyrics-Fetcher documentation Contained in the Lyrics-Fetcher distribution.

Index


Code Index:

NAME

Top

Lyrics::Fetcher::Cache - implement caching of lyrics

DESCRIPTION

Top

This module deals with the caching of lyrics for Lyrics::Fetcher, using whatever supported caching methods are available.

This is not intended to be used directly, it should be called solely by Lyrics::Fetcher. See Lyrics::Fetcher for usage details.

INTERFACE

Top

get($artist, $title)

Attempt to fetch from whatever cache module we managed to use

set($artist, $title, $lyrics)

Attempt to store the value into the cache

fetch($artist, $title)

Alias for get, primarily in case this module gets mistaken for a normal fetcher module. Don't call this, call get().

BUGS

Top

There are no known bugs, if you catch one please let me know.

CONTACT AND COPYRIGHT

Top

LICENSE

Top

All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Lyrics-Fetcher documentation Contained in the Lyrics-Fetcher distribution.

package Lyrics::Fetcher::Cache;
# $Id$

use strict;
use warnings;

use vars qw($VERSION);
$VERSION = '0.0.2';


my $cache;
my $cache_size = 1048576;  # maximum cache size, in bytes


# creating an instance of the different caching modules varies slightly,
# but once created they're used in the same way (->get() and ->set() calls).
my %caches = (
    'Cache::Memory' => sub {
        Cache::Memory->new(
            removal_strategy => 'Cache::RemovalStrategy::LRU',
            size_limit       => $cache_size,
        );
    },

    'Cache::SizeAwareMemoryCache' => sub {
        Cache::SizeAwareMemoryCache->new({ 
                                        'namespace' => 'LyricsFetcher',
                                        'max_size'  => $cache_size,
        });
    },
);

for my $cachemodule (keys %caches) {
    eval "require $cachemodule";
    if (!$@) {
        $cache = $caches{$cachemodule}->();
        last;
    }
}


sub get {
    return if !$cache;
    return $cache->get(join ':', @_);
}


sub set {
    return if !$cache;
    my ($artist, $title, $lyrics) = @_;
    return $cache->set(join(':', $artist, $title), $lyrics);
}


# this is simply in case something calls us as a fetcher module by mistake
# (as L::F v0.5.0 did)
sub fetch {
    return __PACKAGE__::get(@_);
}




1;