Passed
Push — master ( 7de0d5...041459 )
by Pauli
13:50
created

LocalCacheTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 15
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A invalidateCache() 0 2 1
A cachedGet() 0 2 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2025
11
 */
12
13
namespace OCA\Music\Utility;
14
15
/**
16
 * @phpstan-template CachedType
17
 */
18
trait LocalCacheTrait {
19
20
	/** @phpstan-var array<string, array<string, CachedType>> $localCache */
21
	protected array $localCache = [];
22
23
	/**
24
	 * @phpstan-param callable():CachedType $createItem
25
	 * @phpstan-return CachedType
26
	 */
27
	protected function cachedGet(string $userId, ?string $key, callable $createItem) {
28
		return $this->localCache[$userId][$key] ?? $this->localCache[$userId][$key] = $createItem();
29
	}
30
31
	protected function invalidateCache(string $userId) : void {
32
		unset($this->localCache[$userId]);
33
	}
34
35
}