Derek Allard | d2df9bc | 2007-04-15 17:41:17 +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 Rick Ellis
|
| 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Allard | 6838f00 | 2007-10-04 19:29:59 +0000 | [diff] [blame^] | 10 | * @license http://www.codeigniter.com/user_guide/license.html
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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 |
|
| 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);
|
| 66 |
|
| 67 | if ( ! is_dir($path) OR ! is_writable($path))
|
| 68 | {
|
| 69 | if ($this->CI->db->db_debug)
|
| 70 | {
|
| 71 | return $this->CI->db->display_error('db_invalid_cache_path');
|
| 72 | }
|
| 73 |
|
| 74 | // If the path is wrong we'll turn off caching
|
| 75 | return $this->CI->db->cache_off();
|
| 76 | }
|
| 77 |
|
| 78 | $this->CI->db->cachedir = $path;
|
| 79 | return TRUE;
|
| 80 | }
|
| 81 |
|
| 82 | // --------------------------------------------------------------------
|
| 83 |
|
| 84 | /**
|
| 85 | * Retrieve 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 | {
|
| 97 | return $this->CI->db->cache_off();
|
| 98 | }
|
| 99 |
|
| 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);
|
| 102 |
|
| 103 | $filepath = $uri.'/'.md5($sql);
|
| 104 |
|
| 105 | if (FALSE === ($cachedata = read_file($this->CI->db->cachedir.$filepath)))
|
| 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 | {
|
| 125 | return $this->CI->db->cache_off();
|
| 126 | }
|
| 127 |
|
| 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);
|
| 130 |
|
| 131 | $dir_path = $this->CI->db->cachedir.$uri.'/';
|
| 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 | */
|
| 162 | function delete($segment_one = '', $segment_two = '')
|
| 163 | {
|
| 164 | if ($segment_one == '')
|
| 165 | {
|
| 166 | $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(2);
|
| 167 | }
|
| 168 |
|
| 169 | if ($segment_two == '')
|
| 170 | {
|
| 171 | $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
|
| 172 | }
|
| 173 |
|
| 174 | $dir_path = $this->CI->db->cachedir.$segment_one.'+'.$segment_two.'/';
|
| 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 | {
|
| 189 | delete_files($this->CI->db->cachedir, TRUE);
|
| 190 | }
|
| 191 |
|
| 192 | }
|
| 193 |
|
admin | 7e75a7e | 2006-10-04 18:43:24 +0000 | [diff] [blame] | 194 | ?> |