blob: b4279bb9d246491858c829f7ad9b8dfa8271d7ea [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Mike Davies03a57652012-02-29 17:52:36 -05002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Mike Davies03a57652012-02-29 17:52:36 -05006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Mike Davies100934c2012-03-02 19:18:22 -05008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Mike Davies100934c2012-03-02 19:18:22 -050010 *
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:
Mike Davies100934c2012-03-02 19:18:22 -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 3.0.0
Mike Davies100934c2012-03-02 19:18:22 -050036 * @filesource
Mike Davies03a57652012-02-29 17:52:36 -050037 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Mike Davies03a57652012-02-29 17:52:36 -050039
Mike Davies03a57652012-02-29 17:52:36 -050040/**
41 * CodeIgniter Wincache Caching Class
42 *
43 * Read more about Wincache functions here:
44 * http://www.php.net/manual/en/ref.wincache.php
45 *
46 * @package CodeIgniter
47 * @subpackage Libraries
48 * @category Core
49 * @author Mike Murkovic
Andrey Andreev6b535f52012-03-12 22:19:13 +020050 * @link
Mike Davies03a57652012-02-29 17:52:36 -050051 */
Mike Davies03a57652012-02-29 17:52:36 -050052class CI_Cache_wincache extends CI_Driver {
53
54 /**
Andrey Andreev1be89872016-03-11 18:12:57 +020055 * Class constructor
56 *
57 * Only present so that an error message is logged
58 * if APC is not available.
59 *
60 * @return void
61 */
62 public function __construct()
63 {
64 if ( ! $this->is_supported())
65 {
66 log_message('error', 'Cache: Failed to initialize Wincache; extension not loaded/enabled?');
67 }
68 }
69
70 // ------------------------------------------------------------------------
71
72 /**
Andrey Andreev6b535f52012-03-12 22:19:13 +020073 * Get
Mike Davies03a57652012-02-29 17:52:36 -050074 *
Andrey Andreev6b535f52012-03-12 22:19:13 +020075 * Look for a value in the cache. If it exists, return the data,
Mike Davies03a57652012-02-29 17:52:36 -050076 * if not, return FALSE
77 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +020078 * @param string $id Cache Ide
79 * @return mixed Value that is stored/FALSE on failure
Mike Davies03a57652012-02-29 17:52:36 -050080 */
81 public function get($id)
82 {
Mike Davies100934c2012-03-02 19:18:22 -050083 $success = FALSE;
84 $data = wincache_ucache_get($id, $success);
Andrey Andreev6b535f52012-03-12 22:19:13 +020085
86 // Success returned by reference from wincache_ucache_get()
Mike Davies100934c2012-03-02 19:18:22 -050087 return ($success) ? $data : FALSE;
Mike Davies03a57652012-02-29 17:52:36 -050088 }
89
Andrey Andreev6b535f52012-03-12 22:19:13 +020090 // ------------------------------------------------------------------------
91
Mike Davies03a57652012-02-29 17:52:36 -050092 /**
93 * Cache Save
94 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +020095 * @param string $id Cache ID
96 * @param mixed $data Data to store
97 * @param int $ttl Time to live (in seconds)
98 * @param bool $raw Whether to store the raw value (unused)
Andrey Andreevb24b0332012-03-26 15:34:39 +030099 * @return bool true on success/false on failure
Mike Davies03a57652012-02-29 17:52:36 -0500100 */
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200101 public function save($id, $data, $ttl = 60, $raw = FALSE)
Mike Davies03a57652012-02-29 17:52:36 -0500102 {
103 return wincache_ucache_set($id, $data, $ttl);
104 }
Andrey Andreev6b535f52012-03-12 22:19:13 +0200105
Mike Davies03a57652012-02-29 17:52:36 -0500106 // ------------------------------------------------------------------------
107
108 /**
109 * Delete from Cache
110 *
Andrey Andreev6b535f52012-03-12 22:19:13 +0200111 * @param mixed unique identifier of the item in the cache
Timothy Warren0688ac92012-04-20 10:25:04 -0400112 * @return bool true on success/false on failure
Mike Davies03a57652012-02-29 17:52:36 -0500113 */
114 public function delete($id)
115 {
116 return wincache_ucache_delete($id);
117 }
118
119 // ------------------------------------------------------------------------
120
121 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200122 * Increment a raw value
123 *
124 * @param string $id Cache ID
125 * @param int $offset Step/value to add
126 * @return mixed New value on success or FALSE on failure
127 */
128 public function increment($id, $offset = 1)
129 {
130 $success = FALSE;
131 $value = wincache_ucache_inc($id, $offset, $success);
132
133 return ($success === TRUE) ? $value : FALSE;
134 }
135
136 // ------------------------------------------------------------------------
137
138 /**
139 * Decrement a raw value
140 *
141 * @param string $id Cache ID
142 * @param int $offset Step/value to reduce by
143 * @return mixed New value on success or FALSE on failure
144 */
145 public function decrement($id, $offset = 1)
146 {
147 $success = FALSE;
148 $value = wincache_ucache_dec($id, $offset, $success);
149
150 return ($success === TRUE) ? $value : FALSE;
151 }
152
153 // ------------------------------------------------------------------------
154
155 /**
Mike Davies03a57652012-02-29 17:52:36 -0500156 * Clean the cache
157 *
Andrey Andreev6b535f52012-03-12 22:19:13 +0200158 * @return bool false on failure/true on success
Mike Davies03a57652012-02-29 17:52:36 -0500159 */
160 public function clean()
161 {
162 return wincache_ucache_clear();
163 }
164
165 // ------------------------------------------------------------------------
166
167 /**
168 * Cache Info
169 *
Andrey Andreev6b535f52012-03-12 22:19:13 +0200170 * @return mixed array on success, false on failure
Mike Davies03a57652012-02-29 17:52:36 -0500171 */
172 public function cache_info()
173 {
Andrey Andreev6b535f52012-03-12 22:19:13 +0200174 return wincache_ucache_info(TRUE);
Mike Davies03a57652012-02-29 17:52:36 -0500175 }
176
177 // ------------------------------------------------------------------------
178
179 /**
180 * Get Cache Metadata
181 *
Andrey Andreev6b535f52012-03-12 22:19:13 +0200182 * @param mixed key to get cache metadata on
183 * @return mixed array on success/false on failure
Mike Davies03a57652012-02-29 17:52:36 -0500184 */
185 public function get_metadata($id)
186 {
Andrey Andreev6b535f52012-03-12 22:19:13 +0200187 if ($stored = wincache_ucache_info(FALSE, $id))
Mike Davies100934c2012-03-02 19:18:22 -0500188 {
189 $age = $stored['ucache_entries'][1]['age_seconds'];
190 $ttl = $stored['ucache_entries'][1]['ttl_seconds'];
191 $hitcount = $stored['ucache_entries'][1]['hitcount'];
Mike Davies03a57652012-02-29 17:52:36 -0500192
Mike Davies100934c2012-03-02 19:18:22 -0500193 return array(
Andrey Andreev838a9d62012-12-03 14:37:47 +0200194 'expire' => $ttl - $age,
195 'hitcount' => $hitcount,
196 'age' => $age,
197 'ttl' => $ttl
Mike Davies100934c2012-03-02 19:18:22 -0500198 );
199 }
Andrey Andreev6b535f52012-03-12 22:19:13 +0200200
201 return FALSE;
Mike Davies03a57652012-02-29 17:52:36 -0500202 }
203
204 // ------------------------------------------------------------------------
205
206 /**
207 * is_supported()
208 *
209 * Check to see if WinCache is available on this system, bail if it isn't.
Andrey Andreev6b535f52012-03-12 22:19:13 +0200210 *
211 * @return bool
Mike Davies03a57652012-02-29 17:52:36 -0500212 */
213 public function is_supported()
214 {
Andrey Andreev1be89872016-03-11 18:12:57 +0200215 return (extension_loaded('wincache') && ini_get('wincache.ucenabled'));
Mike Davies03a57652012-02-29 17:52:36 -0500216 }
Mike Davies03a57652012-02-29 17:52:36 -0500217}