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