1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\Cache\Engine; |
4
|
|
|
|
5
|
|
|
use ByJG\Cache\CacheEngineInterface; |
6
|
|
|
use Psr\Log\NullLogger; |
7
|
|
|
|
8
|
|
|
class ArrayCacheEngine implements CacheEngineInterface |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
protected $_L1Cache = array(); |
12
|
|
|
|
13
|
|
|
protected $logger = null; |
14
|
|
|
|
15
|
|
|
public function __construct($logger = null) |
16
|
|
|
{ |
17
|
|
|
$this->logger = $logger; |
18
|
|
|
if (is_null($logger)) { |
19
|
|
|
$this->logger = new NullLogger(); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $key The object KEY |
25
|
|
|
* @param int $ttl IGNORED IN MEMCACHED. |
26
|
|
|
* @return object Description |
27
|
|
|
*/ |
28
|
|
|
public function get($key, $ttl = 0) |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
if (array_key_exists($key, $this->_L1Cache)) { |
33
|
|
|
$this->logger->info("[Array cache] Get '$key' from L1 Cache"); |
34
|
|
|
return $this->_L1Cache[$key]; |
35
|
|
|
} else { |
36
|
|
|
$this->logger->info("[Array cache] Not found '$key'"); |
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $key The object Key |
43
|
|
|
* @param object $object The object to be cached |
44
|
|
|
* @param int $ttl The time to live in seconds of this objects |
45
|
|
|
* @return bool If the object is successfully posted |
46
|
|
|
*/ |
47
|
|
|
public function set($key, $object, $ttl = 0) |
48
|
|
|
{ |
49
|
|
|
$this->logger->info("[Array cache] Set '$key' in L1 Cache"); |
50
|
|
|
|
51
|
|
|
$this->_L1Cache[$key] = $object; |
52
|
|
|
|
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* |
58
|
|
|
* @param string $key |
59
|
|
|
* @param string $str |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function append($key, $str) |
63
|
|
|
{ |
64
|
|
|
$this->logger->info("[Array cache] Append '$key' in L1 Cache"); |
65
|
|
|
|
66
|
|
|
$this->_L1Cache[$key] = $this->_L1Cache[$key] . $str; |
67
|
|
|
|
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Unlock resource |
73
|
|
|
* @param string $key |
74
|
|
|
*/ |
75
|
|
|
public function release($key) |
76
|
|
|
{ |
77
|
|
|
unset($this->_L1Cache[$key]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Lock resource before set it. |
82
|
|
|
* @param string $key |
83
|
|
|
*/ |
84
|
|
|
public function lock($key) |
85
|
|
|
{ |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* UnLock resource after set it |
91
|
|
|
* @param string $key |
92
|
|
|
*/ |
93
|
|
|
public function unlock($key) |
94
|
|
|
{ |
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function isAvailable() |
99
|
|
|
{ |
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|