mirror of
https://github.com/rottenwheel/moner.ooo.git
synced 2025-04-29 11:59:28 -04:00
Merge pull request #15 from ilazaridis/feature/file-cache
Add file cache implementation
This commit is contained in:
commit
67b40152bc
2 changed files with 66 additions and 3 deletions
60
cache.php
Normal file
60
cache.php
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
class FileCache {
|
||||||
|
private $cacheDir;
|
||||||
|
private $defaultExpiration;
|
||||||
|
|
||||||
|
public function __construct($cacheDir = 'cache', $defaultExpiration = 60) {
|
||||||
|
$this->cacheDir = $cacheDir;
|
||||||
|
$this->defaultExpiration = $defaultExpiration;
|
||||||
|
// Create the cache directory if it doesn't exist
|
||||||
|
if (!file_exists($this->cacheDir)) {
|
||||||
|
mkdir($this->cacheDir, 0755, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($key, $expiration = null)
|
||||||
|
{
|
||||||
|
$file = $this->cacheDir . '/' . md5($key) . '.cache';
|
||||||
|
if (file_exists($file)) {
|
||||||
|
$expiration = $expiration ?? $this->defaultExpiration;
|
||||||
|
if (time() - filemtime($file) < $expiration) {
|
||||||
|
$data = file_get_contents($file);
|
||||||
|
return unserialize($data);
|
||||||
|
} else {
|
||||||
|
unlink($file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set($key, $data): void
|
||||||
|
{
|
||||||
|
$file = $this->cacheDir . '/' . md5($key) . '.cache';
|
||||||
|
file_put_contents($file, serialize($data));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete($key): void
|
||||||
|
{
|
||||||
|
$file = $this->cacheDir . '/' . md5($key) . '.cache';
|
||||||
|
if (file_exists($file)) {
|
||||||
|
unlink($file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clear()
|
||||||
|
{
|
||||||
|
array_map('unlink', glob($this->cacheDir . '/*.cache'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getOrSet($key, callable $callback, ?int $expiration = null)
|
||||||
|
{
|
||||||
|
$data = $this->get($key, $expiration);
|
||||||
|
if ($data === null) {
|
||||||
|
$data = $callback(); // Execute the callback to get the data
|
||||||
|
$this->set($key, $data);
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,14 +6,16 @@ date_default_timezone_set('Europe/Berlin');
|
||||||
// Define currencies that should *not* be included in the list
|
// Define currencies that should *not* be included in the list
|
||||||
$excludedCurrencies = ['bits', 'sats'];
|
$excludedCurrencies = ['bits', 'sats'];
|
||||||
|
|
||||||
|
require_once __DIR__ . '/cache.php';
|
||||||
|
|
||||||
// Fetch JSON data from a file and decode it
|
// Fetch JSON data from a file and decode it
|
||||||
function fetchJson($filename) {
|
function fetchJson($filename) {
|
||||||
return json_decode(file_get_contents($filename), true);
|
return json_decode(file_get_contents($filename), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
function fetchCache(string $key, string $url)
|
function fetchCache(string $key, string $url, FileCache $cache)
|
||||||
{
|
{
|
||||||
return apcu_entry($key, function() use ($url) {
|
return $cache->getOrSet($key, function() use ($url) {
|
||||||
return makeApiRequest($url);
|
return makeApiRequest($url);
|
||||||
}, 60);
|
}, 60);
|
||||||
}
|
}
|
||||||
|
@ -78,8 +80,9 @@ function fetchAvailableCurrencies() {
|
||||||
|
|
||||||
// Fetch currency data from CoinGecko API
|
// Fetch currency data from CoinGecko API
|
||||||
function fetchCurrencyData($currencies) {
|
function fetchCurrencyData($currencies) {
|
||||||
|
$cache = new FileCache(__DIR__ . '/cache');
|
||||||
$apiUrl = getCoinGeckoApiUrl('simple/price', ['ids' => 'monero', 'vs_currencies' => implode(',', array_map('strtolower', $currencies))]);
|
$apiUrl = getCoinGeckoApiUrl('simple/price', ['ids' => 'monero', 'vs_currencies' => implode(',', array_map('strtolower', $currencies))]);
|
||||||
return fetchCache('currency_data', $apiUrl);
|
return fetchCache('currency_data', $apiUrl, $cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
$currencyFile = 'coingecko.json';
|
$currencyFile = 'coingecko.json';
|
||||||
|
|
Loading…
Add table
Reference in a new issue