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 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [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 |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [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 { |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 40 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 41 | protected $valid_drivers = array( |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 42 | 'cache_apc', |
| 43 | 'cache_file', |
| 44 | 'cache_memcached', |
| 45 | 'cache_dummy', |
| 46 | 'cache_wincache' |
| 47 | ); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 48 | |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 49 | protected $_cache_path = NULL; // Path of cache files (if file-based cache) |
| 50 | protected $_adapter = 'dummy'; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 51 | protected $_backup_driver; |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 52 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 53 | public function __construct($config = array()) |
| 54 | { |
| 55 | if ( ! empty($config)) |
| 56 | { |
| 57 | $this->_initialize($config); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // ------------------------------------------------------------------------ |
| 62 | |
| 63 | /** |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 64 | * Get |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 65 | * |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 66 | * 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] | 67 | * if not, return FALSE |
| 68 | * |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 69 | * @param string |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 70 | * @return mixed value that is stored/FALSE on failure |
| 71 | */ |
| 72 | public function get($id) |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 73 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 74 | return $this->{$this->_adapter}->get($id); |
| 75 | } |
| 76 | |
| 77 | // ------------------------------------------------------------------------ |
| 78 | |
| 79 | /** |
| 80 | * Cache Save |
| 81 | * |
| 82 | * @param string Unique Key |
| 83 | * @param mixed Data to store |
| 84 | * @param int Length of time (in seconds) to cache the data |
| 85 | * |
| 86 | * @return boolean true on success/false on failure |
| 87 | */ |
| 88 | public function save($id, $data, $ttl = 60) |
| 89 | { |
| 90 | return $this->{$this->_adapter}->save($id, $data, $ttl); |
| 91 | } |
| 92 | |
| 93 | // ------------------------------------------------------------------------ |
| 94 | |
| 95 | /** |
| 96 | * Delete from Cache |
| 97 | * |
| 98 | * @param mixed unique identifier of the item in the cache |
| 99 | * @return boolean true on success/false on failure |
| 100 | */ |
| 101 | public function delete($id) |
| 102 | { |
| 103 | return $this->{$this->_adapter}->delete($id); |
| 104 | } |
| 105 | |
| 106 | // ------------------------------------------------------------------------ |
| 107 | |
| 108 | /** |
| 109 | * Clean the cache |
| 110 | * |
| 111 | * @return boolean false on failure/true on success |
| 112 | */ |
| 113 | public function clean() |
| 114 | { |
| 115 | return $this->{$this->_adapter}->clean(); |
| 116 | } |
| 117 | |
| 118 | // ------------------------------------------------------------------------ |
| 119 | |
| 120 | /** |
| 121 | * Cache Info |
| 122 | * |
| 123 | * @param string user/filehits |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 124 | * @return mixed array on success, false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 125 | */ |
| 126 | public function cache_info($type = 'user') |
| 127 | { |
| 128 | return $this->{$this->_adapter}->cache_info($type); |
| 129 | } |
| 130 | |
| 131 | // ------------------------------------------------------------------------ |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 132 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 133 | /** |
| 134 | * Get Cache Metadata |
| 135 | * |
| 136 | * @param mixed key to get cache metadata on |
| 137 | * @return mixed return value from child method |
| 138 | */ |
| 139 | public function get_metadata($id) |
| 140 | { |
| 141 | return $this->{$this->_adapter}->get_metadata($id); |
| 142 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 143 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 144 | // ------------------------------------------------------------------------ |
| 145 | |
| 146 | /** |
| 147 | * Initialize |
| 148 | * |
| 149 | * Initialize class properties based on the configuration array. |
| 150 | * |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 151 | * @param array |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 152 | * @return void |
| 153 | */ |
| 154 | private function _initialize($config) |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 155 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 156 | $default_config = array( |
| 157 | 'adapter', |
| 158 | 'memcached' |
| 159 | ); |
| 160 | |
| 161 | foreach ($default_config as $key) |
| 162 | { |
| 163 | if (isset($config[$key])) |
| 164 | { |
| 165 | $param = '_'.$key; |
| 166 | |
| 167 | $this->{$param} = $config[$key]; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (isset($config['backup'])) |
| 172 | { |
| 173 | if (in_array('cache_'.$config['backup'], $this->valid_drivers)) |
| 174 | { |
| 175 | $this->_backup_driver = $config['backup']; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // ------------------------------------------------------------------------ |
| 181 | |
| 182 | /** |
| 183 | * Is the requested driver supported in this environment? |
| 184 | * |
| 185 | * @param string The driver to test. |
| 186 | * @return array |
| 187 | */ |
| 188 | public function is_supported($driver) |
| 189 | { |
| 190 | static $support = array(); |
| 191 | |
| 192 | if ( ! isset($support[$driver])) |
| 193 | { |
| 194 | $support[$driver] = $this->{$driver}->is_supported(); |
| 195 | } |
| 196 | |
| 197 | return $support[$driver]; |
| 198 | } |
| 199 | |
| 200 | // ------------------------------------------------------------------------ |
| 201 | |
| 202 | /** |
| 203 | * __get() |
| 204 | * |
| 205 | * @param child |
| 206 | * @return object |
| 207 | */ |
| 208 | public function __get($child) |
| 209 | { |
| 210 | $obj = parent::__get($child); |
| 211 | |
| 212 | if ( ! $this->is_supported($child)) |
| 213 | { |
| 214 | $this->_adapter = $this->_backup_driver; |
| 215 | } |
| 216 | |
| 217 | return $obj; |
| 218 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 219 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 220 | // ------------------------------------------------------------------------ |
| 221 | } |
| 222 | // End Class |
| 223 | |
| 224 | /* End of file Cache.php */ |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 225 | /* Location: ./system/libraries/Cache/Cache.php */ |