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