Passed
Push — master ( fd0d63...16794a )
by smiley
01:53
created

MemoryCache::get()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 2
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
class MemoryCache extends CacheDriverAbstract{
16
17
	/**
18
	 * @var array
19
	 */
20
	protected $cache = [];
21
22
	/** @inheritdoc */
23
	public function get($key, $default = null){
24
		$this->checkKey($key);
25
26
		if(isset($this->cache[$key])){
27
28
			if($this->cache[$key]['ttl'] === null || $this->cache[$key]['ttl'] > time()){
29
				return $this->cache[$key]['content'];
30
			}
31
32
			unset($this->cache[$key]);
33
		}
34
35
		return $default;
36
	}
37
38
	/** @inheritdoc */
39
	public function set($key, $value, $ttl = null):bool{
40
		$this->checkKey($key);
41
42
		$ttl = $this->getTTL($ttl);
43
44
		$this->cache[$key] = [
45
			'ttl'     => $ttl ? time() + $ttl : null,
46
			'content' => $value,
47
		];
48
49
		return true;
50
	}
51
52
	/** @inheritdoc */
53
	public function delete($key):bool{
54
		$this->checkKey($key);
55
56
		unset($this->cache[$key]);
57
58
		return true;
59
	}
60
61
	/** @inheritdoc */
62
	public function clear():bool{
63
		$this->cache = [];
64
65
		return true;
66
	}
67
68
}
69