blob: b42a832e3780f8b771e43fbca49bc1cd5e29c4b3 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Greg Akerbde25d92010-12-21 09:31:21 -06002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Greg Akerbde25d92010-12-21 09:31:21 -06006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev7d4ea072011-12-25 19:23:50 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev7d4ea072011-12-25 19:23:50 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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 Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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 Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 2.0.0
Andrey Andreev7d4ea072011-12-25 19:23:50 +020036 * @filesource
Greg Akerbde25d92010-12-21 09:31:21 -060037 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Greg Akerbde25d92010-12-21 09:31:21 -060039
Greg Akerbde25d92010-12-21 09:31:21 -060040/**
Andrey Andreev7d4ea072011-12-25 19:23:50 +020041 * CodeIgniter APC Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060042 *
43 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Core
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreev7d4ea072011-12-25 19:23:50 +020047 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060048 */
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010049class CI_Cache_apc extends CI_Driver {
Greg Akerbde25d92010-12-21 09:31:21 -060050
51 /**
Andrey Andreev1be89872016-03-11 18:12:57 +020052 * 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 Andreev7d4ea072011-12-25 19:23:50 +020070 * Get
Greg Akerbde25d92010-12-21 09:31:21 -060071 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030072 * Look for a value in the cache. If it exists, return the data
Greg Akerbde25d92010-12-21 09:31:21 -060073 * if not, return FALSE
74 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030075 * @param string
76 * @return mixed value that is stored/FALSE on failure
Greg Akerbde25d92010-12-21 09:31:21 -060077 */
78 public function get($id)
79 {
Andrey Andreeva2b06772012-11-20 15:09:46 +020080 $success = FALSE;
81 $data = apc_fetch($id, $success);
Greg Akerbde25d92010-12-21 09:31:21 -060082
Andrey Andreev88c1a592017-09-25 14:39:34 +030083 return ($success === TRUE) ? $data : FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -060084 }
85
Andrey Andreev7d4ea072011-12-25 19:23:50 +020086 // ------------------------------------------------------------------------
87
Greg Akerbde25d92010-12-21 09:31:21 -060088 /**
89 * Cache Save
90 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +020091 * @param string $id Cache ID
92 * @param mixed $data Data to store
Andrey Andreeve13fa9f2016-05-20 17:30:07 +030093 * @param int $ttl Length of time (in seconds) to cache the data
Andrey Andreev88c1a592017-09-25 14:39:34 +030094 * @param bool $raw Whether to store the raw value (unused)
Andrey Andreev43d7fa72014-01-09 17:29:45 +020095 * @return bool TRUE on success, FALSE on failure
Greg Akerbde25d92010-12-21 09:31:21 -060096 */
Andrey Andreev43d7fa72014-01-09 17:29:45 +020097 public function save($id, $data, $ttl = 60, $raw = FALSE)
Greg Akerbde25d92010-12-21 09:31:21 -060098 {
Andrey Andreev88c1a592017-09-25 14:39:34 +030099 return apc_store($id, $data, (int) $ttl);
Greg Akerbde25d92010-12-21 09:31:21 -0600100 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200101
Greg Akerbde25d92010-12-21 09:31:21 -0600102 // ------------------------------------------------------------------------
103
104 /**
105 * Delete from Cache
106 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300107 * @param mixed unique identifier of the item in the cache
Timothy Warren0688ac92012-04-20 10:25:04 -0400108 * @return bool true on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600109 */
110 public function delete($id)
111 {
112 return apc_delete($id);
113 }
114
115 // ------------------------------------------------------------------------
116
117 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200118 * Increment a raw value
119 *
120 * @param string $id Cache ID
121 * @param int $offset Step/value to add
122 * @return mixed New value on success or FALSE on failure
123 */
124 public function increment($id, $offset = 1)
125 {
126 return apc_inc($id, $offset);
127 }
128
129 // ------------------------------------------------------------------------
130
131 /**
132 * Decrement a raw value
133 *
134 * @param string $id Cache ID
135 * @param int $offset Step/value to reduce by
136 * @return mixed New value on success or FALSE on failure
137 */
138 public function decrement($id, $offset = 1)
139 {
140 return apc_dec($id, $offset);
141 }
142
143 // ------------------------------------------------------------------------
144
145 /**
Greg Akerbde25d92010-12-21 09:31:21 -0600146 * Clean the cache
147 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300148 * @return bool false on failure/true on success
Greg Akerbde25d92010-12-21 09:31:21 -0600149 */
150 public function clean()
151 {
152 return apc_clear_cache('user');
153 }
154
155 // ------------------------------------------------------------------------
156
157 /**
158 * Cache Info
159 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300160 * @param string user/filehits
161 * @return mixed array on success, false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600162 */
163 public function cache_info($type = NULL)
164 {
165 return apc_cache_info($type);
166 }
167
168 // ------------------------------------------------------------------------
169
170 /**
171 * Get Cache Metadata
172 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300173 * @param mixed key to get cache metadata on
174 * @return mixed array on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600175 */
176 public function get_metadata($id)
177 {
Andrey Andreev88c1a592017-09-25 14:39:34 +0300178 $cache_info = apc_cache_info('user', FALSE);
179 if (empty($cache_info) OR empty($cache_info['cache_list']))
Greg Akerbde25d92010-12-21 09:31:21 -0600180 {
181 return FALSE;
182 }
183
Andrey Andreev88c1a592017-09-25 14:39:34 +0300184 foreach ($cache_info['cache_list'] as &$entry)
185 {
186 if ($entry['info'] !== $id)
187 {
188 continue;
189 }
Greg Akerbde25d92010-12-21 09:31:21 -0600190
Andrey Andreev88c1a592017-09-25 14:39:34 +0300191 $success = FALSE;
192 $metadata = array(
193 'expire' => ($entry['ttl'] ? $entry['mtime'] + $entry['ttl'] : 0),
194 'mtime' => $entry['ttl'],
195 'data' => apc_fetch($id, $success)
196 );
197
198 return ($success === TRUE) ? $metadata : FALSE;
199 }
200
201 return FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -0600202 }
203
204 // ------------------------------------------------------------------------
205
206 /**
207 * is_supported()
208 *
209 * Check to see if APC is available on this system, bail if it isn't.
Andrey Andreevb24b0332012-03-26 15:34:39 +0300210 *
211 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600212 */
213 public function is_supported()
214 {
Andrey Andreev1be89872016-03-11 18:12:57 +0200215 return (extension_loaded('apc') && ini_get('apc.enabled'));
Greg Akerbde25d92010-12-21 09:31:21 -0600216 }
Greg Akerbde25d92010-12-21 09:31:21 -0600217}