| 1 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | package FusqlFS::Cache; | ||
| 5 | 1 | ▶ | use FusqlFS::Version; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | our $VERSION = $FusqlFS::Version::VERSION; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | FusqlFS::Cache - main FusqlFS cache factory | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use FusqlFS::Cache; | ||
| 15 | |||
| 16 | our %cache; | ||
| 17 | my $cache = FusqlFS::Cache->init(\%cache, 'limited', 10); | ||
| 18 | # tied(%cache) == $cache | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | This is a cache subsystem initialization class. It is an abstract factory | ||
| 23 | class, just like L<FusqlFS::Backend>, and thus it can't be instantiated | ||
| 24 | directly. | ||
| 25 | |||
| 26 | Its single method C<init()> accepts hashref as first argument, cache strategy | ||
| 27 | as second argument and cache threshold as third argument, and returns | ||
| 28 | L<FusqlFS::Cache::Base> subclass instance or undef, if cache is done directly | ||
| 29 | in memory without any complex cache logic layers. | ||
| 30 | |||
| 31 | FusqlFS cache is actually a simple hash, so all cache strategies are | ||
| 32 | implemented as hash tie()-able classes. See L<FusqlFS::Cache::Base> and its | ||
| 33 | subclasses on details of cache strategies implementation. | ||
| 34 | |||
| 35 | All this concrete class does (with its single C<init()> method) is just | ||
| 36 | verifies cache threshold, recognizes cache strategy by name and tie()s cache | ||
| 37 | hash given by hashref to cache class corresponding chosen cache strategy. | ||
| 38 | |||
| 39 | Special `memory' strategy means cache is done directly in memory and thus no | ||
| 40 | tie()ing is needed, and so C<init()> returns undef as no tied class is | ||
| 41 | instantiated. This strategy is also chosen in case of any errors, so if cache | ||
| 42 | strategy is not defined or not recognized or cache threshold have no sense for | ||
| 43 | chosen cache strategy, `memory' strategy is chosen, debug message is displayed | ||
| 44 | and undef is returned, leaving cache hash untied. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 49 | |||
| 50 | 0 | ▶ | use FusqlFS::Cache::Limited; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 51 | use FusqlFS::Cache::File; | ||
| 52 | |||
| 53 | =begin testing init | ||
| 54 | |||
| 55 | #!noinst | ||
| 56 | |||
| 57 | foreach (qw(Limited File)) | ||
| 58 | { | ||
| 59 | my %cache; | ||
| 60 | ok !FusqlFS::Cache->init(\%cache, lc $_, 0), $_.' cache strategy not chosen'; | ||
| 61 | ok !tied(%cache), $_.' cache handler is untied'; | ||
| 62 | |||
| 63 | isa_ok FusqlFS::Cache->init(\%cache, lc $_, 10), 'FusqlFS::Cache::'.$_, $_.' cache strategy chosen'; | ||
| 64 | isa_ok tied(%cache), 'FusqlFS::Cache::'.$_, $_.' cache handler tied'; | ||
| 65 | } | ||
| 66 | |||
| 67 | my %cache; | ||
| 68 | ok !FusqlFS::Cache->init(\%cache, 'memory'), 'Memory cache strategy chosen'; | ||
| 69 | ok !FusqlFS::Cache->init(\%cache, 'xxxxxx'), 'Memory cache strategy chosen (fallback 1)'; | ||
| 70 | ok !FusqlFS::Cache->init(\%cache), 'Memory cache strategy chosen (fallback 2)'; | ||
| 71 | ok !tied(%cache), 'Memory cache handler is untied'; | ||
| 72 | |||
| 73 | =end testing | ||
| 74 | =cut | ||
| 75 | sub init | ||
| 76 | { | ||
| 77 | my $class = shift; | ||
| 78 | my $hash = shift; | ||
| 79 | my $strategy = shift || 'memory'; | ||
| 80 | my $subclass = ''; | ||
| 81 | |||
| 82 | given ($strategy) | ||
| 83 | { | ||
| 84 | when ('memory') { } | ||
| 85 | when ('limited') { $subclass = '::Limited' } | ||
| 86 | when ('file') { $subclass = '::File' } | ||
| 87 | default | ||
| 88 | { | ||
| 89 | #carp "Unknown cache strategy `$strategy', using default `memory' strategy"; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | return unless $subclass; | ||
| 94 | |||
| 95 | $class .= $subclass; | ||
| 96 | unless ($class->is_needed(@_)) | ||
| 97 | { | ||
| 98 | #carp "Given parameters don't match `$strategy' cache strategy, falling back to `memory' strategy"; | ||
| 99 | return; | ||
| 100 | } | ||
| 101 | |||
| 102 | tie %$hash, $class, @_; | ||
| 103 | } | ||
| 104 | |||
| 105 | 1; | ||