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

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

11 Oct 2024 06:34PM UTC coverage: 81.41%. Remained the same
#7

push

php-coveralls

zozlak
Provide tests and fixes for the ResourceCache::getResponse()

50 of 74 new or added lines in 3 files covered. (67.57%)

2 existing lines in 1 file now uncovered.

127 of 156 relevant lines covered (81.41%)

3.47 hits per line

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

93.22
/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
     */
64
    public function __construct(CacheInterface $cache, callable $missHandler,
65
                                int $ttlResource, int $ttlResponse,
66
                                array $repos, ?SearchConfig $config = null,
67
                                ?LoggerInterface $log = null) {
68
        $this->cache       = $cache;
4✔
69
        $this->missHandler = $missHandler;
4✔
70
        $this->ttlResource = $ttlResource;
4✔
71
        $this->ttlResponse = $ttlResponse;
4✔
72
        $this->repos       = $repos;
4✔
73
        $this->searchCfg   = $config ?? new SearchConfig();
4✔
74
        $this->log         = $log;
4✔
75
    }
76

77
    public function getResponse(string | object | array $params, string $resId): ResponseCacheItem {
78
        $now     = time();
4✔
79
        $respKey = $this->hashParams($params);
4✔
80
        $this->log?->info("Checking cache for resource $resId and parameters hash $respKey");
4✔
81

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

149
        return $value;
4✔
150
    }
151

152
    /**
153
     * Public only to simplify the testing.
154
     * 
155
     * @param string|object|array $key
156
     * @return string
157
     */
158
    public function hashParams(string | object | array $key): string {
159
        if (is_object($key)) {
4✔
UNCOV
160
            $key = get_object_vars($key);
×
161
        }
162
        if (is_array($key)) {
4✔
163
            ksort($key);
4✔
164
            $key = json_encode($key);
4✔
165
            $key = hash('xxh128', $key);
4✔
166
        }
167
        return $key;
4✔
168
    }
169
}
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