blob: 5f373ba17f6c8c2903bc2d98ae7e93c5c35c7400 [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 Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreev125ef472016-01-11 12:33:00 +020032 * @copyright Copyright (c) 2014 - 2016, 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 Andreev7d4ea072011-12-25 19:23:50 +020052 * Get
Greg Akerbde25d92010-12-21 09:31:21 -060053 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030054 * Look for a value in the cache. If it exists, return the data
Greg Akerbde25d92010-12-21 09:31:21 -060055 * if not, return FALSE
56 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030057 * @param string
58 * @return mixed value that is stored/FALSE on failure
Greg Akerbde25d92010-12-21 09:31:21 -060059 */
60 public function get($id)
61 {
Andrey Andreeva2b06772012-11-20 15:09:46 +020062 $success = FALSE;
63 $data = apc_fetch($id, $success);
Greg Akerbde25d92010-12-21 09:31:21 -060064
Andrey Andreev43d7fa72014-01-09 17:29:45 +020065 if ($success === TRUE)
66 {
67 return is_array($data)
68 ? unserialize($data[0])
69 : $data;
70 }
71
72 return FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -060073 }
74
Andrey Andreev7d4ea072011-12-25 19:23:50 +020075 // ------------------------------------------------------------------------
76
Greg Akerbde25d92010-12-21 09:31:21 -060077 /**
78 * Cache Save
79 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +020080 * @param string $id Cache ID
81 * @param mixed $data Data to store
82 * @param int $ttol Length of time (in seconds) to cache the data
83 * @param bool $raw Whether to store the raw value
84 * @return bool TRUE on success, FALSE on failure
Greg Akerbde25d92010-12-21 09:31:21 -060085 */
Andrey Andreev43d7fa72014-01-09 17:29:45 +020086 public function save($id, $data, $ttl = 60, $raw = FALSE)
Greg Akerbde25d92010-12-21 09:31:21 -060087 {
Sean Fisher7bb95df2012-01-16 09:23:14 -050088 $ttl = (int) $ttl;
Andrey Andreev43d7fa72014-01-09 17:29:45 +020089
90 return apc_store(
91 $id,
92 ($raw === TRUE ? $data : array(serialize($data), time(), $ttl)),
93 $ttl
94 );
Greg Akerbde25d92010-12-21 09:31:21 -060095 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020096
Greg Akerbde25d92010-12-21 09:31:21 -060097 // ------------------------------------------------------------------------
98
99 /**
100 * Delete from Cache
101 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300102 * @param mixed unique identifier of the item in the cache
Timothy Warren0688ac92012-04-20 10:25:04 -0400103 * @return bool true on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600104 */
105 public function delete($id)
106 {
107 return apc_delete($id);
108 }
109
110 // ------------------------------------------------------------------------
111
112 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200113 * Increment a raw value
114 *
115 * @param string $id Cache ID
116 * @param int $offset Step/value to add
117 * @return mixed New value on success or FALSE on failure
118 */
119 public function increment($id, $offset = 1)
120 {
121 return apc_inc($id, $offset);
122 }
123
124 // ------------------------------------------------------------------------
125
126 /**
127 * Decrement a raw value
128 *
129 * @param string $id Cache ID
130 * @param int $offset Step/value to reduce by
131 * @return mixed New value on success or FALSE on failure
132 */
133 public function decrement($id, $offset = 1)
134 {
135 return apc_dec($id, $offset);
136 }
137
138 // ------------------------------------------------------------------------
139
140 /**
Greg Akerbde25d92010-12-21 09:31:21 -0600141 * Clean the cache
142 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300143 * @return bool false on failure/true on success
Greg Akerbde25d92010-12-21 09:31:21 -0600144 */
145 public function clean()
146 {
147 return apc_clear_cache('user');
148 }
149
150 // ------------------------------------------------------------------------
151
152 /**
153 * Cache Info
154 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300155 * @param string user/filehits
156 * @return mixed array on success, false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600157 */
158 public function cache_info($type = NULL)
159 {
160 return apc_cache_info($type);
161 }
162
163 // ------------------------------------------------------------------------
164
165 /**
166 * Get Cache Metadata
167 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300168 * @param mixed key to get cache metadata on
169 * @return mixed array on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600170 */
171 public function get_metadata($id)
172 {
Andrey Andreeva2b06772012-11-20 15:09:46 +0200173 $success = FALSE;
174 $stored = apc_fetch($id, $success);
Greg Akerbde25d92010-12-21 09:31:21 -0600175
Andrey Andreeva2b06772012-11-20 15:09:46 +0200176 if ($success === FALSE OR count($stored) !== 3)
Greg Akerbde25d92010-12-21 09:31:21 -0600177 {
178 return FALSE;
179 }
180
Greg Aker999e7472011-01-29 16:16:58 -0600181 list($data, $time, $ttl) = $stored;
Greg Akerbde25d92010-12-21 09:31:21 -0600182
183 return array(
184 'expire' => $time + $ttl,
185 'mtime' => $time,
Andrey Andreeva2b06772012-11-20 15:09:46 +0200186 'data' => unserialize($data)
Greg Akerbde25d92010-12-21 09:31:21 -0600187 );
188 }
189
190 // ------------------------------------------------------------------------
191
192 /**
193 * is_supported()
194 *
195 * Check to see if APC is available on this system, bail if it isn't.
Andrey Andreevb24b0332012-03-26 15:34:39 +0300196 *
197 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600198 */
199 public function is_supported()
200 {
Andrey Andreevf6274742014-02-20 18:05:58 +0200201 if ( ! extension_loaded('apc') OR ! ini_get('apc.enabled'))
Greg Akerbde25d92010-12-21 09:31:21 -0600202 {
Tyler Brownelld967f722013-07-29 19:06:52 -0400203 log_message('debug', 'The APC PHP extension must be loaded to use APC Cache.');
Greg Akerbde25d92010-12-21 09:31:21 -0600204 return FALSE;
205 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200206
Greg Akerbde25d92010-12-21 09:31:21 -0600207 return TRUE;
208 }
209
Greg Akerbde25d92010-12-21 09:31:21 -0600210}