blob: 2efb42c5c3a93d2ebe6a6f9e05cfbb2834013959 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev24276a32012-01-08 02:44:38 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev24276a32012-01-08 02:44:38 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Database Cache Class
31 *
32 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050033 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * @link http://codeigniter.com/user_guide/database/
35 */
36class CI_DB_Cache {
37
Andrey Andreevae85eb42012-11-02 01:42:31 +020038 /**
39 * CI Singleton
40 *
41 * @var object
42 */
Andrey Andreev24276a32012-01-08 02:44:38 +020043 public $CI;
Andrey Andreevae85eb42012-11-02 01:42:31 +020044
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 Allard2067d1a2008-11-13 22:59:24 +000056
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030057 /**
58 * Constructor
59 *
Andrey Andreev6df461a2012-10-24 14:57:37 +030060 * @param object &$db
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030061 * @return void
62 */
Andrey Andreev24276a32012-01-08 02:44:38 +020063 public function __construct(&$db)
Derek Allard2067d1a2008-11-13 22:59:24 +000064 {
Andrey Andreev24276a32012-01-08 02:44:38 +020065 // Assign the main CI object to $this->CI and load the file helper since we use it a lot
Derek Allard2067d1a2008-11-13 22:59:24 +000066 $this->CI =& get_instance();
67 $this->db =& $db;
Barry Mienydd671972010-10-04 16:33:58 +020068 $this->CI->load->helper('file');
Andrey Andreev256a18c2012-10-23 12:18:32 +030069
70 $this->check_path();
Derek Allard2067d1a2008-11-13 22:59:24 +000071 }
72
73 // --------------------------------------------------------------------
74
75 /**
76 * Set Cache Directory Path
77 *
Andrey Andreevae85eb42012-11-02 01:42:31 +020078 * @param string $path Path to the cache directory
Derek Allard2067d1a2008-11-13 22:59:24 +000079 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020080 */
Andrey Andreev24276a32012-01-08 02:44:38 +020081 public function check_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000082 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +010083 if ($path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000084 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +010085 if ($this->db->cachedir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
87 return $this->db->cache_off();
88 }
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 $path = $this->db->cachedir;
91 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 // Add a trailing slash to the path if needed
Andrey Andreev256a18c2012-10-23 12:18:32 +030094 $path = realpath($path)
95 ? rtrim(realpath($path), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR
96 : rtrim($path, '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +000097
Brian Gottier51c0b552012-11-13 18:47:19 -080098 if ( ! is_dir($path))
Derek Allard2067d1a2008-11-13 22:59:24 +000099 {
Brian Gottier4a18c952012-11-14 07:40:55 -0800100 log_message('debug', 'DB cache path error: '.$path);
Brian Gottier51c0b552012-11-13 18:47:19 -0800101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 // If the path is wrong we'll turn off caching
103 return $this->db->cache_off();
104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Brian Gottier51c0b552012-11-13 18:47:19 -0800106 if ( ! is_really_writable($path))
107 {
Brian Gottier4a18c952012-11-14 07:40:55 -0800108 log_message('debug', 'DB cache dir not writable: '.$path);
Brian Gottier51c0b552012-11-13 18:47:19 -0800109
110 // If the path is not really writable we'll turn off caching
111 return $this->db->cache_off();
112 }
113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 $this->db->cachedir = $path;
115 return TRUE;
116 }
Barry Mienydd671972010-10-04 16:33:58 +0200117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 // --------------------------------------------------------------------
119
120 /**
121 * Retrieve a cached query
122 *
123 * The URI being requested will become the name of the cache sub-folder.
Andrey Andreevae85eb42012-11-02 01:42:31 +0200124 * An MD5 hash of the SQL statement will become the cache file name.
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300126 * @param string $sql
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 * @return string
128 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200129 public function read($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300131 $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 Mienydd671972010-10-04 16:33:58 +0200133 $filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql);
134
Andrey Andreev83b2b1c2012-11-13 10:58:38 +0200135 if (FALSE === ($cachedata = @file_get_contents($filepath)))
Barry Mienydd671972010-10-04 16:33:58 +0200136 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 return FALSE;
138 }
Barry Mienydd671972010-10-04 16:33:58 +0200139
140 return unserialize($cachedata);
141 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000142
143 // --------------------------------------------------------------------
144
145 /**
146 * Write a query to a cache file
147 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300148 * @param string $sql
149 * @param object $object
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 * @return bool
151 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200152 public function write($sql, $object)
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300154 $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 Allard2067d1a2008-11-13 22:59:24 +0000156 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 $filename = md5($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200158
Andrey Andreev45965742014-08-27 20:40:11 +0300159 if ( ! is_dir($dir_path) && ! @mkdir($dir_path, 0750))
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
Andrey Andreev45965742014-08-27 20:40:11 +0300161 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 if (write_file($dir_path.$filename, serialize($object)) === FALSE)
165 {
166 return FALSE;
167 }
Barry Mienydd671972010-10-04 16:33:58 +0200168
Andrey Andreev45965742014-08-27 20:40:11 +0300169 chmod($dir_path.$filename, 0640);
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 return TRUE;
171 }
172
173 // --------------------------------------------------------------------
174
175 /**
176 * Delete cache files within a particular directory
177 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200178 * @param string $segment_one
179 * @param string $segment_two
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300180 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200182 public function delete($segment_one = '', $segment_two = '')
Barry Mienydd671972010-10-04 16:33:58 +0200183 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100184 if ($segment_one === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300186 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100189 if ($segment_two === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300191 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 delete_files($dir_path, TRUE);
196 }
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Delete all existing cache files
202 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300203 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200205 public function delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200207 delete_files($this->db->cachedir, TRUE, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
209
210}
211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212/* End of file DB_cache.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400213/* Location: ./system/database/DB_cache.php */