blob: 53f9f81a797630820ded3a9bfa79d3bb71573575 [file] [log] [blame]
Andrey Andreev7d4ea072011-12-25 19:23:50 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Greg Akerbde25d92010-12-21 09:31:21 -06002/**
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 Jonesf4a4bd82011-10-20 12:18:42 -05006 *
7 * NOTICE OF LICENSE
Andrey Andreev7d4ea072011-12-25 19:23:50 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev7d4ea072011-12-25 19:23:50 +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.
Greg Akerbde25d92010-12-21 09:31:21 -060018 *
19 * @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) 2006 - 2012 EllisLab, Inc.
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Greg Akerbde25d92010-12-21 09:31:21 -060023 * @link http://codeigniter.com
24 * @since Version 2.0
Andrey Andreev7d4ea072011-12-25 19:23:50 +020025 * @filesource
Greg Akerbde25d92010-12-21 09:31:21 -060026 */
27
Greg Akerbde25d92010-12-21 09:31:21 -060028/**
Andrey Andreev7d4ea072011-12-25 19:23:50 +020029 * CodeIgniter Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060030 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Core
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Andrey Andreev7d4ea072011-12-25 19:23:50 +020035 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060036 */
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010037class CI_Cache extends CI_Driver_Library {
Andrey Andreev7d4ea072011-12-25 19:23:50 +020038
Timothy Warren0688ac92012-04-20 10:25:04 -040039 /**
40 * Valid cache drivers
41 *
42 * @var array
43 */
44 protected $valid_drivers = array(
45 'cache_apc',
46 'cache_file',
47 'cache_memcached',
48 'cache_dummy',
49 'cache_wincache'
50 );
Greg Akerbde25d92010-12-21 09:31:21 -060051
Timothy Warren0688ac92012-04-20 10:25:04 -040052 /**
53 * Path of cache files (if file-based cache)
54 *
55 * @var string
56 */
57 protected $_cache_path = NULL;
Andrey Andreev56454792012-05-17 14:32:19 +030058
Timothy Warren0688ac92012-04-20 10:25:04 -040059 /**
60 * Reference to the driver
61 *
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040062 * @var mixed
Timothy Warren0688ac92012-04-20 10:25:04 -040063 */
64 protected $_adapter = 'dummy';
Andrey Andreev56454792012-05-17 14:32:19 +030065
Timothy Warren0688ac92012-04-20 10:25:04 -040066 /**
67 * Fallback driver
68 *
69 * @param string
70 */
Eric Robertsbf50a3b2012-05-27 19:04:43 -050071 protected $_backup_driver = 'dummy';
Andrey Andreev7d4ea072011-12-25 19:23:50 +020072
Greg Akerbde25d92010-12-21 09:31:21 -060073 /**
Andrey Andreevb24b0332012-03-26 15:34:39 +030074 * Constructor
Greg Akerbde25d92010-12-21 09:31:21 -060075 *
76 * Initialize class properties based on the configuration array.
77 *
Andrey Andreev7d4ea072011-12-25 19:23:50 +020078 * @param array
Andrey Andreevb24b0332012-03-26 15:34:39 +030079 * @return void
Greg Akerbde25d92010-12-21 09:31:21 -060080 */
Andrey Andreevb24b0332012-03-26 15:34:39 +030081 public function __construct($config = array())
Greg Aker03abee32011-12-25 00:31:29 -060082 {
Greg Akerbde25d92010-12-21 09:31:21 -060083 $default_config = array(
Timothy Warren0688ac92012-04-20 10:25:04 -040084 'adapter',
85 'memcached'
86 );
Greg Akerbde25d92010-12-21 09:31:21 -060087
88 foreach ($default_config as $key)
89 {
90 if (isset($config[$key]))
91 {
92 $param = '_'.$key;
93
94 $this->{$param} = $config[$key];
95 }
96 }
97
98 if (isset($config['backup']))
99 {
100 if (in_array('cache_'.$config['backup'], $this->valid_drivers))
101 {
102 $this->_backup_driver = $config['backup'];
103 }
104 }
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500105
106 // If the specified adapter isn't available, check the backup.
107 if ( ! $this->is_supported($this->_adapter))
108 {
109 if ( ! $this->is_supported($this->_backup_driver))
110 {
111 // Backup isn't supported either. Default to 'Dummy' driver.
112 log_message('error', 'Cache adapter "'.$this->_adapter.'" and backup "'.$this->_backup_driver.'" are both unavailable. Cache is now using "Dummy" adapter.');
113 $this->_adapter = 'dummy';
114 }
115 else
116 {
117 // Backup is supported. Set it to primary.
118 $this->_adapter = $this->_backup_driver;
119 }
120 }
Greg Akerbde25d92010-12-21 09:31:21 -0600121 }
122
123 // ------------------------------------------------------------------------
124
125 /**
Andrey Andreevb24b0332012-03-26 15:34:39 +0300126 * Get
127 *
128 * Look for a value in the cache. If it exists, return the data
129 * if not, return FALSE
130 *
131 * @param string
132 * @return mixed value that is stored/FALSE on failure
133 */
134 public function get($id)
135 {
136 return $this->{$this->_adapter}->get($id);
137 }
138
139 // ------------------------------------------------------------------------
140
141 /**
142 * Cache Save
143 *
144 * @param string Unique Key
145 * @param mixed Data to store
146 * @param int Length of time (in seconds) to cache the data
147 * @return bool true on success/false on failure
148 */
149 public function save($id, $data, $ttl = 60)
150 {
151 return $this->{$this->_adapter}->save($id, $data, $ttl);
152 }
153
154 // ------------------------------------------------------------------------
155
156 /**
157 * Delete from Cache
158 *
159 * @param mixed unique identifier of the item in the cache
160 * @return bool true on success/false on failure
161 */
162 public function delete($id)
163 {
164 return $this->{$this->_adapter}->delete($id);
165 }
166
167 // ------------------------------------------------------------------------
168
169 /**
170 * Clean the cache
171 *
172 * @return bool false on failure/true on success
173 */
174 public function clean()
175 {
176 return $this->{$this->_adapter}->clean();
177 }
178
179 // ------------------------------------------------------------------------
180
181 /**
182 * Cache Info
183 *
184 * @param string user/filehits
185 * @return mixed array on success, false on failure
186 */
187 public function cache_info($type = 'user')
188 {
189 return $this->{$this->_adapter}->cache_info($type);
190 }
191
192 // ------------------------------------------------------------------------
193
194 /**
195 * Get Cache Metadata
196 *
197 * @param mixed key to get cache metadata on
198 * @return mixed return value from child method
199 */
200 public function get_metadata($id)
201 {
202 return $this->{$this->_adapter}->get_metadata($id);
203 }
204
205 // ------------------------------------------------------------------------
206
207 /**
Greg Akerbde25d92010-12-21 09:31:21 -0600208 * Is the requested driver supported in this environment?
209 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300210 * @param string The driver to test.
211 * @return array
Greg Akerbde25d92010-12-21 09:31:21 -0600212 */
213 public function is_supported($driver)
214 {
215 static $support = array();
216
217 if ( ! isset($support[$driver]))
218 {
219 $support[$driver] = $this->{$driver}->is_supported();
220 }
221
222 return $support[$driver];
223 }
224
Greg Akerbde25d92010-12-21 09:31:21 -0600225}
Greg Akerbde25d92010-12-21 09:31:21 -0600226
227/* End of file Cache.php */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300228/* Location: ./system/libraries/Cache/Cache.php */