Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 6 | * |
| 7 | * NOTICE OF LICENSE |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 18 | * |
| 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2006 - 2012 EllisLab, Inc. |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 2.0 |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 25 | * @filesource |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 26 | */ |
| 27 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 28 | /** |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 29 | * CodeIgniter Caching Class |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Libraries |
| 33 | * @category Core |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 35 | * @link |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 36 | */ |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 37 | class CI_Cache extends CI_Driver_Library { |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 38 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 39 | /** |
| 40 | * Valid cache drivers |
| 41 | * |
| 42 | * @var array |
| 43 | */ |
| 44 | protected $valid_drivers = array( |
| 45 | 'cache_apc', |
| 46 | 'cache_file', |
| 47 | 'cache_memcached', |
| 48 | 'cache_dummy', |
| 49 | 'cache_wincache' |
| 50 | ); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 51 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 52 | /** |
| 53 | * Path of cache files (if file-based cache) |
| 54 | * |
| 55 | * @var string |
| 56 | */ |
| 57 | protected $_cache_path = NULL; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame^] | 58 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 59 | /** |
| 60 | * Reference to the driver |
| 61 | * |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 62 | * @var mixed |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 63 | */ |
| 64 | protected $_adapter = 'dummy'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame^] | 65 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 66 | /** |
| 67 | * Fallback driver |
| 68 | * |
| 69 | * @param string |
| 70 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 71 | protected $_backup_driver; |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 72 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 73 | /** |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 74 | * Constructor |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 75 | * |
| 76 | * Initialize class properties based on the configuration array. |
| 77 | * |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 78 | * @param array |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 79 | * @return void |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 80 | */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 81 | public function __construct($config = array()) |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 82 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 83 | $default_config = array( |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 84 | 'adapter', |
| 85 | 'memcached' |
| 86 | ); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 87 | |
| 88 | foreach ($default_config as $key) |
| 89 | { |
| 90 | if (isset($config[$key])) |
| 91 | { |
| 92 | $param = '_'.$key; |
| 93 | |
| 94 | $this->{$param} = $config[$key]; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if (isset($config['backup'])) |
| 99 | { |
| 100 | if (in_array('cache_'.$config['backup'], $this->valid_drivers)) |
| 101 | { |
| 102 | $this->_backup_driver = $config['backup']; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // ------------------------------------------------------------------------ |
| 108 | |
| 109 | /** |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 110 | * Get |
| 111 | * |
| 112 | * Look for a value in the cache. If it exists, return the data |
| 113 | * if not, return FALSE |
| 114 | * |
| 115 | * @param string |
| 116 | * @return mixed value that is stored/FALSE on failure |
| 117 | */ |
| 118 | public function get($id) |
| 119 | { |
| 120 | return $this->{$this->_adapter}->get($id); |
| 121 | } |
| 122 | |
| 123 | // ------------------------------------------------------------------------ |
| 124 | |
| 125 | /** |
| 126 | * Cache Save |
| 127 | * |
| 128 | * @param string Unique Key |
| 129 | * @param mixed Data to store |
| 130 | * @param int Length of time (in seconds) to cache the data |
| 131 | * @return bool true on success/false on failure |
| 132 | */ |
| 133 | public function save($id, $data, $ttl = 60) |
| 134 | { |
| 135 | return $this->{$this->_adapter}->save($id, $data, $ttl); |
| 136 | } |
| 137 | |
| 138 | // ------------------------------------------------------------------------ |
| 139 | |
| 140 | /** |
| 141 | * Delete from Cache |
| 142 | * |
| 143 | * @param mixed unique identifier of the item in the cache |
| 144 | * @return bool true on success/false on failure |
| 145 | */ |
| 146 | public function delete($id) |
| 147 | { |
| 148 | return $this->{$this->_adapter}->delete($id); |
| 149 | } |
| 150 | |
| 151 | // ------------------------------------------------------------------------ |
| 152 | |
| 153 | /** |
| 154 | * Clean the cache |
| 155 | * |
| 156 | * @return bool false on failure/true on success |
| 157 | */ |
| 158 | public function clean() |
| 159 | { |
| 160 | return $this->{$this->_adapter}->clean(); |
| 161 | } |
| 162 | |
| 163 | // ------------------------------------------------------------------------ |
| 164 | |
| 165 | /** |
| 166 | * Cache Info |
| 167 | * |
| 168 | * @param string user/filehits |
| 169 | * @return mixed array on success, false on failure |
| 170 | */ |
| 171 | public function cache_info($type = 'user') |
| 172 | { |
| 173 | return $this->{$this->_adapter}->cache_info($type); |
| 174 | } |
| 175 | |
| 176 | // ------------------------------------------------------------------------ |
| 177 | |
| 178 | /** |
| 179 | * Get Cache Metadata |
| 180 | * |
| 181 | * @param mixed key to get cache metadata on |
| 182 | * @return mixed return value from child method |
| 183 | */ |
| 184 | public function get_metadata($id) |
| 185 | { |
| 186 | return $this->{$this->_adapter}->get_metadata($id); |
| 187 | } |
| 188 | |
| 189 | // ------------------------------------------------------------------------ |
| 190 | |
| 191 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 192 | * Is the requested driver supported in this environment? |
| 193 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 194 | * @param string The driver to test. |
| 195 | * @return array |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 196 | */ |
| 197 | public function is_supported($driver) |
| 198 | { |
| 199 | static $support = array(); |
| 200 | |
| 201 | if ( ! isset($support[$driver])) |
| 202 | { |
| 203 | $support[$driver] = $this->{$driver}->is_supported(); |
| 204 | } |
| 205 | |
| 206 | return $support[$driver]; |
| 207 | } |
| 208 | |
| 209 | // ------------------------------------------------------------------------ |
| 210 | |
| 211 | /** |
| 212 | * __get() |
| 213 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 214 | * @param child |
| 215 | * @return object |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 216 | */ |
| 217 | public function __get($child) |
| 218 | { |
| 219 | $obj = parent::__get($child); |
| 220 | |
| 221 | if ( ! $this->is_supported($child)) |
| 222 | { |
| 223 | $this->_adapter = $this->_backup_driver; |
| 224 | } |
| 225 | |
| 226 | return $obj; |
| 227 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 228 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 229 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 230 | |
| 231 | /* End of file Cache.php */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 232 | /* Location: ./system/libraries/Cache/Cache.php */ |