| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Lyrics::Fetcher::Cache; | 
| 2 |  |  |  |  |  |  | # $Id$ | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 34 |  | 
| 5 | 1 |  |  | 1 |  | 4 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 23 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 4 | use vars qw($VERSION); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 304 |  | 
| 8 |  |  |  |  |  |  | $VERSION = '0.0.2'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | my $cache; | 
| 12 |  |  |  |  |  |  | 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 |  |  |  |  |  |  | 'Cache::Memory' => sub { | 
| 19 |  |  |  |  |  |  | Cache::Memory->new( | 
| 20 |  |  |  |  |  |  | removal_strategy => 'Cache::RemovalStrategy::LRU', | 
| 21 |  |  |  |  |  |  | size_limit       => $cache_size, | 
| 22 |  |  |  |  |  |  | ); | 
| 23 |  |  |  |  |  |  | }, | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | 'Cache::SizeAwareMemoryCache' => sub { | 
| 26 |  |  |  |  |  |  | Cache::SizeAwareMemoryCache->new({ | 
| 27 |  |  |  |  |  |  | 'namespace' => 'LyricsFetcher', | 
| 28 |  |  |  |  |  |  | 'max_size'  => $cache_size, | 
| 29 |  |  |  |  |  |  | }); | 
| 30 |  |  |  |  |  |  | }, | 
| 31 |  |  |  |  |  |  | ); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | for my $cachemodule (keys %caches) { | 
| 34 |  |  |  |  |  |  | eval "require $cachemodule"; | 
| 35 |  |  |  |  |  |  | if (!$@) { | 
| 36 |  |  |  |  |  |  | $cache = $caches{$cachemodule}->(); | 
| 37 |  |  |  |  |  |  | last; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | sub get { | 
| 43 | 0 | 0 |  | 0 | 1 |  | return if !$cache; | 
| 44 | 0 |  |  |  |  |  | return $cache->get(join ':', @_); | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | sub set { | 
| 49 | 0 | 0 |  | 0 | 1 |  | return if !$cache; | 
| 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 |  |  |  |  |  |  | sub fetch { | 
| 58 | 0 |  |  | 0 | 1 |  | 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 for usage details. | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =head1 INTERFACE | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | =over 4 | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =item I($artist, $title) | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | Attempt to fetch from whatever cache module we managed to use | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | =item I($artist, $title, $lyrics) | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | Attempt to store the value into the cache | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | =item I($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  (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 . | 
| 109 |  |  |  |  |  |  | Copyright 2003 Zachary P. Landau | 
| 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 |  |  |  |  |  |  | 1; |