Skip to content

Commit 59fb384

Browse files
committed
implement basic cache system
1 parent 1e70134 commit 59fb384

2 files changed

Lines changed: 48 additions & 28 deletions

File tree

public_html/lib/module/rpcn/inc-rpcn-stats.php

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class RPCNStats {
44
private string $log_file;
55
private string $api_url;
66
private string $icons_json;
7+
private string $cache;
78

89
public int $total_users = 0;
910

@@ -34,11 +35,12 @@ class RPCNStats {
3435
"BLES01112" => "MRTC00016"
3536
];
3637

37-
public function __construct(string $games_json, string $log_file, string $api_url, string $icons_json) {
38+
public function __construct(string $games_json, string $log_file, string $api_url, string $icons_json, string $cache) {
3839
$this->games_json = $games_json;
3940
$this->log_file = $log_file;
4041
$this->api_url = $api_url;
4142
$this->icons_json = $icons_json;
43+
$this->cache = $cache;
4244

4345
try {
4446
$this->processStats();
@@ -99,38 +101,56 @@ private function processStats(): void {
99101
$icons_db = json_decode(file_get_contents($this->icons_json), true) ?: [];
100102
}
101103

102-
// cURL
103-
$ch = curl_init();
104-
assert($this->api_url !== '');
105-
curl_setopt($ch, CURLOPT_URL, $this->api_url);
106-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
107-
curl_setopt($ch, CURLOPT_HEADER, true);
108-
curl_setopt($ch, CURLOPT_HTTPHEADER, [
109-
'Accept: application/json',
110-
]);
104+
$api_data = null;
105+
$cache_lifetime = 300; // 5 minutes
111106

112-
$response = curl_exec($ch);
113-
114-
if ($response === false) {
115-
$error_message = 'cURL error: ' . curl_error($ch);
116-
$this->log_error($error_message);
117-
throw new Exception($error_message);
107+
// check cache
108+
if (file_exists($this->cache) && (time() - filemtime($this->cache)) < $cache_lifetime) {
109+
$api_data = file_get_contents($this->cache);
110+
if ($api_data === false) {
111+
$this->log_error("Failed to read cache: {$this->cache}. Fetching from API.");
112+
$api_data = null;
113+
}
118114
}
119115

120-
// Get HTTP status code
121-
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
122-
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
116+
if ($api_data === null) {
117+
// cURL
118+
$ch = curl_init();
119+
assert($this->api_url !== '');
120+
curl_setopt($ch, CURLOPT_URL, $this->api_url);
121+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
122+
curl_setopt($ch, CURLOPT_HEADER, true);
123+
curl_setopt($ch, CURLOPT_HTTPHEADER, [
124+
'Accept: application/json',
125+
]);
126+
127+
$response = curl_exec($ch);
128+
129+
if ($response === false) {
130+
$error_message = 'cURL error: ' . curl_error($ch);
131+
$this->log_error($error_message);
132+
throw new Exception($error_message);
133+
}
123134

124-
if ($http_code !== 200) {
125-
$error_message = "HTTP $http_code";
126-
$this->log_error($error_message);
127-
throw new Exception($error_message);
128-
}
135+
// Get HTTP status code
136+
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
137+
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
138+
139+
if ($http_code !== 200) {
140+
$error_message = "HTTP $http_code";
141+
$this->log_error($error_message);
142+
throw new Exception($error_message);
143+
}
129144

130-
/** @var string $response */
131-
$api_data = substr($response, $header_size);
145+
/** @var string $response */
146+
$api_data = substr($response, $header_size);
147+
curl_close($ch);
132148

133-
curl_close($ch);
149+
// save cache
150+
if (file_put_contents($this->cache, $api_data) === false) {
151+
$this->log_error("Failed to save cache: {$this->cache}");
152+
}
153+
}
134154

135155
$data = json_decode($api_data, true);
136156
if (json_last_error() !== JSON_ERROR_NONE) {

public_html/rpcn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
include 'lib/module/rpcn/inc-rpcn-stats.php';
44

55
// Initialize RPCNStats class
6-
$rpcn_stats = new RPCNStats($games_json, $log_file, $api_url, $icons_json);
6+
$rpcn_stats = new RPCNStats($games_json, $log_file, $api_url, $icons_json, $cache);
77

88
// Fetch data and check for errors
99
$has_error = $rpcn_stats->has_error;

0 commit comments

Comments
 (0)