blob: 37d2dd0b5beb811456eb2b555f7a6e0cf52e248b [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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 Andreev24276a32012-01-08 02:44:38 +020038 public $CI;
39 public $db; // allows passing of db object so that multiple database connections and returned db objects can be supported
Derek Allard2067d1a2008-11-13 22:59:24 +000040
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030041 /**
42 * Constructor
43 *
Andrey Andreev6df461a2012-10-24 14:57:37 +030044 * @param object &$db
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030045 * @return void
46 */
Andrey Andreev24276a32012-01-08 02:44:38 +020047 public function __construct(&$db)
Derek Allard2067d1a2008-11-13 22:59:24 +000048 {
Andrey Andreev24276a32012-01-08 02:44:38 +020049 // 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 +000050 $this->CI =& get_instance();
51 $this->db =& $db;
Barry Mienydd671972010-10-04 16:33:58 +020052 $this->CI->load->helper('file');
Andrey Andreev256a18c2012-10-23 12:18:32 +030053
54 $this->check_path();
Derek Allard2067d1a2008-11-13 22:59:24 +000055 }
56
57 // --------------------------------------------------------------------
58
59 /**
60 * Set Cache Directory Path
61 *
Derek Allard2067d1a2008-11-13 22:59:24 +000062 * @param string the path to the cache directory
63 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020064 */
Andrey Andreev24276a32012-01-08 02:44:38 +020065 public function check_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000066 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +010067 if ($path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000068 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +010069 if ($this->db->cachedir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000070 {
71 return $this->db->cache_off();
72 }
Barry Mienydd671972010-10-04 16:33:58 +020073
Derek Allard2067d1a2008-11-13 22:59:24 +000074 $path = $this->db->cachedir;
75 }
Barry Mienydd671972010-10-04 16:33:58 +020076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 // Add a trailing slash to the path if needed
Andrey Andreev256a18c2012-10-23 12:18:32 +030078 $path = realpath($path)
79 ? rtrim(realpath($path), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR
80 : rtrim($path, '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +000081
82 if ( ! is_dir($path) OR ! is_really_writable($path))
83 {
84 // If the path is wrong we'll turn off caching
85 return $this->db->cache_off();
86 }
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Allard2067d1a2008-11-13 22:59:24 +000088 $this->db->cachedir = $path;
89 return TRUE;
90 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 // --------------------------------------------------------------------
93
94 /**
95 * Retrieve a cached query
96 *
97 * The URI being requested will become the name of the cache sub-folder.
98 * An MD5 hash of the SQL statement will become the cache file name
99 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300100 * @param string $sql
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 * @return string
102 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200103 public function read($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300105 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
106 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Barry Mienydd671972010-10-04 16:33:58 +0200107 $filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql);
108
Andrey Andreev0f0b7692012-06-07 14:57:04 +0300109 if (FALSE === ($cachedata = file_get_contents($filepath)))
Barry Mienydd671972010-10-04 16:33:58 +0200110 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 return FALSE;
112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
114 return unserialize($cachedata);
115 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000116
117 // --------------------------------------------------------------------
118
119 /**
120 * Write a query to a cache file
121 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300122 * @param string $sql
123 * @param object $object
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 * @return bool
125 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200126 public function write($sql, $object)
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300128 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
129 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 $filename = md5($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 if ( ! @is_dir($dir_path))
134 {
135 if ( ! @mkdir($dir_path, DIR_WRITE_MODE))
136 {
137 return FALSE;
138 }
Barry Mienydd671972010-10-04 16:33:58 +0200139
140 @chmod($dir_path, DIR_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 if (write_file($dir_path.$filename, serialize($object)) === FALSE)
144 {
145 return FALSE;
146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Jones172e1612009-10-13 14:32:48 +0000148 @chmod($dir_path.$filename, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 return TRUE;
150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * Delete cache files within a particular directory
156 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300157 * @param string $segment_one = ''
158 * @param string $segment_two = ''
159 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200161 public function delete($segment_one = '', $segment_two = '')
Barry Mienydd671972010-10-04 16:33:58 +0200162 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100163 if ($segment_one === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300165 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100168 if ($segment_two === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300170 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 delete_files($dir_path, TRUE);
175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * Delete all existing cache files
181 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300182 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200184 public function delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Andrey Andreevfd6c2bc2012-04-03 16:21:48 +0300186 delete_files($this->db->cachedir, TRUE, 0, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
188
189}
190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191/* End of file DB_cache.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400192/* Location: ./system/database/DB_cache.php */