Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [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 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame^] | 5 | * 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 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 |
Derek Jones | 700205a | 2011-01-28 07:44:28 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2006 - 2011 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 |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 25 | * @filesource |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 31 | * CodeIgniter Caching Class |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Libraries |
| 35 | * @category Core |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame^] | 36 | * @author EllisLab Dev Team |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 37 | * @link |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 38 | */ |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 39 | class CI_Cache extends CI_Driver_Library { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 40 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 41 | protected $valid_drivers = array( |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 42 | 'cache_apc', 'cache_file', 'cache_memcached', 'cache_dummy' |
| 43 | ); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 44 | |
| 45 | protected $_cache_path = NULL; // Path of cache files (if file-based cache) |
| 46 | protected $_adapter = 'dummy'; |
| 47 | protected $_backup_driver; |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 48 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 49 | // ------------------------------------------------------------------------ |
| 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 Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 67 | * Get |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 68 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 69 | * Look for a value in the cache. If it exists, return the data |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 70 | * if not, return FALSE |
| 71 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 72 | * @param string |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 73 | * @return mixed value that is stored/FALSE on failure |
| 74 | */ |
| 75 | public function get($id) |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 76 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 77 | 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 Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 127 | * @return mixed array on success, false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 128 | */ |
| 129 | public function cache_info($type = 'user') |
| 130 | { |
| 131 | return $this->{$this->_adapter}->cache_info($type); |
| 132 | } |
| 133 | |
| 134 | // ------------------------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 135 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 136 | /** |
| 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 Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 146 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 147 | // ------------------------------------------------------------------------ |
| 148 | |
| 149 | /** |
| 150 | * Initialize |
| 151 | * |
| 152 | * Initialize class properties based on the configuration array. |
| 153 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 154 | * @param array |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 155 | * @return void |
| 156 | */ |
| 157 | private function _initialize($config) |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 158 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 159 | $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 Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 222 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 223 | // ------------------------------------------------------------------------ |
| 224 | } |
| 225 | // End Class |
| 226 | |
| 227 | /* End of file Cache.php */ |
| 228 | /* Location: ./system/libraries/Cache/Cache.php */ |