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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 12 | * @since Version 1.0
|
| 13 | * @filesource
|
| 14 | */
|
| 15 |
|
| 16 | // ------------------------------------------------------------------------
|
| 17 |
|
| 18 | /**
|
| 19 | * Database Cache Class
|
| 20 | *
|
| 21 | * @category Database
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 22 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com/user_guide/database/
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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);
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 66 |
|
Derek Allard | 7327499 | 2008-05-05 16:39:18 +0000 | [diff] [blame] | 67 | if (! is_dir($path) OR ! is_really_writable($path))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 68 | {
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 69 | // 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 Allard | 7327499 | 2008-05-05 16:39:18 +0000 | [diff] [blame] | 90 | if (! $this->check_path())
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 91 | {
|
| 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 Allard | 7327499 | 2008-05-05 16:39:18 +0000 | [diff] [blame] | 118 | if (! $this->check_path())
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 119 | {
|
| 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 Allard | 7327499 | 2008-05-05 16:39:18 +0000 | [diff] [blame] | 130 | if (! @is_dir($dir_path))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 131 | {
|
Derek Allard | 7327499 | 2008-05-05 16:39:18 +0000 | [diff] [blame] | 132 | if (! @mkdir($dir_path, DIR_WRITE_MODE))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 133 | {
|
| 134 | return FALSE;
|
| 135 | }
|
| 136 |
|
Derek Jones | 3ad8efe | 2008-04-04 18:56:04 +0000 | [diff] [blame] | 137 | @chmod($dir_path, DIR_WRITE_MODE);
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 138 | }
|
| 139 |
|
| 140 | if (write_file($dir_path.$filename, serialize($object)) === FALSE)
|
| 141 | {
|
| 142 | return FALSE;
|
| 143 | }
|
| 144 |
|
Derek Jones | 3ad8efe | 2008-04-04 18:56:04 +0000 | [diff] [blame] | 145 | @chmod($dir_path.$filename, DIR_WRITE_MODE);
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 146 | 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 Allard | 85c7c61 | 2008-01-14 13:18:36 +0000 | [diff] [blame] | 161 | $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 162 | }
|
| 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 |
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame^] | 189 | |
| 190 | /* End of file DB_cache.php */ |
| 191 | /* Location: ./system/database/DB_cache.php */ |