Hoa central
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Hoa\Cache\Backend\Sqlite Class Reference
Inheritance diagram for Hoa\Cache\Backend\Sqlite:

Public Member Functions

 __construct (Array $parameters=[])
 
 store ($data)
 
 load ()
 
 clean ($lifetime=Cache::CLEAN_EXPIRED)
 
 remove ()
 
 __destruct ()
 
- Public Member Functions inherited from Hoa\Cache\Backend\Backend
 store ($data)
 
 load ()
 
 clean ($lifetime=Cache::CLEAN_EXPIRED)
 
 remove ()
 
- Public Member Functions inherited from Hoa\Cache\Cache
 __construct (Array $parameters=[])
 
 getParameters ()
 
 ksort (Array &$array)
 

Protected Member Functions

 setSqlite ()
 
 getSqlite ()
 
 createSchema ()
 
- Protected Member Functions inherited from Hoa\Cache\Cache
 makeId ($id=null)
 
 setId ($id)
 
 getId ()
 
 getIdMd5 ()
 
 removeId ()
 

Protected Attributes

 $_sqlite = null
 

Additional Inherited Members

- Public Attributes inherited from Hoa\Cache\Cache
const CLEAN_ALL = -1
 
const CLEAN_EXPIRED = 0
 
const CLEAN_USER = 1
 
- Static Protected Attributes inherited from Hoa\Cache\Cache
static $_id = []
 

Detailed Description

Class .

SQLite backend manager. SQLite is an extension, take care that SQLite is loaded.

Definition at line 51 of file Sqlite.php.

Constructor & Destructor Documentation

Hoa\Cache\Backend\Sqlite::__construct ( Array  $parameters = [])

Check if SQLite is loaded, else an exception is thrown.

Parameters
array$parametersParameters.
Returns
void
Exceptions

Definition at line 69 of file Sqlite.php.

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  }
Hoa\Cache\Backend\Sqlite::__destruct ( )

Close the SQLite connexion.

Returns
void

Definition at line 320 of file Sqlite.php.

321  {
322  sqlite_close($this->getSqlite());
323 
324  return;
325  }

Here is the call graph for this function:

Member Function Documentation

Hoa\Cache\Backend\Sqlite::clean (   $lifetime = Cache::CLEAN_EXPIRED)

Clean expired cache.

Parameters
string$lifetimeLifetime of caches.
Returns
void
Exceptions

Definition at line 171 of file Sqlite.php.

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  }
const CLEAN_EXPIRED
Definition: Cache.php:62
const CLEAN_ALL
Definition: Cache.php:55
const CLEAN_USER
Definition: Cache.php:69

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Cache\Backend\Sqlite::createSchema ( )
protected

Create the schema, i.e. create the hoa_cache table and the hoa_cache_unique index.

Returns
void
Exceptions

Definition at line 286 of file Sqlite.php.

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  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Cache\Backend\Sqlite::getSqlite ( )
protected

Get the SQLite resource.

Returns
resource

Definition at line 274 of file Sqlite.php.

275  {
276  return $this->_sqlite;
277  }

Here is the caller graph for this function:

Hoa\Cache\Backend\Sqlite::load ( )

Load data from SQLite database.

Returns
mixed

Definition at line 140 of file Sqlite.php.

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  }
clean($lifetime=Cache::CLEAN_EXPIRED)
Definition: Sqlite.php:171
$content
Definition: Hoa.php:119

Here is the call graph for this function:

Hoa\Cache\Backend\Sqlite::remove ( )

Remove a cache data.

Returns
void

Definition at line 208 of file Sqlite.php.

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  }

Here is the call graph for this function:

Hoa\Cache\Backend\Sqlite::setSqlite ( )
protected

Set the SQLite support. If the specified database is :memory: or an unexistant file, the self::createSchema() will be called. If an existent file is given, it must contain the hoa_cache table (please, see the self::createSchema() method). No test is done. By default, the database -> host value will be choosen, but if it's empty, the cache_directory will be choosen to place the database file.

Returns
void
Exceptions

Definition at line 232 of file Sqlite.php.

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  }
static create($name, $mode=self::MODE_CREATE_RECURSIVE, $context=null)
Definition: Directory.php:221

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Cache\Backend\Sqlite::store (   $data)

Save cache content in SQLite database.

Parameters
mixed$dataData to store.
Returns
void

Definition at line 89 of file Sqlite.php.

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  }
clean($lifetime=Cache::CLEAN_EXPIRED)
Definition: Sqlite.php:171

Here is the call graph for this function:

Member Data Documentation

Hoa\Cache\Backend\Sqlite::$_sqlite = null
protected

Definition at line 58 of file Sqlite.php.


The documentation for this class was generated from the following file: