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 | */ |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 44 | protected $valid_drivers = array( |
Anton Lindqvist | 3093042 | 2012-04-25 12:32:58 +0200 | [diff] [blame] | 45 | 'cache_apc', |
Anton Lindqvist | 6581cac | 2012-04-25 12:35:41 +0200 | [diff] [blame] | 46 | 'cache_dummy', |
Anton Lindqvist | 3093042 | 2012-04-25 12:32:58 +0200 | [diff] [blame] | 47 | 'cache_file', |
Anton Lindqvist | 6581cac | 2012-04-25 12:35:41 +0200 | [diff] [blame] | 48 | 'cache_memcached', |
Anton Lindqvist | 3093042 | 2012-04-25 12:32:58 +0200 | [diff] [blame] | 49 | 'cache_redis', |
| 50 | 'cache_wincache' |
| 51 | ); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 52 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 53 | /** |
| 54 | * Path of cache files (if file-based cache) |
| 55 | * |
| 56 | * @var string |
| 57 | */ |
| 58 | protected $_cache_path = NULL; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 59 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 60 | /** |
| 61 | * Reference to the driver |
| 62 | * |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 63 | * @var mixed |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 64 | */ |
| 65 | protected $_adapter = 'dummy'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 66 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 67 | /** |
| 68 | * Fallback driver |
| 69 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 70 | * @var string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 71 | */ |
Eric Roberts | bf50a3b | 2012-05-27 19:04:43 -0500 | [diff] [blame] | 72 | protected $_backup_driver = 'dummy'; |
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 | /** |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 75 | * Cache key prefix |
| 76 | * |
| 77 | * @var string |
| 78 | */ |
| 79 | public $key_prefix = ''; |
| 80 | |
| 81 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 82 | * Constructor |
| 83 | * |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 84 | * Initialize class properties based on the configuration array. |
| 85 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 86 | * @param array $config = array() |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 87 | * @return void |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 88 | */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 89 | public function __construct($config = array()) |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 90 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 91 | $default_config = array( |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 92 | 'adapter', |
| 93 | 'memcached' |
| 94 | ); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 95 | |
| 96 | foreach ($default_config as $key) |
| 97 | { |
| 98 | if (isset($config[$key])) |
| 99 | { |
| 100 | $param = '_'.$key; |
| 101 | |
| 102 | $this->{$param} = $config[$key]; |
| 103 | } |
| 104 | } |
| 105 | |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 106 | isset($config['key_prefix']) AND $this->key_prefix = $config['key_prefix']; |
| 107 | |
| 108 | if (isset($config['backup']) && in_array('cache_'.$config['backup'], $this->valid_drivers)) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 109 | { |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 110 | $this->_backup_driver = $config['backup']; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 111 | } |
Eric Roberts | bf50a3b | 2012-05-27 19:04:43 -0500 | [diff] [blame] | 112 | |
| 113 | // If the specified adapter isn't available, check the backup. |
| 114 | if ( ! $this->is_supported($this->_adapter)) |
| 115 | { |
| 116 | if ( ! $this->is_supported($this->_backup_driver)) |
| 117 | { |
| 118 | // Backup isn't supported either. Default to 'Dummy' driver. |
| 119 | log_message('error', 'Cache adapter "'.$this->_adapter.'" and backup "'.$this->_backup_driver.'" are both unavailable. Cache is now using "Dummy" adapter.'); |
| 120 | $this->_adapter = 'dummy'; |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | // Backup is supported. Set it to primary. |
| 125 | $this->_adapter = $this->_backup_driver; |
| 126 | } |
| 127 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // ------------------------------------------------------------------------ |
| 131 | |
| 132 | /** |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 133 | * Get |
| 134 | * |
| 135 | * Look for a value in the cache. If it exists, return the data |
| 136 | * if not, return FALSE |
| 137 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 138 | * @param string $id |
| 139 | * @return mixed value matching $id or FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 140 | */ |
| 141 | public function get($id) |
| 142 | { |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 143 | return $this->{$this->_adapter}->get($this->key_prefix.$id); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // ------------------------------------------------------------------------ |
| 147 | |
| 148 | /** |
| 149 | * Cache Save |
| 150 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 151 | * @param string $id Cache ID |
| 152 | * @param mixed $data Data to store |
| 153 | * @param int $ttl = 60 Cache TTL (in seconds) |
| 154 | * @return bool TRUE on success, FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 155 | */ |
| 156 | public function save($id, $data, $ttl = 60) |
| 157 | { |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 158 | return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // ------------------------------------------------------------------------ |
| 162 | |
| 163 | /** |
| 164 | * Delete from Cache |
| 165 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 166 | * @param string $id Cache ID |
| 167 | * @return bool TRUE on success, FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 168 | */ |
| 169 | public function delete($id) |
| 170 | { |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 171 | return $this->{$this->_adapter}->delete($this->key_prefix.$id); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // ------------------------------------------------------------------------ |
| 175 | |
| 176 | /** |
| 177 | * Clean the cache |
| 178 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 179 | * @return bool TRUE on success, FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 180 | */ |
| 181 | public function clean() |
| 182 | { |
| 183 | return $this->{$this->_adapter}->clean(); |
| 184 | } |
| 185 | |
| 186 | // ------------------------------------------------------------------------ |
| 187 | |
| 188 | /** |
| 189 | * Cache Info |
| 190 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 191 | * @param string $type = 'user' user/filehits |
| 192 | * @return mixed array containing cache info on success OR FALSE on failure |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 193 | */ |
| 194 | public function cache_info($type = 'user') |
| 195 | { |
| 196 | return $this->{$this->_adapter}->cache_info($type); |
| 197 | } |
| 198 | |
| 199 | // ------------------------------------------------------------------------ |
| 200 | |
| 201 | /** |
| 202 | * Get Cache Metadata |
| 203 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 204 | * @param string $id key to get cache metadata on |
| 205 | * @return mixed cache item metadata |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 206 | */ |
| 207 | public function get_metadata($id) |
| 208 | { |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 209 | return $this->{$this->_adapter}->get_metadata($this->key_prefix.$id); |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // ------------------------------------------------------------------------ |
| 213 | |
| 214 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 215 | * Is the requested driver supported in this environment? |
| 216 | * |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame^] | 217 | * @param string $driver The driver to test |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 218 | * @return array |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 219 | */ |
| 220 | public function is_supported($driver) |
| 221 | { |
| 222 | static $support = array(); |
| 223 | |
| 224 | if ( ! isset($support[$driver])) |
| 225 | { |
| 226 | $support[$driver] = $this->{$driver}->is_supported(); |
| 227 | } |
| 228 | |
| 229 | return $support[$driver]; |
| 230 | } |
| 231 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 232 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 233 | |
| 234 | /* End of file Cache.php */ |
Andrey Andreev | 9e674f7 | 2012-06-09 21:02:52 +0300 | [diff] [blame] | 235 | /* Location: ./system/libraries/Cache/Cache.php */ |