|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silviooosilva\CacheerPhp\CacheStore\Support; |
|
4
|
|
|
|
|
5
|
|
|
use Silviooosilva\CacheerPhp\CacheStore\FileCacheStore; |
|
6
|
|
|
use Silviooosilva\CacheerPhp\Exceptions\CacheFileException; |
|
7
|
|
|
use Silviooosilva\CacheerPhp\Helpers\CacheFileHelper; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class FileCacheBatchProcessor |
|
11
|
|
|
* @author Sílvio Silva <https://github.com/silviooosilva> |
|
12
|
|
|
* @package Silviooosilva\CacheerPhp |
|
13
|
|
|
*/ |
|
14
|
|
|
class FileCacheBatchProcessor |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* FileCacheBatchProcessor constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* @param FileCacheStore $store |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(private FileCacheStore $store) |
|
23
|
|
|
{ |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Processes a batch of cache items and stores them. |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $batchItems |
|
30
|
|
|
* @param string $namespace |
|
31
|
|
|
* @return void |
|
32
|
|
|
* @throws CacheFileException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function process(array $batchItems, string $namespace): void |
|
35
|
|
|
{ |
|
36
|
|
|
foreach ($batchItems as $item) { |
|
37
|
|
|
CacheFileHelper::validateCacheItem($item); |
|
38
|
|
|
$cacheKey = $item['cacheKey']; |
|
39
|
|
|
$cacheData = $item['cacheData']; |
|
40
|
|
|
$mergedData = CacheFileHelper::mergeCacheData($cacheData); |
|
41
|
|
|
$this->store->putCache($cacheKey, $mergedData, $namespace); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Processes items in batches and stores them. |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $items |
|
49
|
|
|
* @param string $namespace |
|
50
|
|
|
* @param int $batchSize |
|
51
|
|
|
* @return void |
|
52
|
|
|
* @throws CacheFileException |
|
53
|
|
|
*/ |
|
54
|
|
|
public function processBatches(array $items, string $namespace, int $batchSize = 100): void |
|
55
|
|
|
{ |
|
56
|
|
|
$processedCount = 0; |
|
57
|
|
|
$itemCount = count($items); |
|
58
|
|
|
|
|
59
|
|
|
while ($processedCount < $itemCount) { |
|
60
|
|
|
$batchItems = array_slice($items, $processedCount, $batchSize); |
|
61
|
|
|
$this->process($batchItems, $namespace); |
|
62
|
|
|
$processedCount += count($batchItems); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|