blob: 6c1ffc0f35ad97a8571d38786a8719daf119e954 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Database Cache Class
32 *
33 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/database/
36 */
37class CI_DB_Cache {
38
39 var $CI;
40 var $db; // allows passing of db object so that multiple database connections and returned db objects can be supported
41
42 /**
43 * Constructor
44 *
45 * Grabs the CI super object instance so we can access it.
46 *
Barry Mienydd671972010-10-04 16:33:58 +020047 */
Timothy Warrena2097a02011-10-10 10:10:46 -040048 function __construct(&$db)
Derek Allard2067d1a2008-11-13 22:59:24 +000049 {
50 // Assign the main CI object to $this->CI
51 // and load the file helper since we use it a lot
52 $this->CI =& get_instance();
53 $this->db =& $db;
Barry Mienydd671972010-10-04 16:33:58 +020054 $this->CI->load->helper('file');
Derek Allard2067d1a2008-11-13 22:59:24 +000055 }
56
57 // --------------------------------------------------------------------
58
59 /**
60 * Set Cache Directory Path
61 *
62 * @access public
63 * @param string the path to the cache directory
64 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020065 */
Derek Allard2067d1a2008-11-13 22:59:24 +000066 function check_path($path = '')
67 {
68 if ($path == '')
69 {
70 if ($this->db->cachedir == '')
71 {
72 return $this->db->cache_off();
73 }
Barry Mienydd671972010-10-04 16:33:58 +020074
Derek Allard2067d1a2008-11-13 22:59:24 +000075 $path = $this->db->cachedir;
76 }
Barry Mienydd671972010-10-04 16:33:58 +020077
Derek Allard2067d1a2008-11-13 22:59:24 +000078 // Add a trailing slash to the path if needed
Derek Jones37f4b9c2011-07-01 17:56:50 -050079 $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
Derek Allard2067d1a2008-11-13 22:59:24 +000080
81 if ( ! is_dir($path) OR ! is_really_writable($path))
82 {
83 // If the path is wrong we'll turn off caching
84 return $this->db->cache_off();
85 }
Barry Mienydd671972010-10-04 16:33:58 +020086
Derek Allard2067d1a2008-11-13 22:59:24 +000087 $this->db->cachedir = $path;
88 return TRUE;
89 }
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 // --------------------------------------------------------------------
92
93 /**
94 * Retrieve a cached query
95 *
96 * The URI being requested will become the name of the cache sub-folder.
97 * An MD5 hash of the SQL statement will become the cache file name
98 *
99 * @access public
100 * @return string
101 */
102 function read($sql)
103 {
104 if ( ! $this->check_path())
105 {
106 return $this->db->cache_off();
107 }
108
109 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Barry Mienydd671972010-10-04 16:33:58 +0200112
113 $filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql);
114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 if (FALSE === ($cachedata = read_file($filepath)))
Barry Mienydd671972010-10-04 16:33:58 +0200116 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 return FALSE;
118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119
120 return unserialize($cachedata);
121 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000122
123 // --------------------------------------------------------------------
124
125 /**
126 * Write a query to a cache file
127 *
128 * @access public
129 * @return bool
130 */
131 function write($sql, $object)
132 {
133 if ( ! $this->check_path())
134 {
135 return $this->db->cache_off();
136 }
137
138 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
Barry Mienydd671972010-10-04 16:33:58 +0200139
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 $filename = md5($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 if ( ! @is_dir($dir_path))
147 {
148 if ( ! @mkdir($dir_path, DIR_WRITE_MODE))
149 {
150 return FALSE;
151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152
153 @chmod($dir_path, DIR_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 if (write_file($dir_path.$filename, serialize($object)) === FALSE)
157 {
158 return FALSE;
159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Jones172e1612009-10-13 14:32:48 +0000161 @chmod($dir_path.$filename, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 return TRUE;
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Delete cache files within a particular directory
169 *
170 * @access public
171 * @return bool
172 */
173 function delete($segment_one = '', $segment_two = '')
Barry Mienydd671972010-10-04 16:33:58 +0200174 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 if ($segment_one == '')
176 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500177 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 if ($segment_two == '')
181 {
182 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
183 }
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 delete_files($dir_path, TRUE);
188 }
189
190 // --------------------------------------------------------------------
191
192 /**
193 * Delete all existing cache files
194 *
195 * @access public
196 * @return bool
197 */
198 function delete_all()
199 {
200 delete_files($this->db->cachedir, TRUE);
201 }
202
203}
204
205
206/* End of file DB_cache.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000207/* Location: ./system/database/DB_cache.php */