KumbiaPHP beta2-dev
Framework PHP en español
sqlite_cache.php
Ir a la documentación de este archivo.
00001 <?php
00023 class SqliteCache extends Cache
00024 {
00030     protected $_db = null;
00031     
00036     public function __construct() 
00037     {
00042         $this->_db = sqlite_open(APP_PATH . 'temp/cache.db');
00043         $result = sqlite_query($this->_db, "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND tbl_name='cache' ");
00044         $count = sqlite_fetch_single($result);
00045        
00046         if(!$count) {
00047             sqlite_exec($this->_db, ' CREATE TABLE cache (id TEXT, "group" TEXT, value TEXT, lifetime TEXT) ');
00048         }
00049         
00050         return $this->_db;
00051     }
00052     
00060         public function get($id, $group='default') 
00061     {
00062         $this->_id = $id;
00063         $this->_group = $group;
00064     
00065         $id = addslashes($id);
00066         $group = addslashes($group);
00067         
00068         $id = addslashes($id);
00069         $group = addslashes($group);
00070         $lifetime = time();
00071         
00072         $result = sqlite_query($this->_db, " SELECT value FROM cache WHERE id='$id' AND \"group\"='$group' AND lifetime>'$lifetime' OR lifetime='undefined' ");
00073         return sqlite_fetch_single($result);
00074     }
00075     
00085         public function save($value, $lifetime=null, $id=false, $group='default')
00086     {
00087         if (! $id) {
00088             $id = $this->_id;
00089             $group = $this->_group;
00090         }
00091         
00092         if ($lifetime) {
00093             $lifetime = strtotime($lifetime);
00094         } else {
00095             $lifetime = 'undefined';
00096         }
00097         
00098         $id = addslashes($id);
00099         $group = addslashes($group);
00100         $value = addslashes($value);
00101         
00102         $result = sqlite_query($this->_db, " SELECT COUNT(*) FROM cache WHERE id='$id' AND \"group\"='$group' ");
00103         $count = sqlite_fetch_single($result);
00104         
00105         
00106         // Ya existe el elemento cacheado
00107         if($count) {
00108             return sqlite_exec($this->_db, " UPDATE cache SET value='$value', lifetime='$lifetime' WHERE id='$id' AND \"group\"='$group' ");
00109         }
00110         
00111         return sqlite_exec($this->_db, " INSERT INTO cache (id, \"group\", value, lifetime) VALUES ('$id','$group','$value','$lifetime') ");
00112     }
00113     
00120         public function clean($group=false)
00121     {
00122         if($group) {
00123             $group = addslashes($group);
00124             return sqlite_exec($this->_db, " DELETE FROM cache WHERE \"group\"='$group' ");
00125         }
00126         return sqlite_exec($this->_db, " DELETE FROM cache ");
00127     }
00128     
00136         public function remove($id, $group='default')
00137     {
00138         $id = addslashes($id);
00139         $group = addslashes($group);
00140         
00141         return sqlite_exec($this->_db, " DELETE FROM cache WHERE id='$id' AND \"group\"='$group' ");
00142     }
00143 }
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Enumeraciones