Hoa central
Sqlite.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Cache\Backend;
38 
39 use Hoa\Cache;
40 use Hoa\File as HoaFile;
41 
51 class Sqlite extends Backend
52 {
58  protected $_sqlite = null;
59 
60 
61 
69  public function __construct(Array $parameters = [])
70  {
71  if (!extension_loaded('sqlite')) {
72  throw new Cache\Exception(
73  'SQLite is not loaded on server.',
74  0
75  );
76  }
77 
78  parent::__construct($parameters);
79 
80  return;
81  }
82 
89  public function store($data)
90  {
91  $this->setSqlite();
92  $this->clean();
93 
94  if (true === $this->_parameters->getParameter('serialize_content')) {
95  $data = serialize($data);
96  }
97 
98  $lifetime = $this->_parameters->getParameter('lifetime');
99  $md5 = $this->getIdMd5();
100 
101  $statement =
102  'SELECT data FROM hoa_cache ' . "\n" .
103  'WHERE id = \'' . sqlite_escape_string($this->getIdMd5()) . '\'';
104  $query = sqlite_query($statement, $this->getSqlite());
105 
106  if (0 === sqlite_num_rows($query)) {
107  $statement =
108  'INSERT INTO hoa_cache (' . "\n" .
109  ' id, ' . "\n" .
110  ' data, ' . "\n" .
111  ' will_expire_at ' . "\n" .
112  ')' . "\n" .
113  'VALUES (' . "\n" .
114  ' \'' .
115  sqlite_escape_string($md5) . '\', ' . "\n" .
116  ' \'' .
117  sqlite_escape_string($data) . '\', ' . "\n" .
118  ' \'' .
119  (time() + $lifetime) . '\' ' . "\n" .
120  ')';
121  } else {
122  $statement =
123  'UPDATE hoa_cache ' . "\n" .
124  'SET data = \'' .
125  sqlite_escape_string($data) . '\', ' . "\n" .
126  ' will_expire_at = \'' .
127  (time() + $lifetime) . '\' ' . "\n" .
128  'WHERE id = \'' .
129  sqlite_escape_string($md5) . '\'';
130  }
131 
132  return sqlite_query($statement, $this->getSqlite());
133  }
134 
140  public function load()
141  {
142  $this->setSqlite();
143  $this->clean();
144 
145  $statement =
146  'SELECT data FROM hoa_cache ' . "\n" .
147  'WHERE id = \'' .
148  sqlite_escape_string($this->getIdMd5()) . '\'';
149  $query = sqlite_query($statement, $this->getSqlite());
150 
151  if (0 === sqlite_num_rows($query)) {
152  return false;
153  }
154 
155  $content = sqlite_fetch_single($query);
156 
157  if (true === $this->_parameters->getParameter('serialize_content')) {
158  $content = unserialize($content);
159  }
160 
161  return $content;
162  }
163 
171  public function clean($lifetime = Cache::CLEAN_EXPIRED)
172  {
173  $this->setSqlite();
174 
175  switch ($lifetime) {
176  case Cache::CLEAN_ALL:
177  $statement = 'DELETE FROM hoa_cache';
178 
179  break;
180 
182  $statement =
183  'DELETE FROM hoa_cache ' . "\n" .
184  'WHERE will_expire_at < ' . sqlite_escape_string(time());
185 
186  break;
187 
188  case Cache::CLEAN_USER:
189  throw new Cache\Exception(
190  '\Hoa\Cache::CLEAN_USER constant is not supported by ' .
191  'SQLite cache backend.',
192  1
193  );
194 
195  break;
196  }
197 
198  sqlite_query($statement, $this->getSqlite());
199 
200  return $num;
201  }
202 
208  public function remove()
209  {
210  $this->setSqlite();
211 
212  $statement =
213  'DELETE FROM hoa_cache ' . "\n" .
214  'WHERE id = \'' . sqlite_escape_string($id_md5) . '\'';
215 
216  sqlite_query($statement, $this->getSqlite());
217 
218  return;
219  }
220 
232  protected function setSqlite()
233  {
234  if (null !== $this->_sqlite) {
235  return;
236  }
237 
238  $database = $this->_parameters->getParameter('sqlite.database.host');
239 
240  if (empty($database)) {
241  $database = $this->_parameters->getParameter('sqlite.cache.directory');
242  }
243 
244  $new = false;
245 
246  if (':memory:' === $database) {
247  $new = true;
248  } else {
249  $new = true;
251  $database,
252  HoaFile\Directory::MODE_CREATE_RECURSIVE
253  );
254  }
255 
256  if (false === $this->_sqlite = @sqlite_open($database, 0644, $error)) {
257  throw new Cache\Exception(
258  'Unable to connect to SQLite database : %s.',
259  2,
260  $error
261  );
262  }
263 
264  $new and $this->createSchema();
265 
266  return;
267  }
268 
274  protected function getSqlite()
275  {
276  return $this->_sqlite;
277  }
278 
286  protected function createSchema()
287  {
288  $statements = [
289  'table' =>
290  'CREATE TABLE hoa_cache (' . "\n" .
291  ' id VARCHAR(32), ' . "\n" .
292  ' data LONGVARCHAR, ' . "\n" .
293  ' will_expire_at TIMESTAMP' . "\n" .
294  ')',
295  'index' =>
296  'CREATE UNIQUE INDEX hoa_cache_unique ON hoa_cache (' . "\n" .
297  ' id' . "\n" .
298  ')'
299  ];
300 
301  foreach ($statements as $name => $statement) {
302  if (false === sqlite_query($statement, $this->getSqlite())) {
303  throw new Cache\Exception(
304  sqlite_error_string(
305  sqlite_last_error($this->getSqlite())
306  ),
307  3
308  );
309  }
310  }
311 
312  return;
313  }
314 
320  public function __destruct()
321  {
322  sqlite_close($this->getSqlite());
323 
324  return;
325  }
326 }
clean($lifetime=Cache::CLEAN_EXPIRED)
Definition: Sqlite.php:171
__construct(Array $parameters=[])
Definition: Sqlite.php:69
const CLEAN_EXPIRED
Definition: Cache.php:62
const CLEAN_ALL
Definition: Cache.php:55
$content
Definition: Hoa.php:119
static create($name, $mode=self::MODE_CREATE_RECURSIVE, $context=null)
Definition: Directory.php:221
const CLEAN_USER
Definition: Cache.php:69