-
Notifications
You must be signed in to change notification settings - Fork 3
/
ApcCache.php
executable file
·144 lines (138 loc) · 4.95 KB
/
ApcCache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Simple Abstraction Class to APC.
*
* The Alternative PHP Cache (APC) is a free and open opcode cache for PHP.
* Its goal is to provide a free, open, and robust framework for caching and
* optimizing PHP intermediate code.
*
* @see http://www.php.net/manual/en/book.apc.php
*
* Copyright (c) 2010 Richard Castera
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace rcastera\Apc;
class ApcCache {
/**
* Retrieves cached information from APC's data store.
*
* @param string $type - If $type is "user", information about the user cache will be returned.
* @param boolean $limited - If $limited is true, the return value will exclude the individual
* list of cache entries. This is useful when trying to optimize calls
* for statistics gathering.
*
* @return array of cached data (and meta-data) or false on failure.
*/
public static function info($type = '', $limited = false)
{
try {
return apc_cache_info($type, $limited);
} catch (Exception $e) {
throw new Exception($e->getMessage(), $e->getCode());
}
}
/**
* Checks if APC key exists.
*
* @param mixed $key - A string, or an array of strings, that contain keys.
*
* @return mixed - Returns true if the key exists, otherwise false or if an
* array was passed to keys, then an array is returned that
* contains all existing keys, or an empty array if none exist.
*/
public static function exists($key = '')
{
try {
return apc_exists($key);
} catch (Exception $e) {
throw new Exception($e->getMessage(), $e->getCode());
}
}
/**
* Cache a variable in the data store.
*
* @param string $key - Store the variable using this name.
* @param string $data - The variable to store.
* @param string $ttl - Time To Live; store var in the cache for ttl seconds.
*
* @return boolean - Returns true on success or false on failure.
*/
public static function store($key, $data, $ttl = 0, $overwrite = false)
{
try {
if ($overwrite) {
return apc_store($key, $data, $ttl);
} else {
return apc_add($key, $data, $ttl);
}
} catch (Exception $e) {
throw new Exception($e->getMessage(), $e->getCode());
}
}
/**
* Fetch stored value in APC from key.
*
* @param string $key - The key used to store the value.
*
* @return boolean - The stored variable or array of variables on success; false on failure.
*/
public static function fetch($key = '')
{
try {
if (self::exists($key)) {
return apc_fetch($key);
} else {
return false;
}
} catch (Exception $e) {
throw new Exception($e->getMessage(), $e->getCode());
}
}
/**
* Removes a stored variable from the cache.
*
* @param string $key - The key used to store the value (with apc_store()).
*
* @return boolean - Returns true on success or false on failure.
*/
public static function delete($key = '')
{
try {
return apc_delete($key);
} catch (Exception $e) {
throw new Exception($e->getMessage(), $e->getCode());
}
}
/**
* Clears the APC cache.
*
* @param string $type - If $type is "user", the user cache will be cleared; otherwise,
* the system cache (cached files) will be cleared.
*
* @return boolean - Returns true on success or false on failure.
*/
public static function clear($type = '') {
try {
return apc_clear_cache($type);
} catch (Exception $e) {
throw new Exception($e->getMessage(), $e->getCode());
}
}
}