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