blob: 44b8e815f634f2a4ba55b686e38a53cc89b2d984 [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
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
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, 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;
28
29 /**
30 * Constructor
31 *
32 * Grabs the CI super object instance so we can access it.
33 *
34 */
35 function CI_DB_Cache()
36 {
37 // Assign the main CI object to $this->CI
38 // and load the file helper since we use it a lot
39 $this->CI =& get_instance();
40 $this->CI->load->helper('file');
41 }
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 {
56 if ($this->CI->db->cachedir == '')
57 {
58 return $this->CI->db->cache_off();
59 }
60
61 $path = $this->CI->db->cachedir;
62 }
63
64 // Add a trailing slash to the path if needed
65 $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
Derek Allard39b622d2008-01-16 21:10:09 +000066
67 // Load the file helper
68 $this->CI->load->helper('file');
69
70 if ( ! is_dir($path) OR ! is_really_writable($path))
Derek Allardd2df9bc2007-04-15 17:41:17 +000071 {
Derek Allardd2df9bc2007-04-15 17:41:17 +000072 // If the path is wrong we'll turn off caching
73 return $this->CI->db->cache_off();
74 }
75
76 $this->CI->db->cachedir = $path;
77 return TRUE;
78 }
79
80 // --------------------------------------------------------------------
81
82 /**
83 * Retrieve a cached query
84 *
85 * The URI being requested will become the name of the cache sub-folder.
86 * An MD5 hash of the SQL statement will become the cache file name
87 *
88 * @access public
89 * @return string
90 */
91 function read($sql)
92 {
93 if ( ! $this->check_path())
94 {
95 return $this->CI->db->cache_off();
96 }
97
98 $uri = ($this->CI->uri->segment(1) == FALSE) ? 'default.' : $this->CI->uri->segment(1).'+';
99 $uri .= ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
100
101 $filepath = $uri.'/'.md5($sql);
102
103 if (FALSE === ($cachedata = read_file($this->CI->db->cachedir.$filepath)))
104 {
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 {
121 if ( ! $this->check_path())
122 {
123 return $this->CI->db->cache_off();
124 }
125
126 $uri = ($this->CI->uri->segment(1) == FALSE) ? 'default.' : $this->CI->uri->segment(1).'+';
127 $uri .= ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
128
129 $dir_path = $this->CI->db->cachedir.$uri.'/';
130
131 $filename = md5($sql);
132
133 if ( ! @is_dir($dir_path))
134 {
135 if ( ! @mkdir($dir_path, 0777))
136 {
137 return FALSE;
138 }
139
140 @chmod($dir_path, 0777);
141 }
142
143 if (write_file($dir_path.$filename, serialize($object)) === FALSE)
144 {
145 return FALSE;
146 }
147
148 @chmod($dir_path.$filename, 0777);
149 return TRUE;
150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * Delete cache files within a particular directory
156 *
157 * @access public
158 * @return bool
159 */
160 function delete($segment_one = '', $segment_two = '')
161 {
162 if ($segment_one == '')
163 {
Derek Allard85c7c612008-01-14 13:18:36 +0000164 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000165 }
166
167 if ($segment_two == '')
168 {
169 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
170 }
171
172 $dir_path = $this->CI->db->cachedir.$segment_one.'+'.$segment_two.'/';
173
174 delete_files($dir_path, TRUE);
175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * Delete all existing cache files
181 *
182 * @access public
183 * @return bool
184 */
185 function delete_all()
186 {
187 delete_files($this->CI->db->cachedir, TRUE);
188 }
189
190}
191
admin7e75a7e2006-10-04 18:43:24 +0000192?>