blob: 82657ce58ca38623e4a340dda52845cad87e76c7 [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
Greg Akerbde25d92010-12-21 09:31:21 -060039 protected $valid_drivers = array(
Anton Lindqvist30930422012-04-25 12:32:58 +020040 'cache_apc',
41 'cache_dummy'
42 'cache_file',
43 'cache_memcached'
44 'cache_redis',
45 'cache_wincache'
46 );
Greg Akerbde25d92010-12-21 09:31:21 -060047
Andrey Andreev6b535f52012-03-12 22:19:13 +020048 protected $_cache_path = NULL; // Path of cache files (if file-based cache)
Greg Akerbde25d92010-12-21 09:31:21 -060049 protected $_adapter = 'dummy';
50 protected $_backup_driver;
Andrey Andreev7d4ea072011-12-25 19:23:50 +020051
Greg Akerbde25d92010-12-21 09:31:21 -060052 /**
53 * Constructor
54 *
Greg Akerbde25d92010-12-21 09:31:21 -060055 * Initialize class properties based on the configuration array.
56 *
Andrey Andreev7d4ea072011-12-25 19:23:50 +020057 * @param array
Andrey Andreevb24b0332012-03-26 15:34:39 +030058 * @return void
Greg Akerbde25d92010-12-21 09:31:21 -060059 */
Andrey Andreevb24b0332012-03-26 15:34:39 +030060 public function __construct($config = array())
Greg Aker03abee32011-12-25 00:31:29 -060061 {
Greg Akerbde25d92010-12-21 09:31:21 -060062 $default_config = array(
63 'adapter',
64 'memcached'
65 );
66
67 foreach ($default_config as $key)
68 {
69 if (isset($config[$key]))
70 {
71 $param = '_'.$key;
72
73 $this->{$param} = $config[$key];
74 }
75 }
76
77 if (isset($config['backup']))
78 {
79 if (in_array('cache_'.$config['backup'], $this->valid_drivers))
80 {
81 $this->_backup_driver = $config['backup'];
82 }
83 }
84 }
85
86 // ------------------------------------------------------------------------
87
88 /**
Andrey Andreevb24b0332012-03-26 15:34:39 +030089 * Get
90 *
91 * Look for a value in the cache. If it exists, return the data
92 * if not, return FALSE
93 *
94 * @param string
95 * @return mixed value that is stored/FALSE on failure
96 */
97 public function get($id)
98 {
99 return $this->{$this->_adapter}->get($id);
100 }
101
102 // ------------------------------------------------------------------------
103
104 /**
105 * Cache Save
106 *
107 * @param string Unique Key
108 * @param mixed Data to store
109 * @param int Length of time (in seconds) to cache the data
110 * @return bool true on success/false on failure
111 */
112 public function save($id, $data, $ttl = 60)
113 {
114 return $this->{$this->_adapter}->save($id, $data, $ttl);
115 }
116
117 // ------------------------------------------------------------------------
118
119 /**
120 * Delete from Cache
121 *
122 * @param mixed unique identifier of the item in the cache
123 * @return bool true on success/false on failure
124 */
125 public function delete($id)
126 {
127 return $this->{$this->_adapter}->delete($id);
128 }
129
130 // ------------------------------------------------------------------------
131
132 /**
133 * Clean the cache
134 *
135 * @return bool false on failure/true on success
136 */
137 public function clean()
138 {
139 return $this->{$this->_adapter}->clean();
140 }
141
142 // ------------------------------------------------------------------------
143
144 /**
145 * Cache Info
146 *
147 * @param string user/filehits
148 * @return mixed array on success, false on failure
149 */
150 public function cache_info($type = 'user')
151 {
152 return $this->{$this->_adapter}->cache_info($type);
153 }
154
155 // ------------------------------------------------------------------------
156
157 /**
158 * Get Cache Metadata
159 *
160 * @param mixed key to get cache metadata on
161 * @return mixed return value from child method
162 */
163 public function get_metadata($id)
164 {
165 return $this->{$this->_adapter}->get_metadata($id);
166 }
167
168 // ------------------------------------------------------------------------
169
170 /**
Greg Akerbde25d92010-12-21 09:31:21 -0600171 * Is the requested driver supported in this environment?
172 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300173 * @param string The driver to test.
174 * @return array
Greg Akerbde25d92010-12-21 09:31:21 -0600175 */
176 public function is_supported($driver)
177 {
178 static $support = array();
179
180 if ( ! isset($support[$driver]))
181 {
182 $support[$driver] = $this->{$driver}->is_supported();
183 }
184
185 return $support[$driver];
186 }
187
188 // ------------------------------------------------------------------------
189
190 /**
191 * __get()
192 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300193 * @param child
194 * @return object
Greg Akerbde25d92010-12-21 09:31:21 -0600195 */
196 public function __get($child)
197 {
198 $obj = parent::__get($child);
199
200 if ( ! $this->is_supported($child))
201 {
202 $this->_adapter = $this->_backup_driver;
203 }
204
205 return $obj;
206 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200207
Greg Akerbde25d92010-12-21 09:31:21 -0600208}
Greg Akerbde25d92010-12-21 09:31:21 -0600209
210/* End of file Cache.php */
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200211/* Location: ./system/libraries/Cache/Cache.php */