blob: bdd91867a5d351f25ad5a5574df9aca5d78e4371 [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 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');
Andrey Andreev256a18c2012-10-23 12:18:32 +030046
47 $this->check_path();
Derek Allard2067d1a2008-11-13 22:59:24 +000048 }
49
50 // --------------------------------------------------------------------
51
52 /**
53 * Set Cache Directory Path
54 *
Derek Allard2067d1a2008-11-13 22:59:24 +000055 * @param string the path to the cache directory
56 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020057 */
Andrey Andreev24276a32012-01-08 02:44:38 +020058 public function check_path($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000059 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +010060 if ($path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000061 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +010062 if ($this->db->cachedir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000063 {
64 return $this->db->cache_off();
65 }
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Allard2067d1a2008-11-13 22:59:24 +000067 $path = $this->db->cachedir;
68 }
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070 // Add a trailing slash to the path if needed
Andrey Andreev256a18c2012-10-23 12:18:32 +030071 $path = realpath($path)
72 ? rtrim(realpath($path), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR
73 : rtrim($path, '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +000074
75 if ( ! is_dir($path) OR ! is_really_writable($path))
76 {
77 // If the path is wrong we'll turn off caching
78 return $this->db->cache_off();
79 }
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Allard2067d1a2008-11-13 22:59:24 +000081 $this->db->cachedir = $path;
82 return TRUE;
83 }
Barry Mienydd671972010-10-04 16:33:58 +020084
Derek Allard2067d1a2008-11-13 22:59:24 +000085 // --------------------------------------------------------------------
86
87 /**
88 * Retrieve a cached query
89 *
90 * The URI being requested will become the name of the cache sub-folder.
91 * An MD5 hash of the SQL statement will become the cache file name
92 *
Derek Allard2067d1a2008-11-13 22:59:24 +000093 * @return string
94 */
Andrey Andreev24276a32012-01-08 02:44:38 +020095 public function read($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +000096 {
Andrey Andreeve4c30192012-06-04 15:08:24 +030097 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
98 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Barry Mienydd671972010-10-04 16:33:58 +020099 $filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql);
100
Andrey Andreev0f0b7692012-06-07 14:57:04 +0300101 if (FALSE === ($cachedata = file_get_contents($filepath)))
Barry Mienydd671972010-10-04 16:33:58 +0200102 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 return FALSE;
104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
106 return unserialize($cachedata);
107 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000108
109 // --------------------------------------------------------------------
110
111 /**
112 * Write a query to a cache file
113 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 * @return bool
115 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200116 public function write($sql, $object)
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300118 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
119 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 $filename = md5($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 if ( ! @is_dir($dir_path))
124 {
125 if ( ! @mkdir($dir_path, DIR_WRITE_MODE))
126 {
127 return FALSE;
128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
130 @chmod($dir_path, DIR_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 if (write_file($dir_path.$filename, serialize($object)) === FALSE)
134 {
135 return FALSE;
136 }
Barry Mienydd671972010-10-04 16:33:58 +0200137
Derek Jones172e1612009-10-13 14:32:48 +0000138 @chmod($dir_path.$filename, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 return TRUE;
140 }
141
142 // --------------------------------------------------------------------
143
144 /**
145 * Delete cache files within a particular directory
146 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 * @return bool
148 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200149 public function delete($segment_one = '', $segment_two = '')
Barry Mienydd671972010-10-04 16:33:58 +0200150 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100151 if ($segment_one === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300153 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
Barry Mienydd671972010-10-04 16:33:58 +0200155
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100156 if ($segment_two === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300158 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 delete_files($dir_path, TRUE);
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Delete all existing cache files
169 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 * @return bool
171 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200172 public function delete_all()
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
Andrey Andreevfd6c2bc2012-04-03 16:21:48 +0300174 delete_files($this->db->cachedir, TRUE, 0, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
176
177}
178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179/* End of file DB_cache.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400180/* Location: ./system/database/DB_cache.php */