1
+ <?php
2
+
3
+ namespace Luracast \Restler ;
4
+
5
+ use Luracast \Restler \iCache ;
6
+
7
+ /**
8
+ * Class ApcCache provides an APC based cache for Restler
9
+ *
10
+ * @category Framework
11
+ * @package Restler
12
+ * @author Joel R. Simpson <joel.simpson@gmail.com>
13
+ * @copyright 2013 Luracast
14
+ * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
15
+ * @link http://luracast.com/products/restler/
16
+ *
17
+ */
18
+ class ApcCache implements iCache
19
+ {
20
+ /**
21
+ * The namespace that all of the cached entries will be stored under. This allows multiple APIs to run concurrently.
22
+ *
23
+ * @var string
24
+ */
25
+ static public $ namespace = 'restler ' ;
26
+
27
+ /**
28
+ * store data in the cache
29
+ *
30
+ *
31
+ * @param string $name
32
+ * @param mixed $data
33
+ *
34
+ * @return boolean true if successful
35
+ */
36
+ public function set ($ name , $ data )
37
+ {
38
+ function_exists ('apc_store ' ) || $ this ->apcNotAvailable ();
39
+
40
+ try {
41
+ return apc_store (self ::$ namespace . "- " . $ name , $ data );
42
+ } catch
43
+ (\Exception $ exception ) {
44
+ return false ;
45
+ }
46
+ }
47
+
48
+ private function apcNotAvailable ()
49
+ {
50
+ throw new \Exception ('APC is not available for use as Restler Cache. Please make sure the module is installed. http://php.net/manual/en/apc.installation.php ' );
51
+ }
52
+
53
+ /**
54
+ * retrieve data from the cache
55
+ *
56
+ *
57
+ * @param string $name
58
+ * @param bool $ignoreErrors
59
+ *
60
+ * @throws \Exception
61
+ * @return mixed
62
+ */
63
+ public function get ($ name , $ ignoreErrors = false )
64
+ {
65
+ function_exists ('apc_fetch ' ) || $ this ->apcNotAvailable ();
66
+
67
+ try {
68
+ return apc_fetch (self ::$ namespace . "- " . $ name );
69
+ } catch (\Exception $ exception ) {
70
+ if (!$ ignoreErrors ) {
71
+ throw $ exception ;
72
+ }
73
+ return null ;
74
+ }
75
+ }
76
+
77
+ /**
78
+ * delete data from the cache
79
+ *
80
+ *
81
+ * @param string $name
82
+ * @param bool $ignoreErrors
83
+ *
84
+ * @throws \Exception
85
+ * @return boolean true if successful
86
+ */
87
+ public function clear ($ name , $ ignoreErrors = false )
88
+ {
89
+ function_exists ('apc_delete ' ) || $ this ->apcNotAvailable ();
90
+
91
+ try {
92
+ apc_delete (self ::$ namespace . "- " . $ name );
93
+ } catch (\Exception $ exception ) {
94
+ if (!$ ignoreErrors ) {
95
+ throw $ exception ;
96
+ }
97
+ }
98
+ }
99
+
100
+ /**
101
+ * check if the given name is cached
102
+ *
103
+ *
104
+ * @param string $name
105
+ *
106
+ * @return boolean true if cached
107
+ */
108
+ public function isCached ($ name )
109
+ {
110
+ function_exists ('apc_exists ' ) || $ this ->apcNotAvailable ();
111
+ return apc_exists (self ::$ namespace . "- " . $ name );
112
+ }
113
+
114
+ }
0 commit comments