blob: c296fa7701788cb4ffffc452516b949df9bb89af [file] [log] [blame]
Derek Jonesf4a4bd82011-10-20 12:18:42 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Greg Akerbde25d92010-12-21 09:31:21 -06002/**
3 * CodeIgniter
4 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05005 * An open source application development framework for PHP 5.1.6 or newer
6 *
7 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
Derek Jones700205a2011-01-28 07:44:28 -060021 * @copyright Copyright (c) 2006 - 2011 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
Derek Jones37f4b9c2011-07-01 17:56:50 -050025 * @filesource
Greg Akerbde25d92010-12-21 09:31:21 -060026 */
27
28// ------------------------------------------------------------------------
29
30/**
Derek Jones37f4b9c2011-07-01 17:56:50 -050031 * CodeIgniter Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060032 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Core
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Jones37f4b9c2011-07-01 17:56:50 -050037 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060038 */
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010039class CI_Cache extends CI_Driver_Library {
Derek Jones37f4b9c2011-07-01 17:56:50 -050040
Greg Akerbde25d92010-12-21 09:31:21 -060041 protected $valid_drivers = array(
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010042 'cache_apc', 'cache_file', 'cache_memcached', 'cache_dummy'
43 );
Greg Akerbde25d92010-12-21 09:31:21 -060044
45 protected $_cache_path = NULL; // Path of cache files (if file-based cache)
46 protected $_adapter = 'dummy';
47 protected $_backup_driver;
Derek Jones37f4b9c2011-07-01 17:56:50 -050048
Greg Akerbde25d92010-12-21 09:31:21 -060049 // ------------------------------------------------------------------------
50
51 /**
52 * Constructor
53 *
54 * @param array
55 */
56 public function __construct($config = array())
57 {
58 if ( ! empty($config))
59 {
60 $this->_initialize($config);
61 }
62 }
63
64 // ------------------------------------------------------------------------
65
66 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050067 * Get
Greg Akerbde25d92010-12-21 09:31:21 -060068 *
Derek Jones37f4b9c2011-07-01 17:56:50 -050069 * Look for a value in the cache. If it exists, return the data
Greg Akerbde25d92010-12-21 09:31:21 -060070 * if not, return FALSE
71 *
Derek Jones37f4b9c2011-07-01 17:56:50 -050072 * @param string
Greg Akerbde25d92010-12-21 09:31:21 -060073 * @return mixed value that is stored/FALSE on failure
74 */
75 public function get($id)
Derek Jones37f4b9c2011-07-01 17:56:50 -050076 {
Greg Akerbde25d92010-12-21 09:31:21 -060077 return $this->{$this->_adapter}->get($id);
78 }
79
80 // ------------------------------------------------------------------------
81
82 /**
83 * Cache Save
84 *
85 * @param string Unique Key
86 * @param mixed Data to store
87 * @param int Length of time (in seconds) to cache the data
88 *
89 * @return boolean true on success/false on failure
90 */
91 public function save($id, $data, $ttl = 60)
92 {
93 return $this->{$this->_adapter}->save($id, $data, $ttl);
94 }
95
96 // ------------------------------------------------------------------------
97
98 /**
99 * Delete from Cache
100 *
101 * @param mixed unique identifier of the item in the cache
102 * @return boolean true on success/false on failure
103 */
104 public function delete($id)
105 {
106 return $this->{$this->_adapter}->delete($id);
107 }
108
109 // ------------------------------------------------------------------------
110
111 /**
112 * Clean the cache
113 *
114 * @return boolean false on failure/true on success
115 */
116 public function clean()
117 {
118 return $this->{$this->_adapter}->clean();
119 }
120
121 // ------------------------------------------------------------------------
122
123 /**
124 * Cache Info
125 *
126 * @param string user/filehits
Derek Jones37f4b9c2011-07-01 17:56:50 -0500127 * @return mixed array on success, false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600128 */
129 public function cache_info($type = 'user')
130 {
131 return $this->{$this->_adapter}->cache_info($type);
132 }
133
134 // ------------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500135
Greg Akerbde25d92010-12-21 09:31:21 -0600136 /**
137 * Get Cache Metadata
138 *
139 * @param mixed key to get cache metadata on
140 * @return mixed return value from child method
141 */
142 public function get_metadata($id)
143 {
144 return $this->{$this->_adapter}->get_metadata($id);
145 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500146
Greg Akerbde25d92010-12-21 09:31:21 -0600147 // ------------------------------------------------------------------------
148
149 /**
150 * Initialize
151 *
152 * Initialize class properties based on the configuration array.
153 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500154 * @param array
Greg Akerbde25d92010-12-21 09:31:21 -0600155 * @return void
156 */
157 private function _initialize($config)
Greg Aker03abee32011-12-25 00:31:29 -0600158 {
Greg Akerbde25d92010-12-21 09:31:21 -0600159 $default_config = array(
160 'adapter',
161 'memcached'
162 );
163
164 foreach ($default_config as $key)
165 {
166 if (isset($config[$key]))
167 {
168 $param = '_'.$key;
169
170 $this->{$param} = $config[$key];
171 }
172 }
173
174 if (isset($config['backup']))
175 {
176 if (in_array('cache_'.$config['backup'], $this->valid_drivers))
177 {
178 $this->_backup_driver = $config['backup'];
179 }
180 }
181 }
182
183 // ------------------------------------------------------------------------
184
185 /**
186 * Is the requested driver supported in this environment?
187 *
188 * @param string The driver to test.
189 * @return array
190 */
191 public function is_supported($driver)
192 {
193 static $support = array();
194
195 if ( ! isset($support[$driver]))
196 {
197 $support[$driver] = $this->{$driver}->is_supported();
198 }
199
200 return $support[$driver];
201 }
202
203 // ------------------------------------------------------------------------
204
205 /**
206 * __get()
207 *
208 * @param child
209 * @return object
210 */
211 public function __get($child)
212 {
213 $obj = parent::__get($child);
214
215 if ( ! $this->is_supported($child))
216 {
217 $this->_adapter = $this->_backup_driver;
218 }
219
220 return $obj;
221 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500222
Greg Akerbde25d92010-12-21 09:31:21 -0600223 // ------------------------------------------------------------------------
224}
225// End Class
226
227/* End of file Cache.php */
228/* Location: ./system/libraries/Cache/Cache.php */