This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.php
executable file
·70 lines (53 loc) · 1.58 KB
/
cache.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
<?php
namespace Accolade\Cashew;
class Cache {
public $cache_folder = '_cache/'; // cache store folder
public $cache_time = 1 * 60 * 60; // in hour
public function get_the_json($label, $url) {
if($data = $this->get_cache($label)){
$data = json_decode($data);
} else {
$data = $this->fetch_data($url);
$this->set_cache($label, $data);
$data = json_decode($data);
}
return $data;
}
public function set_cache($label, $data){
file_put_contents($this->cache_folder . $this->regex($label), $data);
}
public function get_cache($label){
if($this->is_cached($label)){
$filename = $this->cache_folder . $this->regex($label);
return file_get_contents($filename);
}
return false;
}
public function is_cached($label){
$filename = $this->cache_folder . $this->regex($label);
if(file_exists($filename) && (filemtime($filename) + $this->cache_time >= time())) return true;
return false;
}
//regex function to standardize a filename
private function regex($filename){
if (!file_exists($this->cache_folder)) {
mkdir($this->cache_folder, 0755, true);
}
return preg_replace('/[^0-9a-z\.\_\-]/i','', strtolower($filename));
}
// Fetch API data using CURL
public function fetch_data($url){
if(function_exists("curl_init")){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$content = curl_exec($ch);
curl_close($ch);
return $content;
} else {
return file_get_contents($url);
}
}
}