1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.0
\r
16 // ------------------------------------------------------------------------
\r
19 * Database Cache Class
\r
21 * @category Database
\r
22 * @author ExpressionEngine Dev Team
\r
23 * @link http://codeigniter.com/user_guide/database/
\r
28 var $db; // allows passing of db object so that multiple database connections and returned db objects can be supported
\r
33 * Grabs the CI super object instance so we can access it.
\r
36 function CI_DB_Cache(&$db)
\r
38 // Assign the main CI object to $this->CI
\r
39 // and load the file helper since we use it a lot
\r
40 $this->CI =& get_instance();
\r
42 $this->CI->load->helper('file');
\r
45 // --------------------------------------------------------------------
\r
48 * Set Cache Directory Path
\r
51 * @param string the path to the cache directory
\r
54 function check_path($path = '')
\r
58 if ($this->db->cachedir == '')
\r
60 return $this->db->cache_off();
\r
63 $path = $this->db->cachedir;
\r
66 // Add a trailing slash to the path if needed
\r
67 $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
\r
69 if ( ! is_dir($path) OR ! is_really_writable($path))
\r
71 // If the path is wrong we'll turn off caching
\r
72 return $this->db->cache_off();
\r
75 $this->db->cachedir = $path;
\r
79 // --------------------------------------------------------------------
\r
82 * Retrieve a cached query
\r
84 * The URI being requested will become the name of the cache sub-folder.
\r
85 * An MD5 hash of the SQL statement will become the cache file name
\r
92 if ( ! $this->check_path())
\r
94 return $this->db->cache_off();
\r
97 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
\r
99 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
\r
101 $filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql);
\r
103 if (FALSE === ($cachedata = read_file($filepath)))
\r
108 return unserialize($cachedata);
\r
111 // --------------------------------------------------------------------
\r
114 * Write a query to a cache file
\r
119 function write($sql, $object)
\r
121 if ( ! $this->check_path())
\r
123 return $this->db->cache_off();
\r
126 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
\r
128 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
\r
130 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
\r
132 $filename = md5($sql);
\r
134 if ( ! @is_dir($dir_path))
\r
136 if ( ! @mkdir($dir_path, DIR_WRITE_MODE))
\r
141 @chmod($dir_path, DIR_WRITE_MODE);
\r
144 if (write_file($dir_path.$filename, serialize($object)) === FALSE)
\r
149 @chmod($dir_path.$filename, DIR_WRITE_MODE);
\r
153 // --------------------------------------------------------------------
\r
156 * Delete cache files within a particular directory
\r
161 function delete($segment_one = '', $segment_two = '')
\r
163 if ($segment_one == '')
\r
165 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
\r
168 if ($segment_two == '')
\r
170 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
\r
173 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
\r
175 delete_files($dir_path, TRUE);
\r
178 // --------------------------------------------------------------------
\r
181 * Delete all existing cache files
\r
186 function delete_all()
\r
188 delete_files($this->db->cachedir, TRUE);
\r
194 /* End of file DB_cache.php */
\r
195 /* Location: ./system/database/DB_cache.php */