Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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
|
Rick Ellis | 9ba2bf2 | 2008-09-12 23:34:39 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008, 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;
|
Derek Jones | d36ade0 | 2008-05-12 15:17:41 +0000 | [diff] [blame] | 28 | var $db; // allows passing of db object so that multiple database connections and returned db objects can be supported
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 29 |
|
| 30 | /**
|
| 31 | * Constructor
|
| 32 | *
|
| 33 | * Grabs the CI super object instance so we can access it.
|
| 34 | *
|
| 35 | */
|
Derek Jones | d36ade0 | 2008-05-12 15:17:41 +0000 | [diff] [blame] | 36 | function CI_DB_Cache(&$db)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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();
|
Derek Jones | d36ade0 | 2008-05-12 15:17:41 +0000 | [diff] [blame] | 41 | $this->db =& $db;
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 42 | $this->CI->load->helper('file');
|
| 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
|
| 53 | */
|
| 54 | function check_path($path = '')
|
| 55 | {
|
| 56 | if ($path == '')
|
| 57 | {
|
Derek Jones | d36ade0 | 2008-05-12 15:17:41 +0000 | [diff] [blame] | 58 | if ($this->db->cachedir == '')
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 59 | {
|
Derek Jones | d36ade0 | 2008-05-12 15:17:41 +0000 | [diff] [blame] | 60 | return $this->db->cache_off();
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 61 | }
|
| 62 |
|
Derek Jones | d36ade0 | 2008-05-12 15:17:41 +0000 | [diff] [blame] | 63 | $path = $this->db->cachedir;
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 64 | }
|
| 65 |
|
| 66 | // Add a trailing slash to the path if needed
|
| 67 | $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 68 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 69 | if ( ! is_dir($path) OR ! is_really_writable($path))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 70 | {
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 71 | // If the path is wrong we'll turn off caching
|
Derek Jones | d36ade0 | 2008-05-12 15:17:41 +0000 | [diff] [blame] | 72 | return $this->db->cache_off();
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 73 | }
|
| 74 |
|
Derek Jones | d36ade0 | 2008-05-12 15:17:41 +0000 | [diff] [blame] | 75 | $this->db->cachedir = $path;
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 76 | return TRUE;
|
| 77 | }
|
| 78 |
|
| 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 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 92 | if ( ! $this->check_path())
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 93 | {
|
Derek Jones | d36ade0 | 2008-05-12 15:17:41 +0000 | [diff] [blame] | 94 | return $this->db->cache_off();
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 95 | }
|
| 96 |
|
| 97 | $uri = ($this->CI->uri->segment(1) == FALSE) ? 'default.' : $this->CI->uri->segment(1).'+';
|
| 98 | $uri .= ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
|
| 99 |
|
| 100 | $filepath = $uri.'/'.md5($sql);
|
| 101 |
|
Derek Jones | d36ade0 | 2008-05-12 15:17:41 +0000 | [diff] [blame] | 102 | if (FALSE === ($cachedata = read_file($this->db->cachedir.$filepath)))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 103 | {
|
| 104 | return FALSE;
|
| 105 | }
|
| 106 |
|
| 107 | return unserialize($cachedata);
|
| 108 | }
|
| 109 |
|
| 110 | // --------------------------------------------------------------------
|
| 111 |
|
| 112 | /**
|
| 113 | * Write a query to a cache file
|
| 114 | *
|
| 115 | * @access public
|
| 116 | * @return bool
|
| 117 | */
|
| 118 | function write($sql, $object)
|
| 119 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 120 | if ( ! $this->check_path())
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 121 | {
|
Derek Jones | d36ade0 | 2008-05-12 15:17:41 +0000 | [diff] [blame] | 122 | return $this->db->cache_off();
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 123 | }
|
| 124 |
|
| 125 | $uri = ($this->CI->uri->segment(1) == FALSE) ? 'default.' : $this->CI->uri->segment(1).'+';
|
| 126 | $uri .= ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
|
| 127 |
|
Derek Jones | d36ade0 | 2008-05-12 15:17:41 +0000 | [diff] [blame] | 128 | $dir_path = $this->db->cachedir.$uri.'/';
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 129 |
|
| 130 | $filename = md5($sql);
|
| 131 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 132 | if ( ! @is_dir($dir_path))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 133 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 134 | if ( ! @mkdir($dir_path, DIR_WRITE_MODE))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 135 | {
|
| 136 | return FALSE;
|
| 137 | }
|
| 138 |
|
Derek Jones | 3ad8efe | 2008-04-04 18:56:04 +0000 | [diff] [blame] | 139 | @chmod($dir_path, DIR_WRITE_MODE);
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 140 | }
|
| 141 |
|
| 142 | if (write_file($dir_path.$filename, serialize($object)) === FALSE)
|
| 143 | {
|
| 144 | return FALSE;
|
| 145 | }
|
| 146 |
|
Derek Jones | 3ad8efe | 2008-04-04 18:56:04 +0000 | [diff] [blame] | 147 | @chmod($dir_path.$filename, DIR_WRITE_MODE);
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 148 | return TRUE;
|
| 149 | }
|
| 150 |
|
| 151 | // --------------------------------------------------------------------
|
| 152 |
|
| 153 | /**
|
| 154 | * Delete cache files within a particular directory
|
| 155 | *
|
| 156 | * @access public
|
| 157 | * @return bool
|
| 158 | */
|
| 159 | function delete($segment_one = '', $segment_two = '')
|
| 160 | {
|
| 161 | if ($segment_one == '')
|
| 162 | {
|
Derek Allard | 85c7c61 | 2008-01-14 13:18:36 +0000 | [diff] [blame] | 163 | $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] | 164 | }
|
| 165 |
|
| 166 | if ($segment_two == '')
|
| 167 | {
|
| 168 | $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
|
| 169 | }
|
| 170 |
|
Derek Jones | d36ade0 | 2008-05-12 15:17:41 +0000 | [diff] [blame] | 171 | $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 172 |
|
| 173 | delete_files($dir_path, TRUE);
|
| 174 | }
|
| 175 |
|
| 176 | // --------------------------------------------------------------------
|
| 177 |
|
| 178 | /**
|
| 179 | * Delete all existing cache files
|
| 180 | *
|
| 181 | * @access public
|
| 182 | * @return bool
|
| 183 | */
|
| 184 | function delete_all()
|
| 185 | {
|
Derek Jones | d36ade0 | 2008-05-12 15:17:41 +0000 | [diff] [blame] | 186 | delete_files($this->db->cachedir, TRUE);
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 187 | }
|
| 188 |
|
| 189 | }
|
| 190 |
|
Derek Jones | d36ade0 | 2008-05-12 15:17:41 +0000 | [diff] [blame] | 191 |
|
| 192 | /* End of file DB_cache.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 193 | /* Location: ./system/database/DB_cache.php */ |