1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kemist\Cache\Storage; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* AbstractStorage class |
7
|
|
|
* |
8
|
|
|
* @package Kemist\Cache |
9
|
|
|
* |
10
|
|
|
* @version 1.0.6 |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractStorage { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Cached field names |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $fields = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Number of hits |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
protected $hits = 0; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Number of misses |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
protected $misses = 0; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Cache provider Object |
35
|
|
|
* @var object |
36
|
|
|
*/ |
37
|
|
|
protected $provider; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Info method |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $infoMethod = 'info'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Key prefix to avoid collisions |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $prefix = ''; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Retrieves the content of $name cache |
53
|
|
|
* |
54
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
55
|
|
|
* @param string $name cache name |
56
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
|
public function get($name, $compressed = false) { |
60
|
|
|
$value = $this->provider->get($this->prefix . $name); |
61
|
|
|
if ($value !== false) { |
62
|
|
|
$this->hit(); |
63
|
|
|
$this->storeName($name); |
64
|
|
|
} else { |
65
|
|
|
$this->miss(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $value; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Cache miss occured |
73
|
|
|
*/ |
74
|
|
|
public function miss() { |
75
|
|
|
$this->misses++; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Cache hit occured |
80
|
|
|
*/ |
81
|
|
|
public function hit() { |
82
|
|
|
$this->hits++; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Deletes the specified cache or each one if '' given |
87
|
|
|
* |
88
|
|
|
* @param string $name cache name |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function delete($name = '') { |
93
|
|
|
if ($name == '') { |
94
|
|
|
return $this->provider->flush(); |
95
|
|
|
} else { |
96
|
|
|
return $this->provider->delete($this->prefix . $name); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Retrieves information of Cache state |
102
|
|
|
* |
103
|
|
|
* @param bool $getFields |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function info($getFields = false) { |
108
|
|
|
$info = array(); |
109
|
|
|
$className = explode('\\', get_class($this)); |
110
|
|
|
$info['CACHE_TYPE'] = end($className); |
111
|
|
|
$info['CACHE_HITS'] = $this->hits; |
112
|
|
|
$info['CACHE_MISSES'] = $this->misses; |
113
|
|
|
|
114
|
|
|
$info = array_merge($info, call_user_func(array($this->provider, $this->infoMethod))); |
115
|
|
|
|
116
|
|
|
if ($getFields) { |
117
|
|
|
foreach ($this->fields as $field) { |
118
|
|
|
$info['field_content'][$field] = $this->get($field); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $info; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Retrieves cache hits |
127
|
|
|
* |
128
|
|
|
* @return int |
129
|
|
|
*/ |
130
|
|
|
public function getHits() { |
131
|
|
|
return $this->hits; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Retrieves cache misses |
136
|
|
|
* |
137
|
|
|
* @return int |
138
|
|
|
*/ |
139
|
|
|
public function getMisses() { |
140
|
|
|
return $this->misses; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Stores cache name |
145
|
|
|
* |
146
|
|
|
* @param string $name |
147
|
|
|
*/ |
148
|
|
|
protected function storeName($name) { |
149
|
|
|
if (!in_array($name, $this->fields)) { |
150
|
|
|
$this->fields[] = $name; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|