| 1 | package PEF::Front::Cache; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use PEF::Front::Config; | ||
| 5 | use Data::Dumper; | ||
| 6 | |||
| 7 | use base 'Exporter'; | ||
| 8 | our @EXPORT; | ||
| 9 | |||
| 10 | my $dumper; | ||
| 11 | |||
| 12 | BEGIN { | ||
| 13 | @EXPORT = qw{ | ||
| 14 | get_cache | ||
| 15 | make_request_cache_key | ||
| 16 | remove_cache_key | ||
| 17 | set_cache | ||
| 18 | }; | ||
| 19 | $dumper = Data::Dumper->new([]); | ||
| 20 | $dumper->Indent(0); | ||
| 21 | $dumper->Pair(":"); | ||
| 22 | $dumper->Useqq(1); | ||
| 23 | $dumper->Terse(1); | ||
| 24 | $dumper->Deepcopy(1); | ||
| 25 | $dumper->Sortkeys(1); | ||
| 26 | my $module = cfg_cache_module(); | ||
| 27 | |||
| 28 | if ($module !~ /::/) { | ||
| 29 | $module = "PEF::Front::Cache::$module"; | ||
| 30 | } | ||
| 31 | my $module_file = $module; | ||
| 32 | $module_file =~ s|::|/|g; | ||
| 33 | $module_file .= ".pm"; | ||
| 34 | if (grep {/AppFrontConfig\.pm$/} keys %INC) { | ||
| 35 | eval {require $module_file}; | ||
| 36 | if ($@) { | ||
| 37 | die { | ||
| 38 | result => 'INTERR', | ||
| 39 | answer => 'Unknown cache provider $1', | ||
| 40 | answer_args => [$module] | ||
| 41 | }; | ||
| 42 | } | ||
| 43 | my $mp = __PACKAGE__; | ||
| 44 | for my $method (@EXPORT) { | ||
| 45 | my $cref = "$module"->can($method); | ||
| 46 | if ($cref) { | ||
| 47 | no strict 'refs'; | ||
| 48 | *{$mp . "::$method"} = $cref; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | sub make_request_cache_key { | ||
| 55 | my ($vreq, $cache_attr) = @_; | ||
| 56 | $cache_attr = {key => 'method', expires => $cache_attr} if not ref $cache_attr; | ||
| 57 | my @keys; | ||
| 58 | if (ref($cache_attr->{key}) eq 'ARRAY') { | ||
| 59 | @keys = grep {exists $vreq->{$_}} @{$cache_attr->{key}}; | ||
| 60 | } elsif (not exists $cache_attr->{key}) { | ||
| 61 | @keys = ('method'); | ||
| 62 | } else { | ||
| 63 | @keys = ($cache_attr->{key}); | ||
| 64 | } | ||
| 65 | $dumper->Values([{map {$_ => $vreq->{$_}} @keys}]); | ||
| 66 | return $dumper->Dump; | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | __END__ |