1: <?php
2:
3: namespace Budabot\Core;
4:
5: use Exception;
6:
7: 8: 9:
10: class CacheManager {
11:
12:
13: public $chatBot;
14:
15:
16: public $http;
17:
18:
19: public $util;
20:
21: private $cacheDir;
22:
23:
24: public function init() {
25: $this->cacheDir = $this->chatBot->vars["cachefolder"];
26:
27:
28: if (!dir($this->cacheDir)) {
29: mkdir($this->cacheDir, 0777);
30: }
31: }
32:
33: public function lookup($url, $groupName, $filename, $isValidCallback, $maxCacheAge = 86400, $forceUpdate = false) {
34: if (empty($groupName)) {
35: throw new Exception("Cache group name cannot be empty");
36: }
37:
38: $cacheResult = new CacheResult();
39:
40:
41: if (!$forceUpdate && $this->cacheExists($groupName, $filename)) {
42: $cacheAge = $this->getCacheAge($groupName, $filename);
43: if ($cacheAge < $maxCacheAge) {
44: $data = $this->retrieve($groupName, $filename);
45: if (call_user_func($isValidCallback, $data)) {
46: $cacheResult->data = $data;
47: $cacheResult->cacheAge = $cacheAge;
48: $cacheResult->usedCache = true;
49: $cacheResult->oldCache = false;
50: $cacheResult->success = true;
51: } else {
52: unset($data);
53: $this->remove($groupName, $filename);
54: }
55: }
56: }
57:
58:
59: if ($cacheResult->success !== true) {
60: $response = $this->http->get($url)->waitAndReturnResponse();
61: $data = $response->body;
62: if (call_user_func($isValidCallback, $data)) {
63: $cacheResult->data = $data;
64: $cacheResult->cacheAge = 0;
65: $cacheResult->usedCache = false;
66: $cacheResult->oldCache = false;
67: $cacheResult->success = true;
68: } else {
69: unset($data);
70: }
71: }
72:
73:
74: if ($cacheResult->success !== true && $this->cacheExists($groupName, $filename)) {
75: $data = $this->retrieve($groupName, $filename);
76: if (call_user_func($isValidCallback, $data)) {
77: $cacheResult->data = $data;
78: $cacheResult->cacheAge = $this->getCacheAge($groupName, $filename);
79: $cacheResult->usedCache = true;
80: $cacheResult->oldCache = true;
81: $cacheResult->success = true;
82: } else {
83: unset($data);
84: $this->remove($groupName, $filename);
85: }
86: }
87:
88:
89: if ($cacheResult->usedCache === false && $cacheResult->success === true) {
90: $this->store($groupName, $filename, $cacheResult->data);
91: }
92:
93: return $cacheResult;
94: }
95:
96: public function store($groupName, $filename, $contents) {
97: if (!dir($this->cacheDir . '/' . $groupName)) {
98: mkdir($this->cacheDir . '/' . $groupName, 0777);
99: }
100:
101: $cacheFile = "$this->cacheDir/$groupName/$filename";
102:
103:
104:
105: @unlink($cacheFile);
106:
107: $fp = fopen($cacheFile, "w");
108: fwrite($fp, $contents);
109: fclose($fp);
110: }
111:
112: public function retrieve($groupName, $filename) {
113: $cacheFile = "{$this->cacheDir}/$groupName/$filename";
114:
115: if (file_exists($cacheFile)) {
116: return file_get_contents($cacheFile);
117: } else {
118: return null;
119: }
120: }
121:
122: public function getCacheAge($groupName, $filename) {
123: $cacheFile = "$this->cacheDir/$groupName/$filename";
124:
125: if (file_exists($cacheFile)) {
126: return time() - filemtime($cacheFile);
127: } else {
128: return null;
129: }
130: }
131:
132: public function cacheExists($groupName, $filename) {
133: $cacheFile = "$this->cacheDir/$groupName/$filename";
134:
135: return file_exists($cacheFile);
136: }
137:
138: public function remove($groupName, $filename) {
139: $cacheFile = "$this->cacheDir/$groupName/$filename";
140:
141: @unlink($cacheFile);
142: }
143:
144: public function getFilesInGroup($groupName) {
145: $path = "$this->cacheDir/$groupName/";
146:
147: return $this->util->getFilesInDirectory($path);
148: }
149:
150: public function getGroups() {
151: return $this->util->getDirectoriesInDirectory($this->cacheDir);
152: }
153: }
154:
155: class CacheResult {
156: public $success = false;
157: public $usedCache = false;
158: public $oldCache = false;
159: public $cacheAge = 0;
160: public $data;
161: }
162: