blob: de849c7e8df00a1cedb73a79815a3edb4d66c649 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Greg Akerbde25d92010-12-21 09:31:21 -06002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Greg Akerbde25d92010-12-21 09:31:21 -06006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev7d4ea072011-12-25 19:23:50 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev7d4ea072011-12-25 19:23:50 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Akerbde25d92010-12-21 09:31:21 -060019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Greg Akerbde25d92010-12-21 09:31:21 -060023 * @link http://codeigniter.com
24 * @since Version 2.0
Andrey Andreev7d4ea072011-12-25 19:23:50 +020025 * @filesource
Greg Akerbde25d92010-12-21 09:31:21 -060026 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Greg Akerbde25d92010-12-21 09:31:21 -060028
Greg Akerbde25d92010-12-21 09:31:21 -060029/**
Andrey Andreev7d4ea072011-12-25 19:23:50 +020030 * CodeIgniter APC Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060031 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Core
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Andrey Andreev7d4ea072011-12-25 19:23:50 +020036 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060037 */
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010038class CI_Cache_apc extends CI_Driver {
Greg Akerbde25d92010-12-21 09:31:21 -060039
40 /**
Andrey Andreev7d4ea072011-12-25 19:23:50 +020041 * Get
Greg Akerbde25d92010-12-21 09:31:21 -060042 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030043 * Look for a value in the cache. If it exists, return the data
Greg Akerbde25d92010-12-21 09:31:21 -060044 * if not, return FALSE
45 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030046 * @param string
47 * @return mixed value that is stored/FALSE on failure
Greg Akerbde25d92010-12-21 09:31:21 -060048 */
49 public function get($id)
50 {
Andrey Andreeva2b06772012-11-20 15:09:46 +020051 $success = FALSE;
52 $data = apc_fetch($id, $success);
Greg Akerbde25d92010-12-21 09:31:21 -060053
Andrey Andreev43d7fa72014-01-09 17:29:45 +020054 if ($success === TRUE)
55 {
56 return is_array($data)
57 ? unserialize($data[0])
58 : $data;
59 }
60
61 return FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -060062 }
63
Andrey Andreev7d4ea072011-12-25 19:23:50 +020064 // ------------------------------------------------------------------------
65
Greg Akerbde25d92010-12-21 09:31:21 -060066 /**
67 * Cache Save
68 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +020069 * @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 Akerbde25d92010-12-21 09:31:21 -060074 */
Andrey Andreev43d7fa72014-01-09 17:29:45 +020075 public function save($id, $data, $ttl = 60, $raw = FALSE)
Greg Akerbde25d92010-12-21 09:31:21 -060076 {
Sean Fisher7bb95df2012-01-16 09:23:14 -050077 $ttl = (int) $ttl;
Andrey Andreev43d7fa72014-01-09 17:29:45 +020078
79 return apc_store(
80 $id,
81 ($raw === TRUE ? $data : array(serialize($data), time(), $ttl)),
82 $ttl
83 );
Greg Akerbde25d92010-12-21 09:31:21 -060084 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020085
Greg Akerbde25d92010-12-21 09:31:21 -060086 // ------------------------------------------------------------------------
87
88 /**
89 * Delete from Cache
90 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030091 * @param mixed unique identifier of the item in the cache
Timothy Warren0688ac92012-04-20 10:25:04 -040092 * @return bool true on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -060093 */
94 public function delete($id)
95 {
96 return apc_delete($id);
97 }
98
99 // ------------------------------------------------------------------------
100
101 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200102 * 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 Akerbde25d92010-12-21 09:31:21 -0600130 * Clean the cache
131 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300132 * @return bool false on failure/true on success
Greg Akerbde25d92010-12-21 09:31:21 -0600133 */
134 public function clean()
135 {
136 return apc_clear_cache('user');
137 }
138
139 // ------------------------------------------------------------------------
140
141 /**
142 * Cache Info
143 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300144 * @param string user/filehits
145 * @return mixed array on success, false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600146 */
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 Andreevb24b0332012-03-26 15:34:39 +0300157 * @param mixed key to get cache metadata on
158 * @return mixed array on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600159 */
160 public function get_metadata($id)
161 {
Andrey Andreeva2b06772012-11-20 15:09:46 +0200162 $success = FALSE;
163 $stored = apc_fetch($id, $success);
Greg Akerbde25d92010-12-21 09:31:21 -0600164
Andrey Andreeva2b06772012-11-20 15:09:46 +0200165 if ($success === FALSE OR count($stored) !== 3)
Greg Akerbde25d92010-12-21 09:31:21 -0600166 {
167 return FALSE;
168 }
169
Greg Aker999e7472011-01-29 16:16:58 -0600170 list($data, $time, $ttl) = $stored;
Greg Akerbde25d92010-12-21 09:31:21 -0600171
172 return array(
173 'expire' => $time + $ttl,
174 'mtime' => $time,
Andrey Andreeva2b06772012-11-20 15:09:46 +0200175 'data' => unserialize($data)
Greg Akerbde25d92010-12-21 09:31:21 -0600176 );
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 Andreevb24b0332012-03-26 15:34:39 +0300185 *
186 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600187 */
188 public function is_supported()
189 {
Andrey Andreevf6274742014-02-20 18:05:58 +0200190 if ( ! extension_loaded('apc') OR ! ini_get('apc.enabled'))
Greg Akerbde25d92010-12-21 09:31:21 -0600191 {
Tyler Brownelld967f722013-07-29 19:06:52 -0400192 log_message('debug', 'The APC PHP extension must be loaded to use APC Cache.');
Greg Akerbde25d92010-12-21 09:31:21 -0600193 return FALSE;
194 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200195
Greg Akerbde25d92010-12-21 09:31:21 -0600196 return TRUE;
197 }
198
Greg Akerbde25d92010-12-21 09:31:21 -0600199}
Greg Akerbde25d92010-12-21 09:31:21 -0600200
201/* End of file Cache_apc.php */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300202/* Location: ./system/libraries/Cache/drivers/Cache_apc.php */