KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
sqlite_cache.php
Ir a la documentación de este archivo.
1 <?php
29 class SqliteCache extends Cache
30 {
31 
37  protected $_db = null;
38 
43  public function __construct()
44  {
49  $this->_db = sqlite_open(APP_PATH . 'temp/cache.db');
50  $result = sqlite_query($this->_db, "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND tbl_name='cache' ");
51  $count = sqlite_fetch_single($result);
52 
53  if (!$count) {
54  sqlite_exec($this->_db, ' CREATE TABLE cache (id TEXT, "group" TEXT, value TEXT, lifetime TEXT) ');
55  }
56 
57  return $this->_db;
58  }
59 
67  public function get($id, $group='default')
68  {
69  $this->_id = $id;
70  $this->_group = $group;
71 
72  $id = addslashes($id);
73  $group = addslashes($group);
74 
75  $id = addslashes($id);
76  $group = addslashes($group);
77  $lifetime = time();
78 
79  $result = sqlite_query($this->_db, " SELECT value FROM cache WHERE id='$id' AND \"group\"='$group' AND lifetime>'$lifetime' OR lifetime='undefined' ");
80  return sqlite_fetch_single($result);
81  }
82 
92  public function save($value, $lifetime=null, $id=false, $group='default')
93  {
94  if (!$id) {
95  $id = $this->_id;
96  $group = $this->_group;
97  }
98 
99  if ($lifetime) {
100  $lifetime = strtotime($lifetime);
101  } else {
102  $lifetime = 'undefined';
103  }
104 
105  $id = addslashes($id);
106  $group = addslashes($group);
107  $value = addslashes($value);
108 
109  $result = sqlite_query($this->_db, " SELECT COUNT(*) FROM cache WHERE id='$id' AND \"group\"='$group' ");
110  $count = sqlite_fetch_single($result);
111 
112 
113  // Ya existe el elemento cacheado
114  if ($count) {
115  return sqlite_exec($this->_db, " UPDATE cache SET value='$value', lifetime='$lifetime' WHERE id='$id' AND \"group\"='$group' ");
116  }
117 
118  return sqlite_exec($this->_db, " INSERT INTO cache (id, \"group\", value, lifetime) VALUES ('$id','$group','$value','$lifetime') ");
119  }
120 
127  public function clean($group=false)
128  {
129  if ($group) {
130  $group = addslashes($group);
131  return sqlite_exec($this->_db, " DELETE FROM cache WHERE \"group\"='$group' ");
132  }
133  return sqlite_exec($this->_db, " DELETE FROM cache ");
134  }
135 
143  public function remove($id, $group='default')
144  {
145  $id = addslashes($id);
146  $group = addslashes($group);
147 
148  return sqlite_exec($this->_db, " DELETE FROM cache WHERE id='$id' AND \"group\"='$group' ");
149  }
150 
151 }