| 1 | package PEF::Front::Cache::FastMmap; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Cache::FastMmap; | ||
| 5 | use PEF::Front::Config; | ||
| 6 | use Time::Duration::Parse; | ||
| 7 | use Time::HiRes 'time'; | ||
| 8 | use Data::Dumper; | ||
| 9 | |||
| 10 | my $cache; | ||
| 11 | |||
| 12 | BEGIN { | ||
| 13 | # empty grep means there's no config loaded - some statical | ||
| 14 | # analyzing tools can break | ||
| 15 | if (grep { /AppFrontConfig\.pm$/ } keys %INC) { | ||
| 16 | $cache = Cache::FastMmap->new( | ||
| 17 | share_file => cfg_cache_file, | ||
| 18 | cache_size => cfg_cache_size, | ||
| 19 | empty_on_exit => 0, | ||
| 20 | unlink_on_exit => 0, | ||
| 21 | expire_time => 0, | ||
| 22 | init_file => 1 | ||
| 23 | ) or die "Can't create cache: $!"; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | sub get_cache { | ||
| 28 | my $key = $_[0]; | ||
| 29 | my $res = $cache->get($key); | ||
| 30 | if ($res) { | ||
| 31 | if ($res->[0] < time) { | ||
| 32 | $cache->remove($key); | ||
| 33 | return; | ||
| 34 | } | ||
| 35 | return $res->[1]; | ||
| 36 | } else { | ||
| 37 | return; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | sub set_cache { | ||
| 42 | my ($key, $obj, $expires) = @_; | ||
| 43 | my $seconds = parse_duration($expires) || 60; | ||
| 44 | $cache->set($key, [$seconds + time, $obj]); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub remove_cache_key { | ||
| 48 | my $key = $_[0]; | ||
| 49 | $cache->remove($key); | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | 1; | ||
| 54 |