1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* KumbiaPHP web & app Framework |
4
|
|
|
* |
5
|
|
|
* LICENSE |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the new BSD license that is bundled |
8
|
|
|
* with this package in the file LICENSE. |
9
|
|
|
* |
10
|
|
|
* @category Kumbia |
11
|
|
|
* @package Cache |
12
|
|
|
* |
13
|
|
|
* @copyright Copyright (c) 2005 - 2023 KumbiaPHP Team (http://www.kumbiaphp.com) |
14
|
|
|
* @license https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE New BSD License |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Clase base para componentes de cacheo |
19
|
|
|
* |
20
|
|
|
* @category Kumbia |
21
|
|
|
* @package Cache |
22
|
|
|
*/ |
23
|
|
|
abstract class Cache |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Pool de drivers para cache |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
* */ |
31
|
|
|
protected static $_drivers = []; |
32
|
|
|
/** |
33
|
|
|
* Driver por defecto |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
* */ |
37
|
|
|
protected static $_default_driver = 'file'; |
38
|
|
|
/** |
39
|
|
|
* Id de ultimo elemento solicitado |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $_id; |
44
|
|
|
/** |
45
|
|
|
* Grupo de ultimo elemento solicitado |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $_group = 'default'; |
50
|
|
|
/** |
51
|
|
|
* Tiempo de vida |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $_lifetime = ''; |
56
|
|
|
/** |
57
|
|
|
* Start - end data |
58
|
|
|
* |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
protected $_start = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Carga un elemento cacheado |
65
|
|
|
* |
66
|
|
|
* @param string $id identificador |
67
|
|
|
* @param string $group grupo |
68
|
|
|
* @return string|null |
69
|
|
|
*/ |
70
|
|
|
abstract public function get($id, $group = 'default'); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Guarda un elemento en la cache con nombre $id y valor $value |
74
|
|
|
* |
75
|
|
|
* @param string $value Contenido a cachear |
76
|
|
|
* @param string $lifetime Tiempo de vida con formato strtotime, utilizado para cache |
77
|
|
|
* @param string|null $id Identificador |
78
|
|
|
* @param string $group Grupo, se crea la cache con $group.$id |
79
|
|
|
* @return boolean |
80
|
|
|
*/ |
81
|
|
|
abstract public function save($value, $lifetime = '', $id = null, $group = 'default'); |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Limpia la cache |
85
|
|
|
* |
86
|
|
|
* @param string|null $group |
87
|
|
|
* @return boolean |
88
|
|
|
*/ |
89
|
|
|
abstract public function clean($group = null); |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Elimina un elemento de la cache |
93
|
|
|
* |
94
|
|
|
* @param string $id |
95
|
|
|
* @param string $group |
96
|
|
|
* @return boolean |
97
|
|
|
*/ |
98
|
|
|
abstract public function remove($id, $group = 'default'); |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Inicia el cacheo del buffer de salida hasta que se llame a end |
102
|
|
|
* |
103
|
|
|
* @param string $lifetime tiempo de vida con formato strtotime, utilizado para cache |
104
|
|
|
* @param string $id |
105
|
|
|
* @param string $group |
106
|
|
|
* @return boolean |
107
|
|
|
*/ |
108
|
|
|
public function start($lifetime, $id, $group = 'default') |
109
|
|
|
{ |
110
|
|
|
if ($data = $this->get($id, $group)) { |
111
|
|
|
echo $data; |
112
|
|
|
|
113
|
|
|
// No es necesario cachear |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
$this->_start = [ |
117
|
|
|
'lifetime' => $lifetime, |
118
|
|
|
'id' => $id, |
119
|
|
|
'group' => $group |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
// inicia la captura del buffer |
123
|
|
|
ob_start(); |
124
|
|
|
|
125
|
|
|
// Inicia cacheo |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Termina el buffer de salida |
131
|
|
|
* |
132
|
|
|
* @param boolean $save indica si al terminar guarda la cache |
133
|
|
|
* @return boolean |
134
|
|
|
*/ |
135
|
|
|
public function end($save = true) |
136
|
|
|
{ |
137
|
|
|
if (!$save) { |
138
|
|
|
ob_end_flush(); |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// obtiene el contenido del buffer |
143
|
|
|
$value = ob_get_contents(); |
144
|
|
|
|
145
|
|
|
// libera el buffer |
146
|
|
|
ob_end_flush(); |
147
|
|
|
|
148
|
|
|
return $this->save($value, $this->_start['lifetime'], $this->_start['id'], $this->_start['group']); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Obtiene el driver de cache indicado |
153
|
|
|
* |
154
|
|
|
* @param string $driver (file, sqlite, memsqlite, APC) |
155
|
|
|
* @return Cache |
156
|
|
|
* */ |
157
|
|
|
public static function driver($driver = '') |
158
|
|
|
{ |
159
|
|
|
if (!$driver) { |
160
|
|
|
$driver = self::$_default_driver; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (!isset(self::$_drivers[$driver])) { |
164
|
|
|
require __DIR__ . "/drivers/{$driver}_cache.php"; |
165
|
|
|
$class = $driver . 'cache'; |
166
|
|
|
self::$_drivers[$driver] = new $class(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return self::$_drivers[$driver]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Cambia el driver por defecto |
174
|
|
|
* |
175
|
|
|
* @param string $driver nombre del driver por defecto |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
public static function setDefault($driver = 'file') |
179
|
|
|
{ |
180
|
|
|
self::$_default_driver = $driver; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|