File Coverage

blib/lib/WebService/GrowthBook/InMemoryFeatureCache.pm
Criterion Covered Total %
statement 48 48 100.0
branch 6 6 100.0
condition n/a
subroutine 11 11 100.0
pod 0 4 0.0
total 65 69 94.2


line stmt bran cond sub pod time code
1             package WebService::GrowthBook::InMemoryFeatureCache;
2 7     7   381296 use strict;
  7         16  
  7         399  
3 7     7   67 use warnings;
  7         16  
  7         570  
4 7     7   1204 no indirect;
  7         3377  
  7         63  
5 7     7   651 use feature qw(state);
  7         18  
  7         1299  
6 7     7   1642 use Object::Pad;
  7         30419  
  7         63  
7 7     7   5872 use WebService::GrowthBook::CacheEntry;
  7         44  
  7         1043  
8              
9             our $VERSION = '0.003'; ## VERSION
10              
11 7     7   4171 class WebService::GrowthBook::InMemoryFeatureCache :isa(WebService::GrowthBook::AbstractFeatureCache) {
  7         22  
  7         462  
12             field %cache;
13              
14 12     12 0 59 method get($key){
  12         39  
  12         28  
  12         23  
15 12         31 my $entry = $cache{$key};
16 12 100       115 return undef unless $entry;
17 4 100       19 return $entry->value if !$entry->expired;
18 1         13 return undef;
19             }
20              
21 4     4 0 17 method set($key, $value, $ttl){
  4         19  
  4         10  
  4         12  
  4         11  
  4         9  
22 4 100       22 if(exists $cache{$key}){
23 1         10 $cache{$key}->update($value);
24 1         8 return;
25             }
26 3         63 $cache{$key} = WebService::GrowthBook::CacheEntry->new(value => $value, ttl => $ttl);
27             }
28              
29 1     1 0 4 method clear(){
  1         5  
  1         2  
30 1         12 %cache = ();
31             }
32              
33 104     104 0 232 sub singleton($class){
  104         234  
  104         6606  
34 104         257 state $instance = $class->new();
35 104         308 return $instance;
36             }
37             }