• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

acdh-oeaw / arche-diss-cache / #57

24 Oct 2024 10:11PM UTC coverage: 80.841%. Remained the same
#57

push

php-coveralls

zozlak
Service::setCallback() introduced

6 of 6 new or added lines in 1 file covered. (100.0%)

173 of 214 relevant lines covered (80.84%)

3.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.94
/src/acdhOeaw/arche/lib/dissCache/ResponseCache.php
1
<?php
2

3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2024 zozlak.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26

27
namespace acdhOeaw\arche\lib\dissCache;
28

29
use DateTimeImmutable;
30
use acdhOeaw\arche\lib\exception\NotFound;
31
use acdhOeaw\arche\lib\SearchConfig;
32
use Psr\Log\LoggerInterface;
33

34
/**
35
 * Description of Cache
36
 *
37
 * @author zozlak
38
 */
39
class ResponseCache {
40

41
    private CacheInterface $cache;
42

43
    /**
44
     * 
45
     * @var array<RepoWrapperInterface>
46
     */
47
    private array $repos;
48

49
    /**
50
     * 
51
     * @var callable
52
     */
53
    private $missHandler;
54
    private int $ttlResource;
55
    private int $ttlResponse;
56
    private SearchConfig $searchCfg;
57
    private LoggerInterface | null $log;
58

59
    /**
60
     * 
61
     * @param callable $missHandler with the signature 
62
     *   `function(RepoResourceInterface $res): ResponseCacheItem`
63
     * @param array<RepoWrapperInterface> $repos
64
     */
65
    public function __construct(CacheInterface $cache, callable $missHandler,
66
                                int $ttlResource, int $ttlResponse,
67
                                array $repos, ?SearchConfig $config = null,
68
                                ?LoggerInterface $log = null) {
69
        $this->cache       = $cache;
5✔
70
        $this->missHandler = $missHandler;
5✔
71
        $this->ttlResource = $ttlResource;
5✔
72
        $this->ttlResponse = $ttlResponse;
5✔
73
        $this->repos       = $repos;
5✔
74
        $this->searchCfg   = $config ?? new SearchConfig();
5✔
75
        $this->log         = $log;
5✔
76
    }
77

78
    /**
79
     * 
80
     * @param string|object|array<mixed> $params
81
     */
82
    public function getResponse(string | object | array $params, string $resId): ResponseCacheItem {
83
        $now     = time();
5✔
84
        $respKey = $this->hashParams($params, $resId);
5✔
85
        $this->log?->info("Checking cache for resource $resId and parameters hash $respKey");
5✔
86

87
        // first check if the resource exists in cache
88
        // this is needed to check the TTL on the resource level
89
        $resItem      = $this->cache->get($resId);
5✔
90
        $matchingRepo = false;
5✔
91
        if ($resItem !== false) {
5✔
92
            $resCacheCreation = (new DateTimeImmutable($resItem->created))->getTimestamp();
4✔
93
            $diffRes          = $now - $resCacheCreation;
4✔
94
            $this->log?->debug("Resource found in cache (diffRes $diffRes, resTtl $this->ttlResource)");
4✔
95
            // if resource in cache is too old, check the resource's modification date at source
96
            if ($diffRes >= $this->ttlResource) {
4✔
97
                $resLastMod = PHP_INT_MAX;
3✔
98
                foreach ($this->repos as $repo) {
3✔
99
                    try {
100
                        $resLastMod = $repo->getModificationTimestamp($resId);
3✔
101
                        if ($resLastMod < PHP_INT_MAX) {
3✔
102
                            $matchingRepo = $repo;
3✔
103
                            break;
3✔
104
                        }
105
                    } catch (NotFound) {
×
106
                        
107
                    }
108
                }
109
                if ($resLastMod > $resCacheCreation) {
3✔
110
                    // invalidate resource cache
111
                    $this->log?->debug("Invalidating resource's cache (resLastMod - resCacheCreation = " . ($resLastMod - $resCacheCreation) . ")");
1✔
112
                    $resItem = false;
1✔
113
                } else {
114
                    $this->log?->debug("Keeping resource's cache (resLastMod - resCacheCreation = " . ($resLastMod - $resCacheCreation) . ")");
2✔
115
                }
116
            }
117
            if ($resItem !== false) {
4✔
118
                $respItem = $this->cache->get($respKey);
3✔
119
                $respDiff = $respItem !== false ? $now - (new DateTimeImmutable($respItem->created))->getTimestamp() : 'not in cache';
3✔
120
                if ($respItem !== false && $respDiff < $this->ttlResponse) {
3✔
121
                    $this->log?->info("Serving response from cache (respDiff $respDiff, respTtl $this->ttlResponse)");
2✔
122
                    return ResponseCacheItem::deserialize($respItem->value);
2✔
123
                } else {
124
                    $this->log?->info("Regenerating response (respDiff $respDiff, respTtl $this->ttlResponse)");
1✔
125
                }
126
            }
127
        }
128
        // must be separate if as code block above may invalidate $resItem
129
        if ($resItem) {
5✔
130
            $res = RepoResourceCacheItem::deserialize($resItem->value);
1✔
131
        } else {
132
            $this->log?->debug("Fetching the resource");
5✔
133
            $res   = false;
5✔
134
            $repos = $matchingRepo ? [$matchingRepo] : $this->repos;
5✔
135
            foreach ($repos as $repo) {
5✔
136
                try {
137
                    $res = $repo->getResourceById($resId, $this->searchCfg);
5✔
138
                    break;
5✔
139
                } catch (NotFound) {
×
140
                    
141
                }
142
            }
143
            if (!$res) {
5✔
144
                throw new NotFound("Resource $resId can not be found", 400);
×
145
            }
146
            $this->log?->debug("Caching the resource");
5✔
147
            $this->cache->set($res->getIds(), RepoResourceCacheItem::serialize($res), null);
5✔
148
        }
149
        // finally generate the response
150
        $this->log?->info("Generating the response");
5✔
151
        $value = ($this->missHandler)($res, $params);
5✔
152
        $this->log?->info("Caching the response");
5✔
153
        $this->cache->set([$respKey], $value->serialize(), null);
5✔
154

155
        return $value;
5✔
156
    }
157

158
    /**
159
     * Public only to simplify the testing.
160
     * 
161
     * @param string|object|array<mixed> $key
162
     * @return string
163
     */
164
    public function hashParams(string | object | array $key, string $resId): string {
165
        if (is_object($key)) {
5✔
166
            $key = get_object_vars($key);
×
167
        } elseif (!is_array($key)) {
5✔
168
            $key = [$key];
×
169
        }
170
        $key['__RESID__'] = $resId;
5✔
171
        ksort($key);
5✔
172
        $key              = (string) json_encode($key);
5✔
173
        $key              = hash('xxh128', $key);
5✔
174
        return $key;
5✔
175
    }
176
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc