Changeset 1421

Show
Ignore:
Timestamp:
12/05/2007 12:41:08 AM (13 months ago)
Author:
Shadowhand
Message:

Updates to Cache:

  • Added an sqlite error logger and put it into use
  • Added a config/cache_sqlite file with the schema for the table
  • Updated Cache_Sqlite with optimizations, error logging, and code clarity
Location:
trunk/system
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/i18n/en_US/cache.php

    r1297 r1421  
    66        'unwritable'           => 'The configured storage location, <tt>%s</tt>, is not writable.', 
    77        'resources'            => 'Caching of resources is impossible, because resources cannot be serialized.', 
     8        'driver_error'         => '%s', 
    89); 
  • trunk/system/libraries/drivers/Cache_Sqlite.php

    r1420 r1421  
    1717 
    1818        /** 
     19         * Logs an SQLite error. 
     20         */ 
     21        protected static function log_error($code) 
     22        { 
     23                // Log an error 
     24                Log::add('error', 'Cache: SQLite error: '.sqlite_error_string($error)); 
     25        } 
     26 
     27        /** 
    1928         * Tests that the storage location is a directory and is writable. 
    2029         */ 
     
    2332                // Find the real path to the directory 
    2433                $filename = str_replace('\\', '/', realpath($filename)); 
    25  
    26                 if ( ! is_file($filename) OR ! is_writable($filename)) 
    27                         throw new Kohana_Exception('cache.unwritable', $filename); 
    2834 
    2935                // Get the filename from the directory 
     
    3440                        throw new Kohana_Exception('cache.unwritable', $directory); 
    3541 
    36                 // Open the database 
    37                 $this->db = sqlite_factory($filename, '0666', $error); 
     42                // Open up an instance of the database 
     43                $this->db = new SQLiteDatabase($filename, '0666', $error); 
    3844 
    3945                // Throw an exception if there's an error 
     
    4147                        throw new Kohana_Exception('cache.driver_error', sqlite_error_string($error)); 
    4248 
    43                 // Directory is valid 
    44                 $this->directory = $directory; 
     49                $query  = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'caches'"; 
     50                $tables = $this->db->query($query, SQLITE_BOTH, $error); 
     51 
     52                // Throw an exception if there's an error 
     53                if ( ! empty($error)) 
     54                        throw new Kohana_Exception('cache.driver_error', sqlite_error_string($error)); 
     55 
     56                if ($tables->numRows() == 0) 
     57                { 
     58                        Log::add('error', 'Cache: Initializing new SQLite cache database'); 
     59 
     60                        // Issue a CREATE TABLE command 
     61                        $this->db->unbufferedQuery(Config::item('cache_sqlite.schema')); 
     62                } 
    4563        } 
    4664 
     
    5472        { 
    5573                // Find the id that matches 
    56                 $query = $this->db->query('SELECT id FROM caches WHERE id = "'.$id.'"', SQLITE_BOTH, $error); 
    57  
    58                 return ($query->numRows() > 0); 
     74                $query = "SELECT id FROM caches WHERE id = '$id'"; 
     75 
     76                return ($this->db->query($query)->numRows() > 0); 
    5977        } 
    6078 
     
    7997                $tags = sqlite_escape_string(implode(',', $tags)); 
    8098 
    81                 if ($this->exists($id)) 
    82                 { 
    83                         $this->db->unbufferedQuery("UPDATE caches SET hash = '$hash', tags = '$tags', expiration = '$expiration', data = '$data' WHERE id = '$id'", SQLITE_BOTH, $error); 
    84                 } 
    85                 else 
    86                 { 
    87                         $this->db->unbufferedQuery("INSERT INTO caches VALUES('$id', '$hash', '$tags', '$expiration', '$data')", SQLITE_BOTH, $error); 
    88                 } 
    89  
    90                 // Log errors 
    91                 empty($error) or Log::add('error', 'Cache: unable to write '.$id.' to cache database'); 
     99                $query = $this->exists($id) 
     100                        ? "UPDATE caches SET hash = '$hash', tags = '$tags', expiration = '$expiration', cache = '$data' WHERE id = '$id'" 
     101                        : "INSERT INTO caches VALUES('$id', '$hash', '$tags', '$expiration', '$data')"; 
     102 
     103                // Run the query 
     104                $this->db->unbufferedQuery($query, SQLITE_BOTH, $error); 
     105 
     106                empty($error) or self::log_error($error); 
    92107 
    93108                return empty($error); 
     
    102117        public function find($tag) 
    103118        { 
    104                 $query = $this->db->query("SELECT id FROM caches WHERE tags LIKE '%$tag%'", SQLITE_BOTH, $error); 
     119                $query = "SELECT id FROM caches WHERE tags LIKE '%{$tag}%'"; 
     120                $query = $this->db->query($query, SQLITE_BOTH, $error); 
     121 
     122                empty($error) or self::log_error($error); 
    105123 
    106124                if (empty($error) AND $query->numRows() > 0) 
     
    127145        public function get($id) 
    128146        { 
    129                 $query = $this->db->unbufferedQuery("SELECT id, hash, expiration, data FROM caches WHERE id = '$id' LIMIT 1", SQLITE_BOTH, $error); 
     147                $query = "SELECT id, hash, expiration, cache FROM caches WHERE id = '{$id}' LIMIT 0, 1"; 
     148                $query = $this->db->query($query, SQLITE_BOTH, $error); 
     149 
     150                empty($error) or self::log_error($error); 
    130151 
    131152                if (empty($error) AND $cache = $query->fetchObject()) 
    132153                { 
    133154                        // Make sure the expiration is valid and that the hash matches 
    134                         if (($cache->expiration != 0 AND $cache->expiration <= time()) OR $cache->hash !== sha1($cache->data)) 
     155                        if (($cache->expiration != 0 AND $cache->expiration <= time()) OR $cache->hash !== sha1($cache->cache)) 
    135156                        { 
    136                                 // Cache is not valid 
     157                                // Cache is not valid, delete it now 
    137158                                $this->del($cache->id); 
    138                                 return NULL; 
    139159                        } 
    140                 } 
    141                 else 
    142                 { 
    143                         // Nothing found 
    144                         return NULL; 
    145                 } 
    146  
    147                 return $cache->data; 
     160                        else 
     161                        { 
     162                                // Return the valid cache data 
     163                                return $cache->cache; 
     164                        } 
     165                } 
     166 
     167                // No valid cache foud 
     168                return NULL; 
    148169        } 
    149170 
     
    160181                { 
    161182                        // Delete all caches 
    162                         $this->db->unbufferedQuery('DELETE FROM caches WHERE 1', SQLITE_BOTH, $error); 
     183                        $where = '1'; 
    163184                } 
    164185                elseif ($tag == FALSE) 
    165186                { 
    166187                        // Delete by id 
    167                         $this->db->unbufferedQuery('DELETE FROM caches WHERE id = "'.$id.'"', SQLITE_BOTH, $error); 
     188                        $where = "id = '{$id}'"; 
    168189                } 
    169190                else 
    170191                { 
    171                         // Delete by tags 
    172                         $this->db->unbufferedQuery("DELETE FROM caches WHERE tags LIKE '%$tag%'", SQLITE_BOTH, $error); 
    173                 } 
    174  
    175                 // Log errors 
    176                 empty($error) or Log::add('error', 'Cache: Unable to delete cache: '.$id); 
     192                        // Delete by tag 
     193                        $where = "tags LIKE '%{$tag}%'"; 
     194                } 
     195 
     196                $this->db->unbufferedQuery('DELETE FROM caches WHERE '.$where, SQLITE_BOTH, $error); 
     197 
     198                empty($error) or self::log_error($error); 
    177199 
    178200                return empty($error); 
     
    185207        { 
    186208                // Delete all expired caches 
    187                 $this->db->unbufferedQuery('DELETE FROM caches WHERE expiration != 0 AND expiration <= '.time(), SQLITE_BOTH, $error); 
     209                $query = 'DELETE FROM caches WHERE expiration != 0 AND expiration <= '.time(); 
     210 
     211                $this->db->unbufferedQuery($query); 
    188212 
    189213                return TRUE;