| 1 | package Lyrics::Fetcher::Cache; | ||||||||||
| 2 | # $Id$ | ||||||||||
| 3 | |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | $VERSION = '0.0.2'; | |||||||||
| 9 | |||||||||||
| 10 | |||||||||||
| 11 | 2 | my $cache; | |||||||||
| 12 | 2 | my $cache_size = 1048576; # maximum cache size, in bytes | |||||||||
| 13 | |||||||||||
| 14 | |||||||||||
| 15 | # creating an instance of the different caching modules varies slightly, | ||||||||||
| 16 | # but once created they're used in the same way (->get() and ->set() calls). | ||||||||||
| 17 | my %caches = ( | ||||||||||
| 18 | 0 | ▶ | 'Cache::Memory' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 19 | 0 | Cache::Memory->new( | |||||||||
| 20 | removal_strategy => 'Cache::RemovalStrategy::LRU', | ||||||||||
| 21 | size_limit => $cache_size, | ||||||||||
| 22 | ); | ||||||||||
| 23 | }, | ||||||||||
| 24 | |||||||||||
| 25 | 2 | ▶ | 'Cache::SizeAwareMemoryCache' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 2 | Cache::SizeAwareMemoryCache->new({ | |||||||||
| 27 | 'namespace' => 'LyricsFetcher', | ||||||||||
| 28 | 'max_size' => $cache_size, | ||||||||||
| 29 | }); | ||||||||||
| 30 | }, | ||||||||||
| 31 | 2 | ); | |||||||||
| 32 | |||||||||||
| 33 | 2 | for my $cachemodule (keys %caches) { | |||||||||
| 34 | 2 | eval "require $cachemodule"; | |||||||||
| 35 | 2 | ▶ | if (!$@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 2 | $cache = $caches{$cachemodule}->(); | |||||||||
| 37 | 2 | last; | |||||||||
| 38 | } | ||||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | 0 | ▶ | sub get { | ||||||||
|
Subroutine
get: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 43 | 0 | ▶ | return if !$cache; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | return $cache->get(join ':', @_); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | 0 | ▶ | sub set { | ||||||||
|
Subroutine
set: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 49 | 0 | ▶ | return if !$cache; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 0 | my ($artist, $title, $lyrics) = @_; | |||||||||
| 51 | 0 | return $cache->set(join(':', $artist, $title), $lyrics); | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | # this is simply in case something calls us as a fetcher module by mistake | ||||||||||
| 56 | # (as L::F v0.5.0 did) | ||||||||||
| 57 | 0 | ▶ | sub fetch { | ||||||||
|
Subroutine
fetch: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 58 | 0 | return __PACKAGE__::get(@_); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | |||||||||||
| 62 | =head1 NAME | ||||||||||
| 63 | |||||||||||
| 64 | Lyrics::Fetcher::Cache - implement caching of lyrics | ||||||||||
| 65 | |||||||||||
| 66 | =head1 DESCRIPTION | ||||||||||
| 67 | |||||||||||
| 68 | This module deals with the caching of lyrics for Lyrics::Fetcher, using whatever | ||||||||||
| 69 | supported caching methods are available. | ||||||||||
| 70 | |||||||||||
| 71 | This is not intended to be used directly, it should be called solely by | ||||||||||
| 72 | Lyrics::Fetcher. See L<Lyrics::Fetcher> for usage details. | ||||||||||
| 73 | |||||||||||
| 74 | |||||||||||
| 75 | =head1 INTERFACE | ||||||||||
| 76 | |||||||||||
| 77 | =over 4 | ||||||||||
| 78 | |||||||||||
| 79 | =item I<get>($artist, $title) | ||||||||||
| 80 | |||||||||||
| 81 | Attempt to fetch from whatever cache module we managed to use | ||||||||||
| 82 | |||||||||||
| 83 | =item I<set>($artist, $title, $lyrics) | ||||||||||
| 84 | |||||||||||
| 85 | Attempt to store the value into the cache | ||||||||||
| 86 | |||||||||||
| 87 | =item I<fetch>($artist, $title) | ||||||||||
| 88 | |||||||||||
| 89 | Alias for get, primarily in case this module gets mistaken for a normal | ||||||||||
| 90 | fetcher module. Don't call this, call get(). | ||||||||||
| 91 | |||||||||||
| 92 | =back | ||||||||||
| 93 | |||||||||||
| 94 | |||||||||||
| 95 | =head1 BUGS | ||||||||||
| 96 | |||||||||||
| 97 | There are no known bugs, if you catch one please let me know. | ||||||||||
| 98 | |||||||||||
| 99 | |||||||||||
| 100 | =head1 CONTACT AND COPYRIGHT | ||||||||||
| 101 | |||||||||||
| 102 | Copyright 2007-2008 David Precious <davidp@preshweb.co.uk> (CPAN Id: BIGPRESH) | ||||||||||
| 103 | |||||||||||
| 104 | All comments / suggestions / bug reports gratefully received (ideally use the | ||||||||||
| 105 | RT installation at http://rt.cpan.org/ but mail me direct if you prefer) | ||||||||||
| 106 | |||||||||||
| 107 | Previously: | ||||||||||
| 108 | Copyright 2003 Sir Reflog <reflog@mail15.com>. | ||||||||||
| 109 | Copyright 2003 Zachary P. Landau <kapheine@hypa.net> | ||||||||||
| 110 | |||||||||||
| 111 | |||||||||||
| 112 | =head1 LICENSE | ||||||||||
| 113 | |||||||||||
| 114 | All rights reserved. This program is free software; you can redistribute it | ||||||||||
| 115 | and/or modify it under the same terms as Perl itself. | ||||||||||
| 116 | |||||||||||
| 117 | =cut | ||||||||||
| 118 | |||||||||||
| 119 | |||||||||||
| 120 | |||||||||||
| 121 | 2 | 1; | |||||||||