cache::get()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 2
nop 2
1
<?php
2
/**
3
 * Very simple in-memory caching
4
 */
5
6
namespace projectcleverweb\color\data;
7
8
/**
9
 * Very simple in-memory caching
10
 */
11
class cache {
12
	
13
	/**
14
	 * Whether or not caching is active
15
	 * @var bool
16
	 */
17
	public $active;
18
	
19
	/**
20
	 * The cache data
21
	 * @var array
22
	 */
23
	protected $data;
24
	
25
	/**
26
	 * Initialize this class
27
	 */
28
	public function __construct() {
29
		$this->reset();
30
		$this->active = TRUE;
31
	}
32
	
33
	/**
34
	 * Reset the entire cache
35
	 * 
36
	 * @return void
37
	 */
38
	public function reset() {
39
		$this->data = [];
40
	}
41
	
42
	/**
43
	 * Store a value in the cache
44
	 * 
45
	 * @param string $func  The function trying to store data (should be __FUNCTION__)
46
	 * @param string $id    The storage ID
47
	 * @param mixed  $value The value to store
48
	 */
49
	public function set(string $func, string $id, $value) {
50
		if ($this->active) {
51
			$this->data[$func] = [$id => $value];
52
		}
53
	}
54
	
55
	/**
56
	 * Get a value from the cache
57
	 * 
58
	 * @param  string $func The function trying to get data (should be __FUNCTION__)
59
	 * @param  string $id   The storage ID
60
	 * @return mixed        The value stored or NULL
61
	 */
62
	public function get(string $func, string $id) {
63
		if ($this->active && isset($this->data[$func][$id])) {
64
			return $this->data[$func][$id];
65
		}
66
	}
67
}
68