blob: 86493e77c6f3e74655f6c32be3ae368450cacb9e [file] [log] [blame]
admin7e75a7e2006-10-04 18:43:24 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Database Cache Class
20 *
21 * @category Database
22 * @author Rick Ellis
23 * @link http://www.codeigniter.com/user_guide/database/
24 */
25class CI_DB_Cache {
26
adminb3ab70b2006-10-07 03:07:29 +000027 var $CI;
admin7e75a7e2006-10-04 18:43:24 +000028
29 /**
30 * Constructor
31 *
32 * Grabs the CI super object instance so we can access it.
33 *
34 */
35 function CI_DB_Cache()
36 {
adminb3ab70b2006-10-07 03:07:29 +000037 // Assign the main CI object to $this->CI
admin7e75a7e2006-10-04 18:43:24 +000038 // and load the file helper since we use it a lot
adminb3ab70b2006-10-07 03:07:29 +000039 $this->CI =& get_instance();
40 $this->CI->load->helper('file');
admin7e75a7e2006-10-04 18:43:24 +000041 }
42
43 // --------------------------------------------------------------------
44
45 /**
46 * Set Cache Directory Path
47 *
48 * @access public
49 * @param string the path to the cache directory
50 * @return bool
51 */
52 function check_path($path = '')
53 {
54 if ($path == '')
55 {
adminb3ab70b2006-10-07 03:07:29 +000056 if ($this->CI->db->cachedir == '')
admin7e75a7e2006-10-04 18:43:24 +000057 {
adminb3ab70b2006-10-07 03:07:29 +000058 return $this->CI->db->cache_off();
admin7e75a7e2006-10-04 18:43:24 +000059 }
60
adminb3ab70b2006-10-07 03:07:29 +000061 $path = $this->CI->db->cachedir;
admin7e75a7e2006-10-04 18:43:24 +000062 }
63
64 // Add a trailing slash to the path if needed
65 $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
66
67 if ( ! is_dir($path) OR ! is_writable($path))
68 {
adminb3ab70b2006-10-07 03:07:29 +000069 if ($this->CI->db->db_debug)
admin7e75a7e2006-10-04 18:43:24 +000070 {
adminb3ab70b2006-10-07 03:07:29 +000071 return $this->CI->db->display_error('db_invalid_cache_path');
admin7e75a7e2006-10-04 18:43:24 +000072 }
73
74 // If the path is wrong we'll turn off caching
adminb3ab70b2006-10-07 03:07:29 +000075 return $this->CI->db->cache_off();
admin7e75a7e2006-10-04 18:43:24 +000076 }
77
adminb3ab70b2006-10-07 03:07:29 +000078 $this->CI->db->cachedir = $path;
admin7e75a7e2006-10-04 18:43:24 +000079 return TRUE;
80 }
81
82 // --------------------------------------------------------------------
83
84 /**
85 * Retreive a cached query
86 *
87 * The URI being requested will become the name of the cache sub-folder.
88 * An MD5 hash of the SQL statement will become the cache file name
89 *
90 * @access public
91 * @return string
92 */
93 function read($sql)
94 {
95 if ( ! $this->check_path())
96 {
adminb3ab70b2006-10-07 03:07:29 +000097 return $this->CI->db->cache_off();
admin7e75a7e2006-10-04 18:43:24 +000098 }
99
admindee8ba22006-10-07 03:20:32 +0000100 $uri = ($this->CI->uri->segment(1) == FALSE) ? 'default.' : $this->CI->uri->segment(1).'+';
adminb3ab70b2006-10-07 03:07:29 +0000101 $uri .= ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
admin7e75a7e2006-10-04 18:43:24 +0000102
admina0564c02006-10-05 22:18:11 +0000103 $filepath = $uri.'/'.md5($sql);
admin7e75a7e2006-10-04 18:43:24 +0000104
adminb3ab70b2006-10-07 03:07:29 +0000105 if (FALSE === ($cachedata = read_file($this->CI->db->cachedir.$filepath)))
admin7e75a7e2006-10-04 18:43:24 +0000106 {
107 return FALSE;
108 }
109
110 return unserialize($cachedata);
111 }
112
113 // --------------------------------------------------------------------
114
115 /**
116 * Write a query to a cache file
117 *
118 * @access public
119 * @return bool
120 */
121 function write($sql, $object)
122 {
123 if ( ! $this->check_path())
124 {
adminb3ab70b2006-10-07 03:07:29 +0000125 return $this->CI->db->cache_off();
admin7e75a7e2006-10-04 18:43:24 +0000126 }
127
admindee8ba22006-10-07 03:20:32 +0000128 $uri = ($this->CI->uri->segment(1) == FALSE) ? 'default.' : $this->CI->uri->segment(1).'+';
adminb3ab70b2006-10-07 03:07:29 +0000129 $uri .= ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
admin7e75a7e2006-10-04 18:43:24 +0000130
adminb3ab70b2006-10-07 03:07:29 +0000131 $dir_path = $this->CI->db->cachedir.$uri.'/';
admin7e75a7e2006-10-04 18:43:24 +0000132
133 $filename = md5($sql);
134
135 if ( ! @is_dir($dir_path))
136 {
137 if ( ! @mkdir($dir_path, 0777))
138 {
139 return FALSE;
140 }
141
142 @chmod($dir_path, 0777);
143 }
144
145 if (write_file($dir_path.$filename, serialize($object)) === FALSE)
146 {
147 return FALSE;
148 }
149
150 @chmod($dir_path.$filename, 0777);
151 return TRUE;
152 }
153
154 // --------------------------------------------------------------------
155
156 /**
157 * Delete cache files within a particular directory
158 *
159 * @access public
160 * @return bool
161 */
admine6419c42006-10-06 01:50:43 +0000162 function delete($segment_one = '', $segment_two = '')
163 {
164 if ($segment_one == '')
165 {
adminb3ab70b2006-10-07 03:07:29 +0000166 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(2);
admine6419c42006-10-06 01:50:43 +0000167 }
admin7e75a7e2006-10-04 18:43:24 +0000168
admine6419c42006-10-06 01:50:43 +0000169 if ($segment_two == '')
170 {
adminb3ab70b2006-10-07 03:07:29 +0000171 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
admine6419c42006-10-06 01:50:43 +0000172 }
173
admindee8ba22006-10-07 03:20:32 +0000174 $dir_path = $this->CI->db->cachedir.md5($segment_one.'+'.$segment_two).'/';
admin7e75a7e2006-10-04 18:43:24 +0000175
176 delete_files($dir_path, TRUE);
177 }
178
179 // --------------------------------------------------------------------
180
181 /**
182 * Delete all existing cache files
183 *
184 * @access public
185 * @return bool
186 */
187 function delete_all()
188 {
adminb3ab70b2006-10-07 03:07:29 +0000189 delete_files($this->CI->db->cachedir, TRUE);
admin7e75a7e2006-10-04 18:43:24 +0000190 }
191
192}
193
194?>