1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\DAV\Paginate;
use Generator;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IDBConnection;
use OCP\Security\ISecureRandom;
class PaginateCache {
public const TTL = 60 * 60;
private const CACHE_COUNT_SUFFIX = 'count';
private ICache $cache;
public function __construct(
private IDBConnection $database,
private ISecureRandom $random,
ICacheFactory $cacheFactory,
) {
$this->cache = $cacheFactory->createDistributed('pagination_');
}
/**
* @param string $uri
* @param \Iterator $items
* @return array{'token': string, 'count': int}
*/
public function store(string $uri, \Iterator $items): array {
$token = $this->random->generate(32);
$cacheKey = $this->buildCacheKey($uri, $token);
$count = 0;
foreach ($items as $item) {
// Add small margin to avoid fetching valid count and then expired entries
$this->cache->set($cacheKey . $count, $item, self::TTL + 60);
++$count;
}
$this->cache->set($cacheKey . self::CACHE_COUNT_SUFFIX, $count, self::TTL);
return ['token' => $token, 'count' => $count];
}
/**
* @return Generator<mixed>
*/
public function get(string $uri, string $token, int $offset, int $count): Generator {
$cacheKey = $this->buildCacheKey($uri, $token);
$nbItems = $this->cache->get($cacheKey . self::CACHE_COUNT_SUFFIX);
if (!$nbItems || $offset > $nbItems) {
return [];
}
$lastItem = min($nbItems, $offset + $count);
for ($i = $offset; $i < $lastItem; ++$i) {
yield $this->cache->get($cacheKey . $i);
}
}
public function exists(string $uri, string $token): bool {
return $this->cache->get($this->buildCacheKey($uri, $token) . self::CACHE_COUNT_SUFFIX) > 0;
}
private function buildCacheKey(string $uri, string $token): string {
return $token . '_' . crc32($uri) . '_';
}
}
|