Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [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 |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [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) |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2016, British Columbia Institute of Technology |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [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: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -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/) |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://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 2.0.0 |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 36 | * @filesource |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [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'); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 39 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 40 | /** |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 41 | * CodeIgniter APC Caching Class |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
| 45 | * @category Core |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 47 | * @link |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 48 | */ |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 49 | class CI_Cache_apc extends CI_Driver { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 50 | |
| 51 | /** |
Andrey Andreev | 1be8987 | 2016-03-11 18:12:57 +0200 | [diff] [blame] | 52 | * Class constructor |
| 53 | * |
| 54 | * Only present so that an error message is logged |
| 55 | * if APC is not available. |
| 56 | * |
| 57 | * @return void |
| 58 | */ |
| 59 | public function __construct() |
| 60 | { |
| 61 | if ( ! $this->is_supported()) |
| 62 | { |
| 63 | log_message('error', 'Cache: Failed to initialize APC; extension not loaded/enabled?'); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // ------------------------------------------------------------------------ |
| 68 | |
| 69 | /** |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 70 | * Get |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 71 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 72 | * 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] | 73 | * if not, return FALSE |
| 74 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 75 | * @param string |
| 76 | * @return mixed value that is stored/FALSE on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 77 | */ |
| 78 | public function get($id) |
| 79 | { |
Andrey Andreev | a2b0677 | 2012-11-20 15:09:46 +0200 | [diff] [blame] | 80 | $success = FALSE; |
| 81 | $data = apc_fetch($id, $success); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 82 | |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 83 | if ($success === TRUE) |
| 84 | { |
| 85 | return is_array($data) |
| 86 | ? unserialize($data[0]) |
| 87 | : $data; |
| 88 | } |
| 89 | |
| 90 | return FALSE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 91 | } |
| 92 | |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 93 | // ------------------------------------------------------------------------ |
| 94 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 95 | /** |
| 96 | * Cache Save |
| 97 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 98 | * @param string $id Cache ID |
| 99 | * @param mixed $data Data to store |
Andrey Andreev | e13fa9f | 2016-05-20 17:30:07 +0300 | [diff] [blame^] | 100 | * @param int $ttl Length of time (in seconds) to cache the data |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 101 | * @param bool $raw Whether to store the raw value |
| 102 | * @return bool TRUE on success, FALSE on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 103 | */ |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 104 | public function save($id, $data, $ttl = 60, $raw = FALSE) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 105 | { |
Sean Fisher | 7bb95df | 2012-01-16 09:23:14 -0500 | [diff] [blame] | 106 | $ttl = (int) $ttl; |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 107 | |
| 108 | return apc_store( |
| 109 | $id, |
| 110 | ($raw === TRUE ? $data : array(serialize($data), time(), $ttl)), |
| 111 | $ttl |
| 112 | ); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 113 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 114 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 115 | // ------------------------------------------------------------------------ |
| 116 | |
| 117 | /** |
| 118 | * Delete from Cache |
| 119 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 120 | * @param mixed unique identifier of the item in the cache |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 121 | * @return bool true on success/false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 122 | */ |
| 123 | public function delete($id) |
| 124 | { |
| 125 | return apc_delete($id); |
| 126 | } |
| 127 | |
| 128 | // ------------------------------------------------------------------------ |
| 129 | |
| 130 | /** |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 131 | * Increment a raw value |
| 132 | * |
| 133 | * @param string $id Cache ID |
| 134 | * @param int $offset Step/value to add |
| 135 | * @return mixed New value on success or FALSE on failure |
| 136 | */ |
| 137 | public function increment($id, $offset = 1) |
| 138 | { |
| 139 | return apc_inc($id, $offset); |
| 140 | } |
| 141 | |
| 142 | // ------------------------------------------------------------------------ |
| 143 | |
| 144 | /** |
| 145 | * Decrement a raw value |
| 146 | * |
| 147 | * @param string $id Cache ID |
| 148 | * @param int $offset Step/value to reduce by |
| 149 | * @return mixed New value on success or FALSE on failure |
| 150 | */ |
| 151 | public function decrement($id, $offset = 1) |
| 152 | { |
| 153 | return apc_dec($id, $offset); |
| 154 | } |
| 155 | |
| 156 | // ------------------------------------------------------------------------ |
| 157 | |
| 158 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 159 | * Clean the cache |
| 160 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 161 | * @return bool false on failure/true on success |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 162 | */ |
| 163 | public function clean() |
| 164 | { |
| 165 | return apc_clear_cache('user'); |
| 166 | } |
| 167 | |
| 168 | // ------------------------------------------------------------------------ |
| 169 | |
| 170 | /** |
| 171 | * Cache Info |
| 172 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 173 | * @param string user/filehits |
| 174 | * @return mixed array on success, false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 175 | */ |
| 176 | public function cache_info($type = NULL) |
| 177 | { |
| 178 | return apc_cache_info($type); |
| 179 | } |
| 180 | |
| 181 | // ------------------------------------------------------------------------ |
| 182 | |
| 183 | /** |
| 184 | * Get Cache Metadata |
| 185 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 186 | * @param mixed key to get cache metadata on |
| 187 | * @return mixed array on success/false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 188 | */ |
| 189 | public function get_metadata($id) |
| 190 | { |
Andrey Andreev | a2b0677 | 2012-11-20 15:09:46 +0200 | [diff] [blame] | 191 | $success = FALSE; |
| 192 | $stored = apc_fetch($id, $success); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 193 | |
Andrey Andreev | a2b0677 | 2012-11-20 15:09:46 +0200 | [diff] [blame] | 194 | if ($success === FALSE OR count($stored) !== 3) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 195 | { |
| 196 | return FALSE; |
| 197 | } |
| 198 | |
Greg Aker | 999e747 | 2011-01-29 16:16:58 -0600 | [diff] [blame] | 199 | list($data, $time, $ttl) = $stored; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 200 | |
| 201 | return array( |
| 202 | 'expire' => $time + $ttl, |
| 203 | 'mtime' => $time, |
Andrey Andreev | a2b0677 | 2012-11-20 15:09:46 +0200 | [diff] [blame] | 204 | 'data' => unserialize($data) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 205 | ); |
| 206 | } |
| 207 | |
| 208 | // ------------------------------------------------------------------------ |
| 209 | |
| 210 | /** |
| 211 | * is_supported() |
| 212 | * |
| 213 | * Check to see if APC is available on this system, bail if it isn't. |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 214 | * |
| 215 | * @return bool |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 216 | */ |
| 217 | public function is_supported() |
| 218 | { |
Andrey Andreev | 1be8987 | 2016-03-11 18:12:57 +0200 | [diff] [blame] | 219 | return (extension_loaded('apc') && ini_get('apc.enabled')); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 220 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 221 | } |