DB_cache.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * Database Cache Class
00020  *
00021  * @category    Database
00022  * @author              ExpressionEngine Dev Team
00023  * @link                http://codeigniter.com/user_guide/database/
00024  */
00025 class CI_DB_Cache {
00026 
00027         var $CI;
00028         var $db;        // allows passing of db object so that multiple database connections and returned db objects can be supported
00029 
00030         /**
00031          * Constructor
00032          *
00033          * Grabs the CI super object instance so we can access it.
00034          *
00035          */     
00036         function CI_DB_Cache(&$db)
00037         {
00038                 // Assign the main CI object to $this->CI
00039                 // and load the file helper since we use it a lot
00040                 $this->CI =& get_instance();
00041                 $this->db =& $db;
00042                 $this->CI->load->helper('file');        
00043         }
00044 
00045         // --------------------------------------------------------------------
00046 
00047         /**
00048          * Set Cache Directory Path
00049          *
00050          * @access      public
00051          * @param       string  the path to the cache directory
00052          * @return      bool
00053          */             
00054         function check_path($path = '')
00055         {
00056                 if ($path == '')
00057                 {
00058                         if ($this->db->cachedir == '')
00059                         {
00060                                 return $this->db->cache_off();
00061                         }
00062                 
00063                         $path = $this->db->cachedir;
00064                 }
00065         
00066                 // Add a trailing slash to the path if needed
00067                 $path = preg_replace("/(.+?)\/*$/", "\\1/",  $path);
00068 
00069                 if ( ! is_dir($path) OR ! is_really_writable($path))
00070                 {
00071                         // If the path is wrong we'll turn off caching
00072                         return $this->db->cache_off();
00073                 }
00074                 
00075                 $this->db->cachedir = $path;
00076                 return TRUE;
00077         }
00078         
00079         // --------------------------------------------------------------------
00080 
00081         /**
00082          * Retrieve a cached query
00083          *
00084          * The URI being requested will become the name of the cache sub-folder.
00085          * An MD5 hash of the SQL statement will become the cache file name
00086          *
00087          * @access      public
00088          * @return      string
00089          */
00090         function read($sql)
00091         {
00092                 if ( ! $this->check_path())
00093                 {
00094                         return $this->db->cache_off();
00095                 }
00096 
00097                 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
00098                 
00099                 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
00100         
00101                 $filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql);            
00102                 
00103                 if (FALSE === ($cachedata = read_file($filepath)))
00104                 {       
00105                         return FALSE;
00106                 }
00107                 
00108                 return unserialize($cachedata);                 
00109         }       
00110 
00111         // --------------------------------------------------------------------
00112 
00113         /**
00114          * Write a query to a cache file
00115          *
00116          * @access      public
00117          * @return      bool
00118          */
00119         function write($sql, $object)
00120         {
00121                 if ( ! $this->check_path())
00122                 {
00123                         return $this->db->cache_off();
00124                 }
00125 
00126                 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
00127                 
00128                 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
00129         
00130                 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
00131                 
00132                 $filename = md5($sql);
00133         
00134                 if ( ! @is_dir($dir_path))
00135                 {
00136                         if ( ! @mkdir($dir_path, DIR_WRITE_MODE))
00137                         {
00138                                 return FALSE;
00139                         }
00140                         
00141                         @chmod($dir_path, DIR_WRITE_MODE);                      
00142                 }
00143                 
00144                 if (write_file($dir_path.$filename, serialize($object)) === FALSE)
00145                 {
00146                         return FALSE;
00147                 }
00148                 
00149                 @chmod($dir_path.$filename, DIR_WRITE_MODE);
00150                 return TRUE;
00151         }
00152 
00153         // --------------------------------------------------------------------
00154 
00155         /**
00156          * Delete cache files within a particular directory
00157          *
00158          * @access      public
00159          * @return      bool
00160          */
00161         function delete($segment_one = '', $segment_two = '')
00162         {       
00163                 if ($segment_one == '')
00164                 {
00165                         $segment_one  = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
00166                 }
00167                 
00168                 if ($segment_two == '')
00169                 {
00170                         $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
00171                 }
00172                 
00173                 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
00174                 
00175                 delete_files($dir_path, TRUE);
00176         }
00177 
00178         // --------------------------------------------------------------------
00179 
00180         /**
00181          * Delete all existing cache files
00182          *
00183          * @access      public
00184          * @return      bool
00185          */
00186         function delete_all()
00187         {
00188                 delete_files($this->db->cachedir, TRUE);
00189         }
00190 
00191 }
00192 
00193 
00194 /* End of file DB_cache.php */
00195 /* Location: ./system/database/DB_cache.php */