blob: a9ad6ed19f5e57b28d49390bbcd01e83c0c3dfd7 [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
Andrey Andreevc5536aa2012-11-01 17:33:58 +020021 * @copyright Copyright (c) 2008 - 2012, 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 Andreeva2b06772012-11-20 15:09:46 +020054 return ($success === TRUE && is_array($data))
55 ? unserialize($data[0]) : FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -060056 }
57
Andrey Andreev7d4ea072011-12-25 19:23:50 +020058 // ------------------------------------------------------------------------
59
Greg Akerbde25d92010-12-21 09:31:21 -060060 /**
61 * Cache Save
62 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030063 * @param string Unique Key
64 * @param mixed Data to store
65 * @param int Length of time (in seconds) to cache the data
Greg Akerbde25d92010-12-21 09:31:21 -060066 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030067 * @return bool true on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -060068 */
69 public function save($id, $data, $ttl = 60)
70 {
Sean Fisher7bb95df2012-01-16 09:23:14 -050071 $ttl = (int) $ttl;
Andrey Andreeva2b06772012-11-20 15:09:46 +020072 return apc_store($id, array(serialize($data), time(), $ttl), $ttl);
Greg Akerbde25d92010-12-21 09:31:21 -060073 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020074
Greg Akerbde25d92010-12-21 09:31:21 -060075 // ------------------------------------------------------------------------
76
77 /**
78 * Delete from Cache
79 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030080 * @param mixed unique identifier of the item in the cache
Timothy Warren0688ac92012-04-20 10:25:04 -040081 * @return bool true on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -060082 */
83 public function delete($id)
84 {
85 return apc_delete($id);
86 }
87
88 // ------------------------------------------------------------------------
89
90 /**
91 * Clean the cache
92 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030093 * @return bool false on failure/true on success
Greg Akerbde25d92010-12-21 09:31:21 -060094 */
95 public function clean()
96 {
97 return apc_clear_cache('user');
98 }
99
100 // ------------------------------------------------------------------------
101
102 /**
103 * Cache Info
104 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300105 * @param string user/filehits
106 * @return mixed array on success, false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600107 */
108 public function cache_info($type = NULL)
109 {
110 return apc_cache_info($type);
111 }
112
113 // ------------------------------------------------------------------------
114
115 /**
116 * Get Cache Metadata
117 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300118 * @param mixed key to get cache metadata on
119 * @return mixed array on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600120 */
121 public function get_metadata($id)
122 {
Andrey Andreeva2b06772012-11-20 15:09:46 +0200123 $success = FALSE;
124 $stored = apc_fetch($id, $success);
Greg Akerbde25d92010-12-21 09:31:21 -0600125
Andrey Andreeva2b06772012-11-20 15:09:46 +0200126 if ($success === FALSE OR count($stored) !== 3)
Greg Akerbde25d92010-12-21 09:31:21 -0600127 {
128 return FALSE;
129 }
130
Greg Aker999e7472011-01-29 16:16:58 -0600131 list($data, $time, $ttl) = $stored;
Greg Akerbde25d92010-12-21 09:31:21 -0600132
133 return array(
134 'expire' => $time + $ttl,
135 'mtime' => $time,
Andrey Andreeva2b06772012-11-20 15:09:46 +0200136 'data' => unserialize($data)
Greg Akerbde25d92010-12-21 09:31:21 -0600137 );
138 }
139
140 // ------------------------------------------------------------------------
141
142 /**
143 * is_supported()
144 *
145 * Check to see if APC is available on this system, bail if it isn't.
Andrey Andreevb24b0332012-03-26 15:34:39 +0300146 *
147 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600148 */
149 public function is_supported()
150 {
Andrey Andreevb24b0332012-03-26 15:34:39 +0300151 if ( ! extension_loaded('apc') OR ! (bool) @ini_get('apc.enabled'))
Greg Akerbde25d92010-12-21 09:31:21 -0600152 {
153 log_message('error', 'The APC PHP extension must be loaded to use APC Cache.');
154 return FALSE;
155 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200156
Greg Akerbde25d92010-12-21 09:31:21 -0600157 return TRUE;
158 }
159
Greg Akerbde25d92010-12-21 09:31:21 -0600160}
Greg Akerbde25d92010-12-21 09:31:21 -0600161
162/* End of file Cache_apc.php */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300163/* Location: ./system/libraries/Cache/drivers/Cache_apc.php */