blob: 48bd9581a745a04fa993f7da93c641a1bbf0f3c5 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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
Andrey Andreevc5536aa2012-11-01 17:33:58 +020021 * @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)
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Greg Akerbde25d92010-12-21 09:31:21 -060028
Greg Akerbde25d92010-12-21 09:31:21 -060029/**
Andrey Andreev7d4ea072011-12-25 19:23:50 +020030 * CodeIgniter Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060031 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Core
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Andrey Andreev7d4ea072011-12-25 19:23:50 +020036 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060037 */
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010038class CI_Cache extends CI_Driver_Library {
Andrey Andreev7d4ea072011-12-25 19:23:50 +020039
Timothy Warren0688ac92012-04-20 10:25:04 -040040 /**
41 * Valid cache drivers
42 *
43 * @var array
44 */
Andrey Andreev58c2b102012-10-26 14:42:29 +030045 protected $valid_drivers = array(
dchill426262d052012-11-24 18:41:13 -050046 'apc',
47 'dummy',
48 'file',
49 'memcached',
50 'redis',
51 'wincache'
Anton Lindqvist30930422012-04-25 12:32:58 +020052 );
Greg Akerbde25d92010-12-21 09:31:21 -060053
Timothy Warren0688ac92012-04-20 10:25:04 -040054 /**
55 * Path of cache files (if file-based cache)
56 *
57 * @var string
58 */
59 protected $_cache_path = NULL;
Andrey Andreev56454792012-05-17 14:32:19 +030060
Timothy Warren0688ac92012-04-20 10:25:04 -040061 /**
62 * Reference to the driver
63 *
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040064 * @var mixed
Timothy Warren0688ac92012-04-20 10:25:04 -040065 */
66 protected $_adapter = 'dummy';
Andrey Andreev56454792012-05-17 14:32:19 +030067
Timothy Warren0688ac92012-04-20 10:25:04 -040068 /**
69 * Fallback driver
70 *
Andrey Andreev58c2b102012-10-26 14:42:29 +030071 * @var string
Timothy Warren0688ac92012-04-20 10:25:04 -040072 */
Eric Robertsbf50a3b2012-05-27 19:04:43 -050073 protected $_backup_driver = 'dummy';
Andrey Andreev7d4ea072011-12-25 19:23:50 +020074
Greg Akerbde25d92010-12-21 09:31:21 -060075 /**
Andrey Andreev58c2b102012-10-26 14:42:29 +030076 * Cache key prefix
77 *
78 * @var string
79 */
80 public $key_prefix = '';
81
82 /**
Greg Akerbde25d92010-12-21 09:31:21 -060083 * Constructor
84 *
Greg Akerbde25d92010-12-21 09:31:21 -060085 * Initialize class properties based on the configuration array.
86 *
Andrey Andreev58c2b102012-10-26 14:42:29 +030087 * @param array $config = array()
Andrey Andreevb24b0332012-03-26 15:34:39 +030088 * @return void
Greg Akerbde25d92010-12-21 09:31:21 -060089 */
Andrey Andreevb24b0332012-03-26 15:34:39 +030090 public function __construct($config = array())
Greg Aker03abee32011-12-25 00:31:29 -060091 {
Greg Akerbde25d92010-12-21 09:31:21 -060092 $default_config = array(
Timothy Warren0688ac92012-04-20 10:25:04 -040093 'adapter',
94 'memcached'
95 );
Greg Akerbde25d92010-12-21 09:31:21 -060096
97 foreach ($default_config as $key)
98 {
99 if (isset($config[$key]))
100 {
101 $param = '_'.$key;
102
103 $this->{$param} = $config[$key];
104 }
105 }
106
vkeranov94b1f762012-10-27 18:19:59 +0300107 isset($config['key_prefix']) && $this->key_prefix = $config['key_prefix'];
Andrey Andreev58c2b102012-10-26 14:42:29 +0300108
109 if (isset($config['backup']) && in_array('cache_'.$config['backup'], $this->valid_drivers))
Greg Akerbde25d92010-12-21 09:31:21 -0600110 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300111 $this->_backup_driver = $config['backup'];
Greg Akerbde25d92010-12-21 09:31:21 -0600112 }
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500113
114 // If the specified adapter isn't available, check the backup.
115 if ( ! $this->is_supported($this->_adapter))
116 {
117 if ( ! $this->is_supported($this->_backup_driver))
118 {
119 // Backup isn't supported either. Default to 'Dummy' driver.
120 log_message('error', 'Cache adapter "'.$this->_adapter.'" and backup "'.$this->_backup_driver.'" are both unavailable. Cache is now using "Dummy" adapter.');
121 $this->_adapter = 'dummy';
122 }
123 else
124 {
125 // Backup is supported. Set it to primary.
126 $this->_adapter = $this->_backup_driver;
127 }
128 }
Greg Akerbde25d92010-12-21 09:31:21 -0600129 }
130
131 // ------------------------------------------------------------------------
132
133 /**
Andrey Andreevb24b0332012-03-26 15:34:39 +0300134 * Get
135 *
136 * Look for a value in the cache. If it exists, return the data
137 * if not, return FALSE
138 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300139 * @param string $id
140 * @return mixed value matching $id or FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300141 */
142 public function get($id)
143 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300144 return $this->{$this->_adapter}->get($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300145 }
146
147 // ------------------------------------------------------------------------
148
149 /**
150 * Cache Save
151 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300152 * @param string $id Cache ID
153 * @param mixed $data Data to store
154 * @param int $ttl = 60 Cache TTL (in seconds)
155 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300156 */
157 public function save($id, $data, $ttl = 60)
158 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300159 return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300160 }
161
162 // ------------------------------------------------------------------------
163
164 /**
165 * Delete from Cache
166 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300167 * @param string $id Cache ID
168 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300169 */
170 public function delete($id)
171 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300172 return $this->{$this->_adapter}->delete($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300173 }
174
175 // ------------------------------------------------------------------------
176
177 /**
178 * Clean the cache
179 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300180 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300181 */
182 public function clean()
183 {
184 return $this->{$this->_adapter}->clean();
185 }
186
187 // ------------------------------------------------------------------------
188
189 /**
190 * Cache Info
191 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300192 * @param string $type = 'user' user/filehits
193 * @return mixed array containing cache info on success OR FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300194 */
195 public function cache_info($type = 'user')
196 {
197 return $this->{$this->_adapter}->cache_info($type);
198 }
199
200 // ------------------------------------------------------------------------
201
202 /**
203 * Get Cache Metadata
204 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300205 * @param string $id key to get cache metadata on
206 * @return mixed cache item metadata
Andrey Andreevb24b0332012-03-26 15:34:39 +0300207 */
208 public function get_metadata($id)
209 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300210 return $this->{$this->_adapter}->get_metadata($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300211 }
212
213 // ------------------------------------------------------------------------
214
215 /**
Greg Akerbde25d92010-12-21 09:31:21 -0600216 * Is the requested driver supported in this environment?
217 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300218 * @param string $driver The driver to test
Andrey Andreevb24b0332012-03-26 15:34:39 +0300219 * @return array
Greg Akerbde25d92010-12-21 09:31:21 -0600220 */
221 public function is_supported($driver)
222 {
223 static $support = array();
224
225 if ( ! isset($support[$driver]))
226 {
227 $support[$driver] = $this->{$driver}->is_supported();
228 }
229
230 return $support[$driver];
231 }
232
Greg Akerbde25d92010-12-21 09:31:21 -0600233}
Greg Akerbde25d92010-12-21 09:31:21 -0600234
235/* End of file Cache.php */
Andrey Andreev9e674f72012-06-09 21:02:52 +0300236/* Location: ./system/libraries/Cache/Cache.php */