Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?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 |
| 8 | * @author ExpressionEngine Dev Team |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Database Cache Class |
| 20 | * |
| 21 | * @category Database |
| 22 | * @author ExpressionEngine Dev Team |
| 23 | * @link http://codeigniter.com/user_guide/database/ |
| 24 | */ |
| 25 | class CI_DB_Cache { |
| 26 | |
| 27 | var $CI; |
| 28 | var $db; // allows passing of db object so that multiple database connections and returned db objects can be supported |
| 29 | |
| 30 | /** |
| 31 | * Constructor |
| 32 | * |
| 33 | * Grabs the CI super object instance so we can access it. |
| 34 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 35 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | function CI_DB_Cache(&$db) |
| 37 | { |
| 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(); |
| 41 | $this->db =& $db; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 42 | $this->CI->load->helper('file'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 53 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | function check_path($path = '') |
| 55 | { |
| 56 | if ($path == '') |
| 57 | { |
| 58 | if ($this->db->cachedir == '') |
| 59 | { |
| 60 | return $this->db->cache_off(); |
| 61 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 62 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 63 | $path = $this->db->cachedir; |
| 64 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 65 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | // Add a trailing slash to the path if needed |
| 67 | $path = preg_replace("/(.+?)\/*$/", "\\1/", $path); |
| 68 | |
| 69 | if ( ! is_dir($path) OR ! is_really_writable($path)) |
| 70 | { |
| 71 | // If the path is wrong we'll turn off caching |
| 72 | return $this->db->cache_off(); |
| 73 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 74 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | $this->db->cachedir = $path; |
| 76 | return TRUE; |
| 77 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 78 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | { |
| 92 | if ( ! $this->check_path()) |
| 93 | { |
| 94 | return $this->db->cache_off(); |
| 95 | } |
| 96 | |
| 97 | $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 98 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 99 | $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 100 | |
| 101 | $filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql); |
| 102 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | if (FALSE === ($cachedata = read_file($filepath))) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 104 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | return FALSE; |
| 106 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 107 | |
| 108 | return unserialize($cachedata); |
| 109 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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->db->cache_off(); |
| 124 | } |
| 125 | |
| 126 | $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 127 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 129 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 131 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | $filename = md5($sql); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 133 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | if ( ! @is_dir($dir_path)) |
| 135 | { |
| 136 | if ( ! @mkdir($dir_path, DIR_WRITE_MODE)) |
| 137 | { |
| 138 | return FALSE; |
| 139 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 140 | |
| 141 | @chmod($dir_path, DIR_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 143 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | if (write_file($dir_path.$filename, serialize($object)) === FALSE) |
| 145 | { |
| 146 | return FALSE; |
| 147 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 148 | |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 149 | @chmod($dir_path.$filename, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | 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 = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 162 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | if ($segment_one == '') |
| 164 | { |
| 165 | $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1); |
| 166 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 167 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | if ($segment_two == '') |
| 169 | { |
| 170 | $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2); |
| 171 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 172 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 174 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | { |
| 188 | delete_files($this->db->cachedir, TRUE); |
| 189 | } |
| 190 | |
| 191 | } |
| 192 | |
| 193 | |
| 194 | /* End of file DB_cache.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 195 | /* Location: ./system/database/DB_cache.php */ |