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