blob: 8d8c0db34391276860bb221332798b14b3db2cea [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 */
Greg Akerbde25d92010-12-21 09:31:21 -060044 protected $valid_drivers = array(
Anton Lindqvist30930422012-04-25 12:32:58 +020045 'cache_apc',
Anton Lindqvist6581cac2012-04-25 12:35:41 +020046 'cache_dummy',
Anton Lindqvist30930422012-04-25 12:32:58 +020047 'cache_file',
Anton Lindqvist6581cac2012-04-25 12:35:41 +020048 'cache_memcached',
Anton Lindqvist30930422012-04-25 12:32:58 +020049 'cache_redis',
50 'cache_wincache'
51 );
Greg Akerbde25d92010-12-21 09:31:21 -060052
Timothy Warren0688ac92012-04-20 10:25:04 -040053 /**
54 * Path of cache files (if file-based cache)
55 *
56 * @var string
57 */
58 protected $_cache_path = NULL;
Andrey Andreev56454792012-05-17 14:32:19 +030059
Timothy Warren0688ac92012-04-20 10:25:04 -040060 /**
61 * Reference to the driver
62 *
Timothy Warrenb82bc3a2012-04-27 09:12:58 -040063 * @var mixed
Timothy Warren0688ac92012-04-20 10:25:04 -040064 */
65 protected $_adapter = 'dummy';
Andrey Andreev56454792012-05-17 14:32:19 +030066
Timothy Warren0688ac92012-04-20 10:25:04 -040067 /**
68 * Fallback driver
69 *
70 * @param string
71 */
Eric Robertsbf50a3b2012-05-27 19:04:43 -050072 protected $_backup_driver = 'dummy';
Andrey Andreev7d4ea072011-12-25 19:23:50 +020073
Greg Akerbde25d92010-12-21 09:31:21 -060074 /**
75 * Constructor
76 *
Greg Akerbde25d92010-12-21 09:31:21 -060077 * Initialize class properties based on the configuration array.
78 *
Andrey Andreev7d4ea072011-12-25 19:23:50 +020079 * @param array
Andrey Andreevb24b0332012-03-26 15:34:39 +030080 * @return void
Greg Akerbde25d92010-12-21 09:31:21 -060081 */
Andrey Andreevb24b0332012-03-26 15:34:39 +030082 public function __construct($config = array())
Greg Aker03abee32011-12-25 00:31:29 -060083 {
Greg Akerbde25d92010-12-21 09:31:21 -060084 $default_config = array(
Timothy Warren0688ac92012-04-20 10:25:04 -040085 'adapter',
86 'memcached'
87 );
Greg Akerbde25d92010-12-21 09:31:21 -060088
89 foreach ($default_config as $key)
90 {
91 if (isset($config[$key]))
92 {
93 $param = '_'.$key;
94
95 $this->{$param} = $config[$key];
96 }
97 }
98
99 if (isset($config['backup']))
100 {
101 if (in_array('cache_'.$config['backup'], $this->valid_drivers))
102 {
103 $this->_backup_driver = $config['backup'];
104 }
105 }
Eric Robertsbf50a3b2012-05-27 19:04:43 -0500106
107 // If the specified adapter isn't available, check the backup.
108 if ( ! $this->is_supported($this->_adapter))
109 {
110 if ( ! $this->is_supported($this->_backup_driver))
111 {
112 // Backup isn't supported either. Default to 'Dummy' driver.
113 log_message('error', 'Cache adapter "'.$this->_adapter.'" and backup "'.$this->_backup_driver.'" are both unavailable. Cache is now using "Dummy" adapter.');
114 $this->_adapter = 'dummy';
115 }
116 else
117 {
118 // Backup is supported. Set it to primary.
119 $this->_adapter = $this->_backup_driver;
120 }
121 }
Greg Akerbde25d92010-12-21 09:31:21 -0600122 }
123
124 // ------------------------------------------------------------------------
125
126 /**
Andrey Andreevb24b0332012-03-26 15:34:39 +0300127 * Get
128 *
129 * Look for a value in the cache. If it exists, return the data
130 * if not, return FALSE
131 *
132 * @param string
133 * @return mixed value that is stored/FALSE on failure
134 */
135 public function get($id)
136 {
137 return $this->{$this->_adapter}->get($id);
138 }
139
140 // ------------------------------------------------------------------------
141
142 /**
143 * Cache Save
144 *
145 * @param string Unique Key
146 * @param mixed Data to store
147 * @param int Length of time (in seconds) to cache the data
148 * @return bool true on success/false on failure
149 */
150 public function save($id, $data, $ttl = 60)
151 {
152 return $this->{$this->_adapter}->save($id, $data, $ttl);
153 }
154
155 // ------------------------------------------------------------------------
156
157 /**
158 * Delete from Cache
159 *
160 * @param mixed unique identifier of the item in the cache
161 * @return bool true on success/false on failure
162 */
163 public function delete($id)
164 {
165 return $this->{$this->_adapter}->delete($id);
166 }
167
168 // ------------------------------------------------------------------------
169
170 /**
171 * Clean the cache
172 *
173 * @return bool false on failure/true on success
174 */
175 public function clean()
176 {
177 return $this->{$this->_adapter}->clean();
178 }
179
180 // ------------------------------------------------------------------------
181
182 /**
183 * Cache Info
184 *
185 * @param string user/filehits
186 * @return mixed array on success, false on failure
187 */
188 public function cache_info($type = 'user')
189 {
190 return $this->{$this->_adapter}->cache_info($type);
191 }
192
193 // ------------------------------------------------------------------------
194
195 /**
196 * Get Cache Metadata
197 *
198 * @param mixed key to get cache metadata on
199 * @return mixed return value from child method
200 */
201 public function get_metadata($id)
202 {
203 return $this->{$this->_adapter}->get_metadata($id);
204 }
205
206 // ------------------------------------------------------------------------
207
208 /**
Greg Akerbde25d92010-12-21 09:31:21 -0600209 * Is the requested driver supported in this environment?
210 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300211 * @param string The driver to test.
212 * @return array
Greg Akerbde25d92010-12-21 09:31:21 -0600213 */
214 public function is_supported($driver)
215 {
216 static $support = array();
217
218 if ( ! isset($support[$driver]))
219 {
220 $support[$driver] = $this->{$driver}->is_supported();
221 }
222
223 return $support[$driver];
224 }
225
Greg Akerbde25d92010-12-21 09:31:21 -0600226}
Greg Akerbde25d92010-12-21 09:31:21 -0600227
228/* End of file Cache.php */
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200229/* Location: ./system/libraries/Cache/Cache.php */