blob: 7642a52701e5c02b81e5a5094d39b681dcae2db0 [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
28// ------------------------------------------------------------------------
29
30/**
Andrey Andreev7d4ea072011-12-25 19:23:50 +020031 * 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
Andrey Andreev7d4ea072011-12-25 19:23:50 +020037 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060038 */
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010039class CI_Cache extends CI_Driver_Library {
Andrey Andreev7d4ea072011-12-25 19:23:50 +020040
Greg Akerbde25d92010-12-21 09:31:21 -060041 protected $valid_drivers = array(
Andrey Andreev6b535f52012-03-12 22:19:13 +020042 'cache_apc',
43 'cache_file',
44 'cache_memcached',
45 'cache_dummy',
46 'cache_wincache'
47 );
Greg Akerbde25d92010-12-21 09:31:21 -060048
Andrey Andreev6b535f52012-03-12 22:19:13 +020049 protected $_cache_path = NULL; // Path of cache files (if file-based cache)
50 protected $_adapter = 'dummy';
Greg Akerbde25d92010-12-21 09:31:21 -060051 protected $_backup_driver;
Andrey Andreev7d4ea072011-12-25 19:23:50 +020052
Greg Akerbde25d92010-12-21 09:31:21 -060053 public function __construct($config = array())
54 {
55 if ( ! empty($config))
56 {
57 $this->_initialize($config);
58 }
59 }
60
61 // ------------------------------------------------------------------------
62
63 /**
Andrey Andreev7d4ea072011-12-25 19:23:50 +020064 * Get
Greg Akerbde25d92010-12-21 09:31:21 -060065 *
Andrey Andreev7d4ea072011-12-25 19:23:50 +020066 * Look for a value in the cache. If it exists, return the data
Greg Akerbde25d92010-12-21 09:31:21 -060067 * if not, return FALSE
68 *
Andrey Andreev7d4ea072011-12-25 19:23:50 +020069 * @param string
Greg Akerbde25d92010-12-21 09:31:21 -060070 * @return mixed value that is stored/FALSE on failure
71 */
72 public function get($id)
Andrey Andreev7d4ea072011-12-25 19:23:50 +020073 {
Greg Akerbde25d92010-12-21 09:31:21 -060074 return $this->{$this->_adapter}->get($id);
75 }
76
77 // ------------------------------------------------------------------------
78
79 /**
80 * Cache Save
81 *
82 * @param string Unique Key
83 * @param mixed Data to store
84 * @param int Length of time (in seconds) to cache the data
85 *
86 * @return boolean true on success/false on failure
87 */
88 public function save($id, $data, $ttl = 60)
89 {
90 return $this->{$this->_adapter}->save($id, $data, $ttl);
91 }
92
93 // ------------------------------------------------------------------------
94
95 /**
96 * Delete from Cache
97 *
98 * @param mixed unique identifier of the item in the cache
99 * @return boolean true on success/false on failure
100 */
101 public function delete($id)
102 {
103 return $this->{$this->_adapter}->delete($id);
104 }
105
106 // ------------------------------------------------------------------------
107
108 /**
109 * Clean the cache
110 *
111 * @return boolean false on failure/true on success
112 */
113 public function clean()
114 {
115 return $this->{$this->_adapter}->clean();
116 }
117
118 // ------------------------------------------------------------------------
119
120 /**
121 * Cache Info
122 *
123 * @param string user/filehits
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200124 * @return mixed array on success, false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600125 */
126 public function cache_info($type = 'user')
127 {
128 return $this->{$this->_adapter}->cache_info($type);
129 }
130
131 // ------------------------------------------------------------------------
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200132
Greg Akerbde25d92010-12-21 09:31:21 -0600133 /**
134 * Get Cache Metadata
135 *
136 * @param mixed key to get cache metadata on
137 * @return mixed return value from child method
138 */
139 public function get_metadata($id)
140 {
141 return $this->{$this->_adapter}->get_metadata($id);
142 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200143
Greg Akerbde25d92010-12-21 09:31:21 -0600144 // ------------------------------------------------------------------------
145
146 /**
147 * Initialize
148 *
149 * Initialize class properties based on the configuration array.
150 *
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200151 * @param array
Greg Akerbde25d92010-12-21 09:31:21 -0600152 * @return void
153 */
154 private function _initialize($config)
Greg Aker03abee32011-12-25 00:31:29 -0600155 {
Greg Akerbde25d92010-12-21 09:31:21 -0600156 $default_config = array(
157 'adapter',
158 'memcached'
159 );
160
161 foreach ($default_config as $key)
162 {
163 if (isset($config[$key]))
164 {
165 $param = '_'.$key;
166
167 $this->{$param} = $config[$key];
168 }
169 }
170
171 if (isset($config['backup']))
172 {
173 if (in_array('cache_'.$config['backup'], $this->valid_drivers))
174 {
175 $this->_backup_driver = $config['backup'];
176 }
177 }
178 }
179
180 // ------------------------------------------------------------------------
181
182 /**
183 * Is the requested driver supported in this environment?
184 *
185 * @param string The driver to test.
186 * @return array
187 */
188 public function is_supported($driver)
189 {
190 static $support = array();
191
192 if ( ! isset($support[$driver]))
193 {
194 $support[$driver] = $this->{$driver}->is_supported();
195 }
196
197 return $support[$driver];
198 }
199
200 // ------------------------------------------------------------------------
201
202 /**
203 * __get()
204 *
205 * @param child
206 * @return object
207 */
208 public function __get($child)
209 {
210 $obj = parent::__get($child);
211
212 if ( ! $this->is_supported($child))
213 {
214 $this->_adapter = $this->_backup_driver;
215 }
216
217 return $obj;
218 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200219
Greg Akerbde25d92010-12-21 09:31:21 -0600220 // ------------------------------------------------------------------------
221}
222// End Class
223
224/* End of file Cache.php */
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200225/* Location: ./system/libraries/Cache/Cache.php */