Passed
Push — master ( a2422c...364e76 )
by Christoph
23:57 queued 10:12
created

WithLocalCache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace OC\Memcache;
4
5
use OCP\Cache\CappedMemoryCache;
6
use OCP\ICache;
7
8
/**
9
 * Wrap a cache instance with an extra later of local, in-memory caching
10
 */
11
class WithLocalCache implements ICache {
12
	private ICache $inner;
13
	private CappedMemoryCache $cached;
14
15
	public function __construct(ICache $inner, int $localCapacity = 512) {
16
		$this->inner = $inner;
17
		$this->cached = new CappedMemoryCache($localCapacity);
18
	}
19
20
	public function get($key) {
21
		if (isset($this->cached[$key])) {
22
			return $this->cached[$key];
23
		} else {
24
			$value = $this->inner->get($key);
25
			if (!is_null($value)) {
26
				$this->cached[$key] = $value;
27
			}
28
			return $value;
29
		}
30
	}
31
32
	public function set($key, $value, $ttl = 0) {
33
		$this->cached[$key] = $value;
34
		return $this->inner->set($key, $value, $ttl);
35
	}
36
37
	public function hasKey($key) {
38
		return isset($this->cached[$key]) || $this->inner->hasKey($key);
39
	}
40
41
	public function remove($key) {
42
		unset($this->cached[$key]);
43
		return $this->inner->remove($key);
44
	}
45
46
	public function clear($prefix = '') {
47
		$this->cached->clear();
48
		return $this->inner->clear($prefix);
49
	}
50
51
	public static function isAvailable(): bool {
52
		return false;
53
	}
54
}
55