-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRAM.php
More file actions
72 lines (66 loc) · 1.53 KB
/
Copy pathRAM.php
File metadata and controls
72 lines (66 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace Justuno\Core;
# 2020-06-13 "Port the `Df\Core\RAM` class": https://github.com/justuno-com/core/issues/9
final class RAM {
/**
* 2020-06-13
* The following code will return `1`:
* $a = ['a' => null];
* echo intval(array_key_exists('a', $a));
* https://3v4l.org/9cQOO
* @used-by jucf()
* @used-by get()
*/
function exists(string $k):bool {return array_key_exists($k, $this->_data);}
/**
* 2020-06-13
* @used-by jucf()
* @return mixed
*/
function get(string $k) {return $this->exists($k) ? $this->_data[$k] : null;}
/**
* 2020-06-13
*/
function reset() {$this->_data = []; $this->_tags = [];}
/**
* 2020-06-13
* @used-by jucf()
* @param mixed $v
* @param string[] $tags [optional]
* @return mixed
*/
function set(string $k, $v, array $tags = []) {
if ($v instanceof ICached) {
$tags += $v->tags();
}
$this->_data[$k] = $v;
foreach ($tags as $tag) { /** @var string $tag */
if (!isset($this->_tags[$tag])) {
$this->_tags[$tag] = [$k];
}
elseif (!in_array($k, $this->_tags[$tag])) {
$this->_tags[$tag][] = $k;
}
}
return $v;
}
/**
* 2017-08-10
* @used-by self::exists()
* @used-by self::get()
* @used-by self::set()
* @var array(string => mixed) «Cache Key => Cached Data»
*/
private $_data = [];
/**
* 2017-08-10
* @used-by self::set()
* @var array(string => string[]) «Tag ID => Cache Keys»
*/
private $_tags = [];
/**
* 2017-08-10
* @used-by ju_ram()
*/
static function s():self {static $r; return $r ? $r : $r = new self;}
}