Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Database Cache Class |
| 31 | * |
| 32 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 33 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 34 | * @link http://codeigniter.com/user_guide/database/ |
| 35 | */ |
| 36 | class CI_DB_Cache { |
| 37 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 38 | /** |
| 39 | * CI Singleton |
| 40 | * |
| 41 | * @var object |
| 42 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 43 | public $CI; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Database object |
| 47 | * |
| 48 | * Allows passing of DB object so that multiple database connections |
| 49 | * and returned DB objects can be supported. |
| 50 | * |
| 51 | * @var object |
| 52 | */ |
| 53 | public $db; |
| 54 | |
| 55 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 57 | /** |
| 58 | * Constructor |
| 59 | * |
Andrey Andreev | 6df461a | 2012-10-24 14:57:37 +0300 | [diff] [blame] | 60 | * @param object &$db |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 61 | * @return void |
| 62 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 63 | public function __construct(&$db) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 65 | // Assign the main CI object to $this->CI and load the file helper since we use it a lot |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | $this->CI =& get_instance(); |
| 67 | $this->db =& $db; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 68 | $this->CI->load->helper('file'); |
Andrey Andreev | 256a18c | 2012-10-23 12:18:32 +0300 | [diff] [blame] | 69 | |
| 70 | $this->check_path(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // -------------------------------------------------------------------- |
| 74 | |
| 75 | /** |
| 76 | * Set Cache Directory Path |
| 77 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 78 | * @param string $path Path to the cache directory |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 80 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 81 | public function check_path($path = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 83 | if ($path === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 85 | if ($this->db->cachedir === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | { |
| 87 | return $this->db->cache_off(); |
| 88 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 89 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | $path = $this->db->cachedir; |
| 91 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 92 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | // Add a trailing slash to the path if needed |
Andrey Andreev | 256a18c | 2012-10-23 12:18:32 +0300 | [diff] [blame] | 94 | $path = realpath($path) |
| 95 | ? rtrim(realpath($path), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR |
| 96 | : rtrim($path, '/').'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | |
Brian Gottier | 51c0b55 | 2012-11-13 18:47:19 -0800 | [diff] [blame] | 98 | if ( ! is_dir($path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 99 | { |
Brian Gottier | 4a18c95 | 2012-11-14 07:40:55 -0800 | [diff] [blame] | 100 | log_message('debug', 'DB cache path error: '.$path); |
Brian Gottier | 51c0b55 | 2012-11-13 18:47:19 -0800 | [diff] [blame] | 101 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | // If the path is wrong we'll turn off caching |
| 103 | return $this->db->cache_off(); |
| 104 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 105 | |
Brian Gottier | 51c0b55 | 2012-11-13 18:47:19 -0800 | [diff] [blame] | 106 | if ( ! is_really_writable($path)) |
| 107 | { |
Brian Gottier | 4a18c95 | 2012-11-14 07:40:55 -0800 | [diff] [blame] | 108 | log_message('debug', 'DB cache dir not writable: '.$path); |
Brian Gottier | 51c0b55 | 2012-11-13 18:47:19 -0800 | [diff] [blame] | 109 | |
| 110 | // If the path is not really writable we'll turn off caching |
| 111 | return $this->db->cache_off(); |
| 112 | } |
| 113 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | $this->db->cachedir = $path; |
| 115 | return TRUE; |
| 116 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 117 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | // -------------------------------------------------------------------- |
| 119 | |
| 120 | /** |
| 121 | * Retrieve a cached query |
| 122 | * |
| 123 | * The URI being requested will become the name of the cache sub-folder. |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 124 | * An MD5 hash of the SQL statement will become the cache file name. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 126 | * @param string $sql |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | * @return string |
| 128 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 129 | public function read($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | { |
Andrey Andreev | e4c3019 | 2012-06-04 15:08:24 +0300 | [diff] [blame] | 131 | $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1); |
| 132 | $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] | 133 | $filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql); |
| 134 | |
Andrey Andreev | 83b2b1c | 2012-11-13 10:58:38 +0200 | [diff] [blame] | 135 | if (FALSE === ($cachedata = @file_get_contents($filepath))) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 136 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | return FALSE; |
| 138 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 139 | |
| 140 | return unserialize($cachedata); |
| 141 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | |
| 143 | // -------------------------------------------------------------------- |
| 144 | |
| 145 | /** |
| 146 | * Write a query to a cache file |
| 147 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 148 | * @param string $sql |
| 149 | * @param object $object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | * @return bool |
| 151 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 152 | public function write($sql, $object) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | { |
Andrey Andreev | e4c3019 | 2012-06-04 15:08:24 +0300 | [diff] [blame] | 154 | $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1); |
| 155 | $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | $filename = md5($sql); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame^] | 159 | if ( ! is_dir($dir_path) && ! @mkdir($dir_path, 0750)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | { |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame^] | 161 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | if (write_file($dir_path.$filename, serialize($object)) === FALSE) |
| 165 | { |
| 166 | return FALSE; |
| 167 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 168 | |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame^] | 169 | chmod($dir_path.$filename, 0640); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | return TRUE; |
| 171 | } |
| 172 | |
| 173 | // -------------------------------------------------------------------- |
| 174 | |
| 175 | /** |
| 176 | * Delete cache files within a particular directory |
| 177 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 178 | * @param string $segment_one |
| 179 | * @param string $segment_two |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 180 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 182 | public function delete($segment_one = '', $segment_two = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 183 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 184 | if ($segment_one === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | { |
Andrey Andreev | e4c3019 | 2012-06-04 15:08:24 +0300 | [diff] [blame] | 186 | $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 188 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 189 | if ($segment_two === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | { |
Andrey Andreev | e4c3019 | 2012-06-04 15:08:24 +0300 | [diff] [blame] | 191 | $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | delete_files($dir_path, TRUE); |
| 196 | } |
| 197 | |
| 198 | // -------------------------------------------------------------------- |
| 199 | |
| 200 | /** |
| 201 | * Delete all existing cache files |
| 202 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 203 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 205 | public function delete_all() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | { |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 207 | delete_files($this->db->cachedir, TRUE, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | } |
| 211 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | /* End of file DB_cache.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 213 | /* Location: ./system/database/DB_cache.php */ |