MemoryCache::set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
/**
3
 * Class MemoryCache
4
 *
5
 * @filesource   MemoryCache.php
6
 * @created      27.05.2017
7
 * @package      chillerlan\SimpleCache
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\SimpleCache;
14
15
use function time;
16
17
class MemoryCache extends CacheDriverAbstract{
18
19
	protected array $cache = [];
20
21
	/** @inheritdoc */
22
	public function get($key, $default = null){
23
		$key = $this->checkKey($key);
24
25
		if(isset($this->cache[$key])){
26
27
			if($this->cache[$key]['ttl'] === null || $this->cache[$key]['ttl'] > time()){
28
				return $this->cache[$key]['content'];
29
			}
30
31
			unset($this->cache[$key]);
32
		}
33
34
		return $default;
35
	}
36
37
	/** @inheritdoc */
38
	public function set($key, $value, $ttl = null):bool{
39
		$ttl = $this->getTTL($ttl);
40
41
		$this->cache[$this->checkKey($key)] = [
42
			'ttl'     => $ttl ? time() + $ttl : null,
43
			'content' => $value,
44
		];
45
46
		return true;
47
	}
48
49
	/** @inheritdoc */
50
	public function delete($key):bool{
51
		unset($this->cache[$this->checkKey($key)]);
52
53
		return true;
54
	}
55
56
	/** @inheritdoc */
57
	public function clear():bool{
58
		$this->cache = [];
59
60
		return true;
61
	}
62
63
}
64