Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Mike Davies | 100934c | 2012-03-02 19:18:22 -0500 | [diff] [blame] | 8 | * |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2019, British Columbia Institute of Technology |
Mike Davies | 100934c | 2012-03-02 19:18:22 -0500 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Mike Davies | 100934c | 2012-03-02 19:18:22 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/) |
| 33 | * @license https://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 3.0.0 |
Mike Davies | 100934c | 2012-03-02 19:18:22 -0500 | [diff] [blame] | 36 | * @filesource |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 39 | |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 40 | /** |
| 41 | * CodeIgniter Wincache Caching Class |
| 42 | * |
| 43 | * Read more about Wincache functions here: |
| 44 | * http://www.php.net/manual/en/ref.wincache.php |
| 45 | * |
| 46 | * @package CodeIgniter |
| 47 | * @subpackage Libraries |
| 48 | * @category Core |
| 49 | * @author Mike Murkovic |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 50 | * @link |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 51 | */ |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 52 | class CI_Cache_wincache extends CI_Driver { |
| 53 | |
| 54 | /** |
Andrey Andreev | 1be8987 | 2016-03-11 18:12:57 +0200 | [diff] [blame] | 55 | * Class constructor |
| 56 | * |
| 57 | * Only present so that an error message is logged |
| 58 | * if APC is not available. |
| 59 | * |
| 60 | * @return void |
| 61 | */ |
| 62 | public function __construct() |
| 63 | { |
| 64 | if ( ! $this->is_supported()) |
| 65 | { |
| 66 | log_message('error', 'Cache: Failed to initialize Wincache; extension not loaded/enabled?'); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // ------------------------------------------------------------------------ |
| 71 | |
| 72 | /** |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 73 | * Get |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 74 | * |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 75 | * Look for a value in the cache. If it exists, return the data, |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 76 | * if not, return FALSE |
| 77 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 78 | * @param string $id Cache Ide |
| 79 | * @return mixed Value that is stored/FALSE on failure |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 80 | */ |
| 81 | public function get($id) |
| 82 | { |
Mike Davies | 100934c | 2012-03-02 19:18:22 -0500 | [diff] [blame] | 83 | $success = FALSE; |
| 84 | $data = wincache_ucache_get($id, $success); |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 85 | |
| 86 | // Success returned by reference from wincache_ucache_get() |
Mike Davies | 100934c | 2012-03-02 19:18:22 -0500 | [diff] [blame] | 87 | return ($success) ? $data : FALSE; |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 90 | // ------------------------------------------------------------------------ |
| 91 | |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 92 | /** |
| 93 | * Cache Save |
| 94 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 95 | * @param string $id Cache ID |
| 96 | * @param mixed $data Data to store |
| 97 | * @param int $ttl Time to live (in seconds) |
| 98 | * @param bool $raw Whether to store the raw value (unused) |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 99 | * @return bool true on success/false on failure |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 100 | */ |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 101 | public function save($id, $data, $ttl = 60, $raw = FALSE) |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 102 | { |
| 103 | return wincache_ucache_set($id, $data, $ttl); |
| 104 | } |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 105 | |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 106 | // ------------------------------------------------------------------------ |
| 107 | |
| 108 | /** |
| 109 | * Delete from Cache |
| 110 | * |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 111 | * @param mixed unique identifier of the item in the cache |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 112 | * @return bool true on success/false on failure |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 113 | */ |
| 114 | public function delete($id) |
| 115 | { |
| 116 | return wincache_ucache_delete($id); |
| 117 | } |
| 118 | |
| 119 | // ------------------------------------------------------------------------ |
| 120 | |
| 121 | /** |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 122 | * Increment a raw value |
| 123 | * |
| 124 | * @param string $id Cache ID |
| 125 | * @param int $offset Step/value to add |
| 126 | * @return mixed New value on success or FALSE on failure |
| 127 | */ |
| 128 | public function increment($id, $offset = 1) |
| 129 | { |
| 130 | $success = FALSE; |
| 131 | $value = wincache_ucache_inc($id, $offset, $success); |
| 132 | |
| 133 | return ($success === TRUE) ? $value : FALSE; |
| 134 | } |
| 135 | |
| 136 | // ------------------------------------------------------------------------ |
| 137 | |
| 138 | /** |
| 139 | * Decrement a raw value |
| 140 | * |
| 141 | * @param string $id Cache ID |
| 142 | * @param int $offset Step/value to reduce by |
| 143 | * @return mixed New value on success or FALSE on failure |
| 144 | */ |
| 145 | public function decrement($id, $offset = 1) |
| 146 | { |
| 147 | $success = FALSE; |
| 148 | $value = wincache_ucache_dec($id, $offset, $success); |
| 149 | |
| 150 | return ($success === TRUE) ? $value : FALSE; |
| 151 | } |
| 152 | |
| 153 | // ------------------------------------------------------------------------ |
| 154 | |
| 155 | /** |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 156 | * Clean the cache |
| 157 | * |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 158 | * @return bool false on failure/true on success |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 159 | */ |
| 160 | public function clean() |
| 161 | { |
| 162 | return wincache_ucache_clear(); |
| 163 | } |
| 164 | |
| 165 | // ------------------------------------------------------------------------ |
| 166 | |
| 167 | /** |
| 168 | * Cache Info |
| 169 | * |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 170 | * @return mixed array on success, false on failure |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 171 | */ |
| 172 | public function cache_info() |
| 173 | { |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 174 | return wincache_ucache_info(TRUE); |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // ------------------------------------------------------------------------ |
| 178 | |
| 179 | /** |
| 180 | * Get Cache Metadata |
| 181 | * |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 182 | * @param mixed key to get cache metadata on |
| 183 | * @return mixed array on success/false on failure |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 184 | */ |
| 185 | public function get_metadata($id) |
| 186 | { |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 187 | if ($stored = wincache_ucache_info(FALSE, $id)) |
Mike Davies | 100934c | 2012-03-02 19:18:22 -0500 | [diff] [blame] | 188 | { |
| 189 | $age = $stored['ucache_entries'][1]['age_seconds']; |
| 190 | $ttl = $stored['ucache_entries'][1]['ttl_seconds']; |
| 191 | $hitcount = $stored['ucache_entries'][1]['hitcount']; |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 192 | |
Mike Davies | 100934c | 2012-03-02 19:18:22 -0500 | [diff] [blame] | 193 | return array( |
Andrey Andreev | 838a9d6 | 2012-12-03 14:37:47 +0200 | [diff] [blame] | 194 | 'expire' => $ttl - $age, |
| 195 | 'hitcount' => $hitcount, |
| 196 | 'age' => $age, |
| 197 | 'ttl' => $ttl |
Mike Davies | 100934c | 2012-03-02 19:18:22 -0500 | [diff] [blame] | 198 | ); |
| 199 | } |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 200 | |
| 201 | return FALSE; |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // ------------------------------------------------------------------------ |
| 205 | |
| 206 | /** |
| 207 | * is_supported() |
| 208 | * |
| 209 | * Check to see if WinCache is available on this system, bail if it isn't. |
Andrey Andreev | 6b535f5 | 2012-03-12 22:19:13 +0200 | [diff] [blame] | 210 | * |
| 211 | * @return bool |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 212 | */ |
| 213 | public function is_supported() |
| 214 | { |
Andrey Andreev | 1be8987 | 2016-03-11 18:12:57 +0200 | [diff] [blame] | 215 | return (extension_loaded('wincache') && ini_get('wincache.ucenabled')); |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 216 | } |
Mike Davies | 03a5765 | 2012-02-29 17:52:36 -0500 | [diff] [blame] | 217 | } |