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 | * |
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 |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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. |
| 18 | * |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 28 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 29 | /** |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 30 | * CodeIgniter APC Caching Class |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Libraries |
| 34 | * @category Core |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 36 | * @link |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 37 | */ |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 38 | class CI_Cache_apc extends CI_Driver { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 39 | |
| 40 | /** |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 41 | * Get |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 42 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 43 | * 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] | 44 | * if not, return FALSE |
| 45 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 46 | * @param string |
| 47 | * @return mixed value that is stored/FALSE on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 48 | */ |
| 49 | public function get($id) |
| 50 | { |
Andrey Andreev | a2b0677 | 2012-11-20 15:09:46 +0200 | [diff] [blame] | 51 | $success = FALSE; |
| 52 | $data = apc_fetch($id, $success); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 53 | |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 54 | if ($success === TRUE) |
| 55 | { |
| 56 | return is_array($data) |
| 57 | ? unserialize($data[0]) |
| 58 | : $data; |
| 59 | } |
| 60 | |
| 61 | return FALSE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 62 | } |
| 63 | |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 64 | // ------------------------------------------------------------------------ |
| 65 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 66 | /** |
| 67 | * Cache Save |
| 68 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 69 | * @param string $id Cache ID |
| 70 | * @param mixed $data Data to store |
| 71 | * @param int $ttol Length of time (in seconds) to cache the data |
| 72 | * @param bool $raw Whether to store the raw value |
| 73 | * @return bool TRUE on success, FALSE on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 74 | */ |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 75 | public function save($id, $data, $ttl = 60, $raw = FALSE) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 76 | { |
Sean Fisher | 7bb95df | 2012-01-16 09:23:14 -0500 | [diff] [blame] | 77 | $ttl = (int) $ttl; |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 78 | |
| 79 | return apc_store( |
| 80 | $id, |
| 81 | ($raw === TRUE ? $data : array(serialize($data), time(), $ttl)), |
| 82 | $ttl |
| 83 | ); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 84 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 85 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 86 | // ------------------------------------------------------------------------ |
| 87 | |
| 88 | /** |
| 89 | * Delete from Cache |
| 90 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 91 | * @param mixed unique identifier of the item in the cache |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 92 | * @return bool true on success/false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 93 | */ |
| 94 | public function delete($id) |
| 95 | { |
| 96 | return apc_delete($id); |
| 97 | } |
| 98 | |
| 99 | // ------------------------------------------------------------------------ |
| 100 | |
| 101 | /** |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 102 | * Increment a raw value |
| 103 | * |
| 104 | * @param string $id Cache ID |
| 105 | * @param int $offset Step/value to add |
| 106 | * @return mixed New value on success or FALSE on failure |
| 107 | */ |
| 108 | public function increment($id, $offset = 1) |
| 109 | { |
| 110 | return apc_inc($id, $offset); |
| 111 | } |
| 112 | |
| 113 | // ------------------------------------------------------------------------ |
| 114 | |
| 115 | /** |
| 116 | * Decrement a raw value |
| 117 | * |
| 118 | * @param string $id Cache ID |
| 119 | * @param int $offset Step/value to reduce by |
| 120 | * @return mixed New value on success or FALSE on failure |
| 121 | */ |
| 122 | public function decrement($id, $offset = 1) |
| 123 | { |
| 124 | return apc_dec($id, $offset); |
| 125 | } |
| 126 | |
| 127 | // ------------------------------------------------------------------------ |
| 128 | |
| 129 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 130 | * Clean the cache |
| 131 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 132 | * @return bool false on failure/true on success |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 133 | */ |
| 134 | public function clean() |
| 135 | { |
| 136 | return apc_clear_cache('user'); |
| 137 | } |
| 138 | |
| 139 | // ------------------------------------------------------------------------ |
| 140 | |
| 141 | /** |
| 142 | * Cache Info |
| 143 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 144 | * @param string user/filehits |
| 145 | * @return mixed array on success, false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 146 | */ |
| 147 | public function cache_info($type = NULL) |
| 148 | { |
| 149 | return apc_cache_info($type); |
| 150 | } |
| 151 | |
| 152 | // ------------------------------------------------------------------------ |
| 153 | |
| 154 | /** |
| 155 | * Get Cache Metadata |
| 156 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 157 | * @param mixed key to get cache metadata on |
| 158 | * @return mixed array on success/false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 159 | */ |
| 160 | public function get_metadata($id) |
| 161 | { |
Andrey Andreev | a2b0677 | 2012-11-20 15:09:46 +0200 | [diff] [blame] | 162 | $success = FALSE; |
| 163 | $stored = apc_fetch($id, $success); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 164 | |
Andrey Andreev | a2b0677 | 2012-11-20 15:09:46 +0200 | [diff] [blame] | 165 | if ($success === FALSE OR count($stored) !== 3) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 166 | { |
| 167 | return FALSE; |
| 168 | } |
| 169 | |
Greg Aker | 999e747 | 2011-01-29 16:16:58 -0600 | [diff] [blame] | 170 | list($data, $time, $ttl) = $stored; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 171 | |
| 172 | return array( |
| 173 | 'expire' => $time + $ttl, |
| 174 | 'mtime' => $time, |
Andrey Andreev | a2b0677 | 2012-11-20 15:09:46 +0200 | [diff] [blame] | 175 | 'data' => unserialize($data) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 176 | ); |
| 177 | } |
| 178 | |
| 179 | // ------------------------------------------------------------------------ |
| 180 | |
| 181 | /** |
| 182 | * is_supported() |
| 183 | * |
| 184 | * 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] | 185 | * |
| 186 | * @return bool |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 187 | */ |
| 188 | public function is_supported() |
| 189 | { |
Andrey Andreev | f627474 | 2014-02-20 18:05:58 +0200 | [diff] [blame] | 190 | if ( ! extension_loaded('apc') OR ! ini_get('apc.enabled')) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 191 | { |
Tyler Brownell | d967f72 | 2013-07-29 19:06:52 -0400 | [diff] [blame] | 192 | log_message('debug', 'The APC PHP extension must be loaded to use APC Cache.'); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 193 | return FALSE; |
| 194 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 195 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 196 | return TRUE; |
| 197 | } |
| 198 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 199 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 200 | |
| 201 | /* End of file Cache_apc.php */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 202 | /* Location: ./system/libraries/Cache/drivers/Cache_apc.php */ |