blob: 982725aaa4c4539a82015e41d729302d9ac78040 [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
Derek Allard73274992008-05-05 16:39:18 +000067 if (! is_dir($path) OR ! is_really_writable($path))
Derek Allardd2df9bc2007-04-15 17:41:17 +000068 {
Derek Allardd2df9bc2007-04-15 17:41:17 +000069 // If the path is wrong we'll turn off caching
70 return $this->CI->db->cache_off();
71 }
72
73 $this->CI->db->cachedir = $path;
74 return TRUE;
75 }
76
77 // --------------------------------------------------------------------
78
79 /**
80 * Retrieve a cached query
81 *
82 * The URI being requested will become the name of the cache sub-folder.
83 * An MD5 hash of the SQL statement will become the cache file name
84 *
85 * @access public
86 * @return string
87 */
88 function read($sql)
89 {
Derek Allard73274992008-05-05 16:39:18 +000090 if (! $this->check_path())
Derek Allardd2df9bc2007-04-15 17:41:17 +000091 {
92 return $this->CI->db->cache_off();
93 }
94
95 $uri = ($this->CI->uri->segment(1) == FALSE) ? 'default.' : $this->CI->uri->segment(1).'+';
96 $uri .= ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
97
98 $filepath = $uri.'/'.md5($sql);
99
100 if (FALSE === ($cachedata = read_file($this->CI->db->cachedir.$filepath)))
101 {
102 return FALSE;
103 }
104
105 return unserialize($cachedata);
106 }
107
108 // --------------------------------------------------------------------
109
110 /**
111 * Write a query to a cache file
112 *
113 * @access public
114 * @return bool
115 */
116 function write($sql, $object)
117 {
Derek Allard73274992008-05-05 16:39:18 +0000118 if (! $this->check_path())
Derek Allardd2df9bc2007-04-15 17:41:17 +0000119 {
120 return $this->CI->db->cache_off();
121 }
122
123 $uri = ($this->CI->uri->segment(1) == FALSE) ? 'default.' : $this->CI->uri->segment(1).'+';
124 $uri .= ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
125
126 $dir_path = $this->CI->db->cachedir.$uri.'/';
127
128 $filename = md5($sql);
129
Derek Allard73274992008-05-05 16:39:18 +0000130 if (! @is_dir($dir_path))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000131 {
Derek Allard73274992008-05-05 16:39:18 +0000132 if (! @mkdir($dir_path, DIR_WRITE_MODE))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000133 {
134 return FALSE;
135 }
136
Derek Jones3ad8efe2008-04-04 18:56:04 +0000137 @chmod($dir_path, DIR_WRITE_MODE);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000138 }
139
140 if (write_file($dir_path.$filename, serialize($object)) === FALSE)
141 {
142 return FALSE;
143 }
144
Derek Jones3ad8efe2008-04-04 18:56:04 +0000145 @chmod($dir_path.$filename, DIR_WRITE_MODE);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000146 return TRUE;
147 }
148
149 // --------------------------------------------------------------------
150
151 /**
152 * Delete cache files within a particular directory
153 *
154 * @access public
155 * @return bool
156 */
157 function delete($segment_one = '', $segment_two = '')
158 {
159 if ($segment_one == '')
160 {
Derek Allard85c7c612008-01-14 13:18:36 +0000161 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000162 }
163
164 if ($segment_two == '')
165 {
166 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
167 }
168
169 $dir_path = $this->CI->db->cachedir.$segment_one.'+'.$segment_two.'/';
170
171 delete_files($dir_path, TRUE);
172 }
173
174 // --------------------------------------------------------------------
175
176 /**
177 * Delete all existing cache files
178 *
179 * @access public
180 * @return bool
181 */
182 function delete_all()
183 {
184 delete_files($this->CI->db->cachedir, TRUE);
185 }
186
187}
188
admin7e75a7e2006-10-04 18:43:24 +0000189?>