1 <?php
2 3 4 5 6 7 8 9 10
11
12 namespace Neevo\Cache;
13
14
15 16 17 18
19 class FileStorage implements StorageInterface {
20
21
22
23 private $filename;
24
25
26 private $data = array();
27
28
29 public function __construct($filename){
30 $this->filename = $filename;
31 $this->data = (array) unserialize(@file_get_contents($filename));
32 }
33
34
35 public function fetch($key){
36 return array_key_exists($key, $this->data) ? $this->data[$key] : null;
37 }
38
39
40 public function store($key, $value){
41 if(!array_key_exists($key, $this->data) || $this->data[$key] !== $value){
42 $this->data[$key] = $value;
43 @file_put_contents($this->filename, serialize($this->data), LOCK_EX);
44 }
45 }
46
47
48 public function flush(){
49 $this->data = array();
50 return @file_put_contents($this->filename, serialize($this->data), LOCK_EX);
51 }
52
53
54 }
55
56