blob: 9bb1b165b496d06685a607920c1e22208e18ebb4 [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardd2df9bc2007-04-15 17:41:17 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Rick Ellis9ba2bf22008-09-12 23:34:39 +00009 * @copyright Copyright (c) 2008, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Database Cache Class
20 *
21 * @category Database
Derek Allard3d879d52008-01-18 19:41:32 +000022 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000023 * @link http://codeigniter.com/user_guide/database/
Derek Allardd2df9bc2007-04-15 17:41:17 +000024 */
25class CI_DB_Cache {
26
27 var $CI;
Derek Jonesd36ade02008-05-12 15:17:41 +000028 var $db; // allows passing of db object so that multiple database connections and returned db objects can be supported
Derek Allardd2df9bc2007-04-15 17:41:17 +000029
30 /**
31 * Constructor
32 *
33 * Grabs the CI super object instance so we can access it.
34 *
35 */
Derek Jonesd36ade02008-05-12 15:17:41 +000036 function CI_DB_Cache(&$db)
Derek Allardd2df9bc2007-04-15 17:41:17 +000037 {
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();
Derek Jonesd36ade02008-05-12 15:17:41 +000041 $this->db =& $db;
Derek Allardd2df9bc2007-04-15 17:41:17 +000042 $this->CI->load->helper('file');
43 }
44
45 // --------------------------------------------------------------------
46
47 /**
48 * Set Cache Directory Path
49 *
50 * @access public
51 * @param string the path to the cache directory
52 * @return bool
53 */
54 function check_path($path = '')
55 {
56 if ($path == '')
57 {
Derek Jonesd36ade02008-05-12 15:17:41 +000058 if ($this->db->cachedir == '')
Derek Allardd2df9bc2007-04-15 17:41:17 +000059 {
Derek Jonesd36ade02008-05-12 15:17:41 +000060 return $this->db->cache_off();
Derek Allardd2df9bc2007-04-15 17:41:17 +000061 }
62
Derek Jonesd36ade02008-05-12 15:17:41 +000063 $path = $this->db->cachedir;
Derek Allardd2df9bc2007-04-15 17:41:17 +000064 }
65
66 // Add a trailing slash to the path if needed
67 $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
Derek Allard39b622d2008-01-16 21:10:09 +000068
Derek Jones0b59f272008-05-13 04:22:33 +000069 if ( ! is_dir($path) OR ! is_really_writable($path))
Derek Allardd2df9bc2007-04-15 17:41:17 +000070 {
Derek Allardd2df9bc2007-04-15 17:41:17 +000071 // If the path is wrong we'll turn off caching
Derek Jonesd36ade02008-05-12 15:17:41 +000072 return $this->db->cache_off();
Derek Allardd2df9bc2007-04-15 17:41:17 +000073 }
74
Derek Jonesd36ade02008-05-12 15:17:41 +000075 $this->db->cachedir = $path;
Derek Allardd2df9bc2007-04-15 17:41:17 +000076 return TRUE;
77 }
78
79 // --------------------------------------------------------------------
80
81 /**
82 * Retrieve a cached query
83 *
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
86 *
87 * @access public
88 * @return string
89 */
90 function read($sql)
91 {
Derek Jones0b59f272008-05-13 04:22:33 +000092 if ( ! $this->check_path())
Derek Allardd2df9bc2007-04-15 17:41:17 +000093 {
Derek Jonesd36ade02008-05-12 15:17:41 +000094 return $this->db->cache_off();
Derek Allardd2df9bc2007-04-15 17:41:17 +000095 }
Rick Ellise5ff3dc2008-10-17 04:57:16 +000096
97 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
98
99 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000100
Rick Ellise5ff3dc2008-10-17 04:57:16 +0000101 $filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000102
Rick Ellise5ff3dc2008-10-17 04:57:16 +0000103 if (FALSE === ($cachedata = read_file($filepath)))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000104 {
105 return FALSE;
106 }
107
108 return unserialize($cachedata);
109 }
110
111 // --------------------------------------------------------------------
112
113 /**
114 * Write a query to a cache file
115 *
116 * @access public
117 * @return bool
118 */
119 function write($sql, $object)
120 {
Derek Jones0b59f272008-05-13 04:22:33 +0000121 if ( ! $this->check_path())
Derek Allardd2df9bc2007-04-15 17:41:17 +0000122 {
Derek Jonesd36ade02008-05-12 15:17:41 +0000123 return $this->db->cache_off();
Derek Allardd2df9bc2007-04-15 17:41:17 +0000124 }
125
Rick Ellise5ff3dc2008-10-17 04:57:16 +0000126 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000127
Rick Ellise5ff3dc2008-10-17 04:57:16 +0000128 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
129
130 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000131
132 $filename = md5($sql);
133
Derek Jones0b59f272008-05-13 04:22:33 +0000134 if ( ! @is_dir($dir_path))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000135 {
Derek Jones0b59f272008-05-13 04:22:33 +0000136 if ( ! @mkdir($dir_path, DIR_WRITE_MODE))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000137 {
138 return FALSE;
139 }
140
Derek Jones3ad8efe2008-04-04 18:56:04 +0000141 @chmod($dir_path, DIR_WRITE_MODE);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000142 }
143
144 if (write_file($dir_path.$filename, serialize($object)) === FALSE)
145 {
146 return FALSE;
147 }
148
Derek Jones3ad8efe2008-04-04 18:56:04 +0000149 @chmod($dir_path.$filename, DIR_WRITE_MODE);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000150 return TRUE;
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Delete cache files within a particular directory
157 *
158 * @access public
159 * @return bool
160 */
161 function delete($segment_one = '', $segment_two = '')
162 {
163 if ($segment_one == '')
164 {
Derek Allard85c7c612008-01-14 13:18:36 +0000165 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000166 }
167
168 if ($segment_two == '')
169 {
170 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
171 }
172
Derek Jonesd36ade02008-05-12 15:17:41 +0000173 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000174
175 delete_files($dir_path, TRUE);
176 }
177
178 // --------------------------------------------------------------------
179
180 /**
181 * Delete all existing cache files
182 *
183 * @access public
184 * @return bool
185 */
186 function delete_all()
187 {
Derek Jonesd36ade02008-05-12 15:17:41 +0000188 delete_files($this->db->cachedir, TRUE);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000189 }
190
191}
192
Derek Jonesd36ade02008-05-12 15:17:41 +0000193
194/* End of file DB_cache.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000195/* Location: ./system/database/DB_cache.php */