| 1 | # Copyright (C) 2017 Koha-Suomi | ||
| 2 | # | ||
| 3 | # This file is part of Pootle-Client. | ||
| 4 | |||
| 5 | package Pootle::Cache; | ||
| 6 | |||
| 7 | use Modern::Perl '2015'; | ||
| 8 | use utf8; | ||
| 9 | binmode STDOUT, ':encoding(UTF-8)'; | ||
| 10 | binmode STDERR, ':encoding(UTF-8)'; | ||
| 11 | use feature 'signatures'; no warnings "experimental::signatures"; | ||
| 12 | use Carp::Always; | ||
| 13 | use Try::Tiny; | ||
| 14 | use Scalar::Util qw(blessed); | ||
| 15 | use Data::Dumper; | ||
| 16 | use Cwd; | ||
| 17 | |||
| 18 | =head1 Pootle::Cache | ||
| 19 | |||
| 20 | Persist API results somewhere to prevent excessive spamming of the Pootle-Client. | ||
| 21 | These changes might persist over the current process exiting. | ||
| 22 | |||
| 23 | =head2 Transient cache | ||
| 24 | |||
| 25 | Pootle::Cache has a transient portion, values stored here disappear after the program exits. | ||
| 26 | You can access this cache with methods tGet, tPut, t*, ... | ||
| 27 | If you want to refresh the results of methods, which have the | ||
| 28 | "@CACHED Transiently" -tag | ||
| 29 | you must flush the transient cache using: | ||
| 30 | $cache->tFlush(); | ||
| 31 | or restart the program. | ||
| 32 | |||
| 33 | =head2 Persistent cache | ||
| 34 | |||
| 35 | Pootle::Cache has a persistent portion, values stored here will be flushed to the on-disk cache file, if the program exits normally. | ||
| 36 | You can access this cache with methods pGet, pPut, p*, ... | ||
| 37 | If you want to refresh the results of methods, which have the | ||
| 38 | "@CACHED Persistently" -tag | ||
| 39 | you must flush the persistent cache using: | ||
| 40 | $cache->pFlush(); | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | use Params::Validate qw(:all); | ||
| 45 | use File::Slurp; | ||
| 46 | |||
| 47 | use Pootle::Logger; | ||
| 48 | my $l = bless({}, 'Pootle::Logger'); #Lazy load package logger this way to avoid circular dependency issues with logger includes from many packages | ||
| 49 | |||
| 50 | =head2 new | ||
| 51 | |||
| 52 | my $c = new Pootle::Cache({ | ||
| 53 | cacheFile => 'pootle-client.cache', #Where the persistent cache should be saved? | ||
| 54 | }); | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub new($class, @params) { | ||
| 59 | $l->debug("Initializing '$class' with parameters: ".$l->flatten(@params)) if $l->is_debug(); | ||
| 60 | my %self = validate(@params, { | ||
| 61 | cacheFile => { default => 'pootle-client.cache'}, | ||
| 62 | }); | ||
| 63 | my $s = bless(\%self, $class); | ||
| 64 | |||
| 65 | $s->{cacheFile} = 'pootle-client.cache' unless $s->{cacheFile}; #Params::Validate doesn't set the default value if key exists without value | ||
| 66 | |||
| 67 | $s->loadCache(); | ||
| 68 | |||
| 69 | return $s; | ||
| 70 | } | ||
| 71 | |||
| 72 | =head2 loadCache | ||
| 73 | |||
| 74 | Loads cache from disk, if cache is not present, tests for file permissions to persist one. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub loadCache($s) { | ||
| 79 | |||
| 80 | my $cache = "{}"; | ||
| 81 | try { | ||
| 82 | $cache = File::Slurp::read_file($s->cacheFile, { binmode => ':encoding(UTF-8)' }); | ||
| 83 | } catch { my $e = $_; | ||
| 84 | if ($e =~ /sysopen: No such file or direc/) { | ||
| 85 | open(my $FH, '>:encoding(UTF-8)', $s->cacheFile) or $l->logdie($s->toString()." Couldn't initialize cache file, Cwd=".Cwd::getcwd.", $!"); | ||
| 86 | } | ||
| 87 | else { | ||
| 88 | die $e; | ||
| 89 | } | ||
| 90 | }; | ||
| 91 | |||
| 92 | $s->{pCache} = _evalCacheContents($cache); | ||
| 93 | unless ($s->pCache) { | ||
| 94 | $l->warn("Loaded cache contents are undefined"); | ||
| 95 | $s->{pCache} = {}; | ||
| 96 | } | ||
| 97 | $s->{tCache} = {}; | ||
| 98 | } | ||
| 99 | |||
| 100 | =head2 _evalCacheContents | ||
| 101 | @STATIC | ||
| 102 | |||
| 103 | Turn the raw cache contents into a perl data structure | ||
| 104 | |||
| 105 | @PARAM1 String, raw cache contents | ||
| 106 | @RETURNS HASHRef | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | sub _evalCacheContents($contents) { | ||
| 111 | return eval "$contents"; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub saveCache($s) { | ||
| 115 | $l->debug($s->toString()." is being persisted") if $l->is_debug; | ||
| 116 | open(my $FH, '>:encoding(UTF-8)', $s->cacheFile) or $l->logdie($s->toString()." failed to persist to file. Cwd=".Cwd::getcwd.", $!"); | ||
| 117 | print $FH Data::Dumper->new([$s->pCache],[])->Terse(1)->Indent(1)->Varname('')->Maxdepth(0)->Sortkeys(1)->Quotekeys(1)->Dump(); | ||
| 118 | close($FH); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub flushCaches($s) { | ||
| 122 | $s->tFlush(); | ||
| 123 | $s->pFlush(); | ||
| 124 | } | ||
| 125 | |||
| 126 | =head2 tSet | ||
| 127 | |||
| 128 | Store a value to the transient in-memory store. This will never be flushed to disk. | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub tSet($s, $k, $v) { | ||
| 133 | return $s->tCache->{$k} = $v; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub tGet($s, $k) { | ||
| 137 | return $s->tCache->{$k}; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub tFlush($s) { | ||
| 141 | $s->{tCache} = {}; | ||
| 142 | } | ||
| 143 | |||
| 144 | =head2 pSet | ||
| 145 | |||
| 146 | Store a value to the persistent in-memory store | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub pSet($s, $k, $v) { | ||
| 151 | return $s->pCache->{$k} = $v; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub pGet($s, $k) { | ||
| 155 | return $s->pCache->{$k}; | ||
| 156 | } | ||
| 157 | |||
| 158 | =head pFlush | ||
| 159 | |||
| 160 | Flushes the persistent cache from disk, forcing the Pootle::Client to fetch new values from the Pootle Server's API on subsequent API requests. | ||
| 161 | |||
| 162 | @RETURNS whatever unlink returns, 0 on success, 1 on failure atleast. unlink docs don't say. | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | sub pFlush($s) { | ||
| 167 | $l->debug($s->toString()." is being flushed") if $l->is_debug; | ||
| 168 | $s->{pCache} = {}; | ||
| 169 | my $rv = unlink $s->cacheFile; | ||
| 170 | $l->error($s->toString()." couldn't be flushed: $!") if $rv; | ||
| 171 | return $rv; | ||
| 172 | } | ||
| 173 | |||
| 174 | =head2 toString | ||
| 175 | |||
| 176 | my $pc = Pootle::Cache->new(); | ||
| 177 | print "Cuddling with Cache ".$pc." tonight\n"; | ||
| 178 | |||
| 179 | Serialize this Cache as a simple one-liner keypoint description of it's internal state | ||
| 180 | |||
| 181 | @RETURNS String | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | sub toString($s) { | ||
| 186 | return $s.' cacheFile='.$s->cacheFile.', pCache buckets='.%{$s->pCache}.', tCache buckets='.%{$s->tCache}; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub DESTROY($s) { | ||
| 190 | $l->debug($s->toString()." is getting destroyed") if $l->is_debug(); | ||
| 191 | |||
| 192 | eval { $s->saveCache(); }; | ||
| 193 | if ($@) { | ||
| 194 | $l->warn($@); | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | sub cacheFile($s) { return $s->{cacheFile} } | ||
| 199 | sub pCache($s) { return $s->{pCache} } | ||
| 200 | sub tCache($s) { return $s->{tCache} } | ||
| 201 | |||
| 202 | =head2 Accessors | ||
| 203 | |||
| 204 | =over 4 | ||
| 205 | |||
| 206 | =item B<cacheFile> | ||
| 207 | |||
| 208 | =item B<pCache> | ||
| 209 | |||
| 210 | =item B<tCache> | ||
| 211 | |||
| 212 | =cut | ||
| 213 | |||
| 214 | 1; |