blob: 671147b3dd23648b9bbd6da5263b801f3b741b72 [file] [log] [blame]
Andrey Andreev24276a32012-01-08 02:44:38 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Database Cache Class
30 *
31 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050032 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * @link http://codeigniter.com/user_guide/database/
34 */
35class CI_DB_Cache {
36
Andrey Andreev24276a32012-01-08 02:44:38 +020037 public $CI;
38 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 +000039
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030040 /**
41 * Constructor
42 *
43 * @param &$db
44 * @return void
45 */
Andrey Andreev24276a32012-01-08 02:44:38 +020046 public function __construct(&$db)
Derek Allard2067d1a2008-11-13 22:59:24 +000047 {
Andrey Andreev24276a32012-01-08 02:44:38 +020048 // 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 +000049 $this->CI =& get_instance();
50 $this->db =& $db;
Barry Mienydd671972010-10-04 16:33:58 +020051 $this->CI->load->helper('file');
Andrey Andreev256a18c2012-10-23 12:18:32 +030052
53 $this->check_path();
Derek Allard2067d1a2008-11-13 22:59:24 +000054 }
55
56 // --------------------------------------------------------------------
57
58 /**
59 * Set Cache Directory Path
60 *
Derek Allard2067d1a2008-11-13 22:59:24 +000061 * @param string the path to the cache directory
62 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020063 */
Andrey Andreev24276a32012-01-08 02:44:38 +020064 public function check_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000065 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +010066 if ($path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000067 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +010068 if ($this->db->cachedir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000069 {
70 return $this->db->cache_off();
71 }
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 $path = $this->db->cachedir;
74 }
Barry Mienydd671972010-10-04 16:33:58 +020075
Derek Allard2067d1a2008-11-13 22:59:24 +000076 // Add a trailing slash to the path if needed
Andrey Andreev256a18c2012-10-23 12:18:32 +030077 $path = realpath($path)
78 ? rtrim(realpath($path), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR
79 : rtrim($path, '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +000080
81 if ( ! is_dir($path) OR ! is_really_writable($path))
82 {
83 // If the path is wrong we'll turn off caching
84 return $this->db->cache_off();
85 }
Barry Mienydd671972010-10-04 16:33:58 +020086
Derek Allard2067d1a2008-11-13 22:59:24 +000087 $this->db->cachedir = $path;
88 return TRUE;
89 }
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 // --------------------------------------------------------------------
92
93 /**
94 * Retrieve a cached query
95 *
96 * The URI being requested will become the name of the cache sub-folder.
97 * An MD5 hash of the SQL statement will become the cache file name
98 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030099 * @param string $sql
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 * @return string
101 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200102 public function read($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300104 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
105 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Barry Mienydd671972010-10-04 16:33:58 +0200106 $filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql);
107
Andrey Andreev0f0b7692012-06-07 14:57:04 +0300108 if (FALSE === ($cachedata = file_get_contents($filepath)))
Barry Mienydd671972010-10-04 16:33:58 +0200109 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 return FALSE;
111 }
Barry Mienydd671972010-10-04 16:33:58 +0200112
113 return unserialize($cachedata);
114 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000115
116 // --------------------------------------------------------------------
117
118 /**
119 * Write a query to a cache file
120 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300121 * @param string $sql
122 * @param object $object
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @return bool
124 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200125 public function write($sql, $object)
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300127 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
128 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 $filename = md5($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 if ( ! @is_dir($dir_path))
133 {
134 if ( ! @mkdir($dir_path, DIR_WRITE_MODE))
135 {
136 return FALSE;
137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
139 @chmod($dir_path, DIR_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 if (write_file($dir_path.$filename, serialize($object)) === FALSE)
143 {
144 return FALSE;
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Jones172e1612009-10-13 14:32:48 +0000147 @chmod($dir_path.$filename, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 return TRUE;
149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * Delete cache files within a particular directory
155 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300156 * @param string $segment_one = ''
157 * @param string $segment_two = ''
158 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200160 public function delete($segment_one = '', $segment_two = '')
Barry Mienydd671972010-10-04 16:33:58 +0200161 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100162 if ($segment_one === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300164 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100167 if ($segment_two === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300169 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 delete_files($dir_path, TRUE);
174 }
175
176 // --------------------------------------------------------------------
177
178 /**
179 * Delete all existing cache files
180 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300181 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200183 public function delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
Andrey Andreevfd6c2bc2012-04-03 16:21:48 +0300185 delete_files($this->db->cachedir, TRUE, 0, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
187
188}
189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190/* End of file DB_cache.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400191/* Location: ./system/database/DB_cache.php */