1 <?php
2 3 4 5 6 7 8 9 10
11
12
13 14 15 16 17
18 interface INeevoCache {
19
20
21 22 23 24 25
26 public function fetch($key);
27
28
29 30 31 32 33 34
35 public function store($key, $value);
36
37
38 }
39
40
41
42 43 44 45 46 47
48 class NeevoCacheMemory implements INeevoCache {
49
50
51
52 private $data = array();
53
54
55 public function fetch($key){
56 return array_key_exists($key, $this->data) ? $this->data[$key] : null;
57 }
58
59
60 public function store($key, $value){
61 $this->data[$key] = $value;
62 }
63
64
65 public function flush(){
66 return !$this->data = array();
67 }
68
69
70 }
71
72
73
74 75 76 77 78
79 class NeevoCacheSession implements INeevoCache {
80
81
82 public function fetch($key){
83 return array_key_exists($key, $_SESSION['NeevoCache']) ? $_SESSION['NeevoCache'][$key] : null;
84 }
85
86
87 public function store($key, $value){
88 $_SESSION['NeevoCache'][$key] = $value;
89 }
90
91
92 public function flush(){
93 return !$_SESSION['NeevoCache'] = array();
94 }
95
96
97 }
98
99
100
101 102 103 104 105
106 class NeevoCacheFile implements INeevoCache {
107
108
109
110 private $filename;
111
112
113 private $data = array();
114
115
116 public function __construct($filename){
117 $this->filename = $filename;
118 $this->data = (array) unserialize(@file_get_contents($filename));
119 }
120
121
122 public function fetch($key){
123 return array_key_exists($key, $this->data) ? $this->data[$key] : null;
124 }
125
126
127 public function store($key, $value){
128 if(!array_key_exists($key, $this->data) || $this->data[$key] !== $value){
129 $this->data[$key] = $value;
130 @file_put_contents($this->filename, serialize($this->data), LOCK_EX);
131 }
132 }
133
134
135 public function flush(){
136 $this->data = array();
137 return @file_put_contents($this->filename, serialize($this->data), LOCK_EX);
138 }
139
140
141 }
142
143
144
145 146 147 148 149
150 class NeevoCacheMemcache implements INeevoCache {
151
152
153
154 private $memcache;
155
156 public function __construct(Memcache $memcache){
157 $this->memcache = $memcache;
158 }
159
160
161 public function fetch($key){
162 $value = $this->memcache->get("NeevoCache.$key");
163 return $value !== false ? $value : null;
164 }
165
166
167 public function store($key, $value){
168 $this->memcache->set("NeevoCache.$key", $value);
169 }
170
171
172 public function flush(){
173 return $this->memcache->flush();
174 }
175
176
177 }
178