blob: b8f8995fa936f4e30b09972a775bacd7708bd44f [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 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
98 if ( ! is_dir($path) OR ! is_really_writable($path))
99 {
100 // If the path is wrong we'll turn off caching
101 return $this->db->cache_off();
102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 $this->db->cachedir = $path;
105 return TRUE;
106 }
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 // --------------------------------------------------------------------
109
110 /**
111 * Retrieve a cached query
112 *
113 * The URI being requested will become the name of the cache sub-folder.
Andrey Andreevae85eb42012-11-02 01:42:31 +0200114 * An MD5 hash of the SQL statement will become the cache file name.
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300116 * @param string $sql
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 * @return string
118 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200119 public function read($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300121 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
122 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Barry Mienydd671972010-10-04 16:33:58 +0200123 $filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql);
124
Andrey Andreev0f0b7692012-06-07 14:57:04 +0300125 if (FALSE === ($cachedata = file_get_contents($filepath)))
Barry Mienydd671972010-10-04 16:33:58 +0200126 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 return FALSE;
128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
130 return unserialize($cachedata);
131 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000132
133 // --------------------------------------------------------------------
134
135 /**
136 * Write a query to a cache file
137 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300138 * @param string $sql
139 * @param object $object
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 * @return bool
141 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200142 public function write($sql, $object)
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300144 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
145 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 $filename = md5($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 if ( ! @is_dir($dir_path))
150 {
151 if ( ! @mkdir($dir_path, DIR_WRITE_MODE))
152 {
153 return FALSE;
154 }
Barry Mienydd671972010-10-04 16:33:58 +0200155
156 @chmod($dir_path, DIR_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 if (write_file($dir_path.$filename, serialize($object)) === FALSE)
160 {
161 return FALSE;
162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Jones172e1612009-10-13 14:32:48 +0000164 @chmod($dir_path.$filename, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 return TRUE;
166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Delete cache files within a particular directory
172 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200173 * @param string $segment_one
174 * @param string $segment_two
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300175 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200177 public function delete($segment_one = '', $segment_two = '')
Barry Mienydd671972010-10-04 16:33:58 +0200178 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100179 if ($segment_one === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300181 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100184 if ($segment_two === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300186 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 delete_files($dir_path, TRUE);
191 }
192
193 // --------------------------------------------------------------------
194
195 /**
196 * Delete all existing cache files
197 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300198 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200200 public function delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Andrey Andreevfd6c2bc2012-04-03 16:21:48 +0300202 delete_files($this->db->cachedir, TRUE, 0, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 }
204
205}
206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207/* End of file DB_cache.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400208/* Location: ./system/database/DB_cache.php */