blob: 537897eaf581ce5a88d1eb86eceea64acd7e5b86 [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 Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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
Tyler Brownell516527c2013-07-24 10:51:04 -0400109 if (isset($config['backup']) && in_array($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.
Tyler Brownell516527c2013-07-24 10:51:04 -0400126 log_message('debug', 'Cache adapter "'.$this->_adapter.'" is unavailable. Falling back to "'.$this->_backup_driver.'" backup adapter.');
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500127 $this->_adapter = $this->_backup_driver;
128 }
129 }
Greg Akerbde25d92010-12-21 09:31:21 -0600130 }
131
132 // ------------------------------------------------------------------------
133
134 /**
Andrey Andreevb24b0332012-03-26 15:34:39 +0300135 * Get
136 *
137 * Look for a value in the cache. If it exists, return the data
138 * if not, return FALSE
139 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300140 * @param string $id
141 * @return mixed value matching $id or FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300142 */
143 public function get($id)
144 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300145 return $this->{$this->_adapter}->get($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300146 }
147
148 // ------------------------------------------------------------------------
149
150 /**
151 * Cache Save
152 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300153 * @param string $id Cache ID
154 * @param mixed $data Data to store
155 * @param int $ttl = 60 Cache TTL (in seconds)
156 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300157 */
158 public function save($id, $data, $ttl = 60)
159 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300160 return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300161 }
162
163 // ------------------------------------------------------------------------
164
165 /**
166 * Delete from Cache
167 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300168 * @param string $id Cache ID
169 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300170 */
171 public function delete($id)
172 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300173 return $this->{$this->_adapter}->delete($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300174 }
175
176 // ------------------------------------------------------------------------
177
178 /**
179 * Clean the cache
180 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300181 * @return bool TRUE on success, FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300182 */
183 public function clean()
184 {
185 return $this->{$this->_adapter}->clean();
186 }
187
188 // ------------------------------------------------------------------------
189
190 /**
191 * Cache Info
192 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300193 * @param string $type = 'user' user/filehits
194 * @return mixed array containing cache info on success OR FALSE on failure
Andrey Andreevb24b0332012-03-26 15:34:39 +0300195 */
196 public function cache_info($type = 'user')
197 {
198 return $this->{$this->_adapter}->cache_info($type);
199 }
200
201 // ------------------------------------------------------------------------
202
203 /**
204 * Get Cache Metadata
205 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300206 * @param string $id key to get cache metadata on
207 * @return mixed cache item metadata
Andrey Andreevb24b0332012-03-26 15:34:39 +0300208 */
209 public function get_metadata($id)
210 {
Andrey Andreev58c2b102012-10-26 14:42:29 +0300211 return $this->{$this->_adapter}->get_metadata($this->key_prefix.$id);
Andrey Andreevb24b0332012-03-26 15:34:39 +0300212 }
213
214 // ------------------------------------------------------------------------
215
216 /**
Greg Akerbde25d92010-12-21 09:31:21 -0600217 * Is the requested driver supported in this environment?
218 *
Andrey Andreev58c2b102012-10-26 14:42:29 +0300219 * @param string $driver The driver to test
Andrey Andreevb24b0332012-03-26 15:34:39 +0300220 * @return array
Greg Akerbde25d92010-12-21 09:31:21 -0600221 */
222 public function is_supported($driver)
223 {
224 static $support = array();
225
226 if ( ! isset($support[$driver]))
227 {
228 $support[$driver] = $this->{$driver}->is_supported();
229 }
230
231 return $support[$driver];
232 }
233
Greg Akerbde25d92010-12-21 09:31:21 -0600234}
Greg Akerbde25d92010-12-21 09:31:21 -0600235
236/* End of file Cache.php */
Andrey Andreev9e674f72012-06-09 21:02:52 +0300237/* Location: ./system/libraries/Cache/Cache.php */