admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 1 | <?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 | */ |
| 25 | class CI_DB_Cache { |
| 26 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 27 | var $CI; |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 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 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 37 | // Assign the main CI object to $this->CI |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 38 | // and load the file helper since we use it a lot |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 39 | $this->CI =& get_instance(); |
| 40 | $this->CI->load->helper('file'); |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 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 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 56 | if ($this->CI->db->cachedir == '') |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 57 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 58 | return $this->CI->db->cache_off(); |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 59 | } |
| 60 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 61 | $path = $this->CI->db->cachedir; |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 62 | } |
| 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 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 69 | if ($this->CI->db->db_debug) |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 70 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 71 | return $this->CI->db->display_error('db_invalid_cache_path'); |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // If the path is wrong we'll turn off caching |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 75 | return $this->CI->db->cache_off(); |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 76 | } |
| 77 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 78 | $this->CI->db->cachedir = $path; |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 79 | 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 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 97 | return $this->CI->db->cache_off(); |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 98 | } |
| 99 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 100 | $uri = ($this->CI->uri->segment(1) == FALSE) ? 'default.' : $this->CI->uri->segment(1).'.'; |
| 101 | $uri .= ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2); |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 102 | |
admin | a0564c0 | 2006-10-05 22:18:11 +0000 | [diff] [blame] | 103 | $filepath = $uri.'/'.md5($sql); |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 104 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 105 | if (FALSE === ($cachedata = read_file($this->CI->db->cachedir.$filepath))) |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 106 | { |
| 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 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 125 | return $this->CI->db->cache_off(); |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 126 | } |
| 127 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 128 | $uri = ($this->CI->uri->segment(1) == FALSE) ? 'default.' : $this->CI->uri->segment(1).'.'; |
| 129 | $uri .= ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2); |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 130 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 131 | $dir_path = $this->CI->db->cachedir.$uri.'/'; |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 132 | |
| 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 | */ |
admin | e6419c4 | 2006-10-06 01:50:43 +0000 | [diff] [blame] | 162 | function delete($segment_one = '', $segment_two = '') |
| 163 | { |
| 164 | if ($segment_one == '') |
| 165 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 166 | $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(2); |
admin | e6419c4 | 2006-10-06 01:50:43 +0000 | [diff] [blame] | 167 | } |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 168 | |
admin | e6419c4 | 2006-10-06 01:50:43 +0000 | [diff] [blame] | 169 | if ($segment_two == '') |
| 170 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 171 | $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2); |
admin | e6419c4 | 2006-10-06 01:50:43 +0000 | [diff] [blame] | 172 | } |
| 173 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 174 | $dir_path = $this->CI->db->cachedir.md5($segment_one.'.'.$segment_two).'/'; |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 175 | |
| 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 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 189 | delete_files($this->CI->db->cachedir, TRUE); |
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | } |
| 193 | |
| 194 | ?> |