| 1 | package Git::SVN::Memoize::YAML; |
| 2 | use warnings $ENV{GIT_PERL_FATAL_WARNINGS} ? qw(FATAL all) : (); |
| 3 | use strict; |
| 4 | use YAML::Any (); |
| 5 | |
| 6 | # based on Memoize::Storable. |
| 7 | |
| 8 | sub TIEHASH { |
| 9 | my $package = shift; |
| 10 | my $filename = shift; |
| 11 | my $truehash = (-e $filename) ? YAML::Any::LoadFile($filename) : {}; |
| 12 | my $self = {FILENAME => $filename, H => $truehash}; |
| 13 | bless $self => $package; |
| 14 | } |
| 15 | |
| 16 | sub STORE { |
| 17 | my $self = shift; |
| 18 | $self->{H}{$_[0]} = $_[1]; |
| 19 | } |
| 20 | |
| 21 | sub FETCH { |
| 22 | my $self = shift; |
| 23 | $self->{H}{$_[0]}; |
| 24 | } |
| 25 | |
| 26 | sub EXISTS { |
| 27 | my $self = shift; |
| 28 | exists $self->{H}{$_[0]}; |
| 29 | } |
| 30 | |
| 31 | sub DESTROY { |
| 32 | my $self = shift; |
| 33 | YAML::Any::DumpFile($self->{FILENAME}, $self->{H}); |
| 34 | } |
| 35 | |
| 36 | sub SCALAR { |
| 37 | my $self = shift; |
| 38 | scalar(%{$self->{H}}); |
| 39 | } |
| 40 | |
| 41 | sub FIRSTKEY { |
| 42 | 'Fake hash from Git::SVN::Memoize::YAML'; |
| 43 | } |
| 44 | |
| 45 | sub NEXTKEY { |
| 46 | undef; |
| 47 | } |
| 48 | |
| 49 | 1; |
| 50 | __END__ |
| 51 | |
| 52 | =head1 NAME |
| 53 | |
| 54 | Git::SVN::Memoize::YAML - store Memoized data in YAML format |
| 55 | |
| 56 | =head1 SYNOPSIS |
| 57 | |
| 58 | use Memoize; |
| 59 | use Git::SVN::Memoize::YAML; |
| 60 | |
| 61 | tie my %cache => 'Git::SVN::Memoize::YAML', $filename; |
| 62 | memoize('slow_function', SCALAR_CACHE => [HASH => \%cache]); |
| 63 | slow_function(arguments); |
| 64 | |
| 65 | =head1 DESCRIPTION |
| 66 | |
| 67 | This module provides a class that can be used to tie a hash to a |
| 68 | YAML file. The file is read when the hash is initialized and |
| 69 | rewritten when the hash is destroyed. |
| 70 | |
| 71 | The intent is to allow L<Memoize> to back its cache with a file in |
| 72 | YAML format, just like L<Memoize::Storable> allows L<Memoize> to |
| 73 | back its cache with a file in Storable format. Unlike the Storable |
| 74 | format, the YAML format is platform-independent and fairly stable. |
| 75 | |
| 76 | Carps on error. |
| 77 | |
| 78 | =head1 DIAGNOSTICS |
| 79 | |
| 80 | See L<YAML::Any>. |
| 81 | |
| 82 | =head1 DEPENDENCIES |
| 83 | |
| 84 | L<YAML::Any> from CPAN. |
| 85 | |
| 86 | =head1 INCOMPATIBILITIES |
| 87 | |
| 88 | None reported. |
| 89 | |
| 90 | =head1 BUGS |
| 91 | |
| 92 | The entire cache is read into a Perl hash when loading the file, |
| 93 | so this is not very scalable. |