blob: 79651fcb072930b90eaec62caddeee83e31d7b4a [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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 Andreev24276a32012-01-08 02:44:38 +020040 public function __construct(&$db)
Derek Allard2067d1a2008-11-13 22:59:24 +000041 {
Andrey Andreev24276a32012-01-08 02:44:38 +020042 // 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 +000043 $this->CI =& get_instance();
44 $this->db =& $db;
Barry Mienydd671972010-10-04 16:33:58 +020045 $this->CI->load->helper('file');
Derek Allard2067d1a2008-11-13 22:59:24 +000046 }
47
48 // --------------------------------------------------------------------
49
50 /**
51 * Set Cache Directory Path
52 *
Derek Allard2067d1a2008-11-13 22:59:24 +000053 * @param string the path to the cache directory
54 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020055 */
Andrey Andreev24276a32012-01-08 02:44:38 +020056 public function check_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000057 {
58 if ($path == '')
59 {
60 if ($this->db->cachedir == '')
61 {
62 return $this->db->cache_off();
63 }
Barry Mienydd671972010-10-04 16:33:58 +020064
Derek Allard2067d1a2008-11-13 22:59:24 +000065 $path = $this->db->cachedir;
66 }
Barry Mienydd671972010-10-04 16:33:58 +020067
Derek Allard2067d1a2008-11-13 22:59:24 +000068 // Add a trailing slash to the path if needed
Andrey Andreev24276a32012-01-08 02:44:38 +020069 $path = preg_replace('/(.+?)\/*$/', '\\1/', $path);
Derek Allard2067d1a2008-11-13 22:59:24 +000070
71 if ( ! is_dir($path) OR ! is_really_writable($path))
72 {
73 // If the path is wrong we'll turn off caching
74 return $this->db->cache_off();
75 }
Barry Mienydd671972010-10-04 16:33:58 +020076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 $this->db->cachedir = $path;
78 return TRUE;
79 }
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Allard2067d1a2008-11-13 22:59:24 +000081 // --------------------------------------------------------------------
82
83 /**
84 * Retrieve a cached query
85 *
86 * The URI being requested will become the name of the cache sub-folder.
87 * An MD5 hash of the SQL statement will become the cache file name
88 *
Derek Allard2067d1a2008-11-13 22:59:24 +000089 * @return string
90 */
Andrey Andreev24276a32012-01-08 02:44:38 +020091 public function read($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
93 if ( ! $this->check_path())
94 {
95 return $this->db->cache_off();
96 }
97
98 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
Derek Allard2067d1a2008-11-13 22:59:24 +000099 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Barry Mienydd671972010-10-04 16:33:58 +0200100 $filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql);
101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 if (FALSE === ($cachedata = read_file($filepath)))
Barry Mienydd671972010-10-04 16:33:58 +0200103 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 return FALSE;
105 }
Barry Mienydd671972010-10-04 16:33:58 +0200106
107 return unserialize($cachedata);
108 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000109
110 // --------------------------------------------------------------------
111
112 /**
113 * Write a query to a cache file
114 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 * @return bool
116 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200117 public function write($sql, $object)
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
119 if ( ! $this->check_path())
120 {
121 return $this->db->cache_off();
122 }
123
124 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 $filename = md5($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 if ( ! @is_dir($dir_path))
130 {
131 if ( ! @mkdir($dir_path, DIR_WRITE_MODE))
132 {
133 return FALSE;
134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
136 @chmod($dir_path, DIR_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 if (write_file($dir_path.$filename, serialize($object)) === FALSE)
140 {
141 return FALSE;
142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Jones172e1612009-10-13 14:32:48 +0000144 @chmod($dir_path.$filename, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 return TRUE;
146 }
147
148 // --------------------------------------------------------------------
149
150 /**
151 * Delete cache files within a particular directory
152 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 * @return bool
154 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200155 public function delete($segment_one = '', $segment_two = '')
Barry Mienydd671972010-10-04 16:33:58 +0200156 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 if ($segment_one == '')
158 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500159 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 if ($segment_two == '')
163 {
164 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 delete_files($dir_path, TRUE);
169 }
170
171 // --------------------------------------------------------------------
172
173 /**
174 * Delete all existing cache files
175 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 * @return bool
177 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200178 public function delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
180 delete_files($this->db->cachedir, TRUE);
181 }
182
183}
184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185/* End of file DB_cache.php */
Andrey Andreev24276a32012-01-08 02:44:38 +0200186/* Location: ./system/database/DB_cache.php */