mirror of
https://github.com/getnamingo/registry.git
synced 2025-07-13 14:25:05 +02:00
Improved system cache script
This commit is contained in:
parent
265a9eb1f7
commit
c7b4e3c631
2 changed files with 50 additions and 23 deletions
|
@ -51,7 +51,7 @@ $scheduler->php('/opt/registry/automation/write-zone.php')->at('*/15 * * * *');
|
|||
$scheduler->php('/opt/registry/automation/registrar.php')->at('35 * * * *');
|
||||
$scheduler->php('/opt/registry/automation/statistics.php')->at('59 * * * *');
|
||||
$scheduler->php('/opt/registry/automation/rdap-urls.php')->at('50 1 * * *');
|
||||
$scheduler->php('/var/www/cp/bin/file_cache.php')->at('0 0 * * 2,6');
|
||||
$scheduler->php('/var/www/cp/bin/file_cache.php')->at('0 0 * * 1,4');
|
||||
|
||||
$scheduler->php('/opt/registry/automation/domain-lifecycle-manager.php')->at('*/5 * * * *');
|
||||
$scheduler->php('/opt/registry/automation/auto-approve-transfer.php')->at('*/30 * * * *');
|
||||
|
|
|
@ -1,38 +1,65 @@
|
|||
<?php
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
require_once '/opt/registry/automation/helpers.php';
|
||||
|
||||
use League\Flysystem\Local\LocalFilesystemAdapter;
|
||||
use League\Flysystem\Filesystem;
|
||||
use MatthiasMullie\Scrapbook\Adapters\Flysystem as ScrapbookFlysystem;
|
||||
use MatthiasMullie\Scrapbook\Psr6\Pool;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
|
||||
$logFilePath = '/var/log/namingo/system-cache.log';
|
||||
$log = setupLogger($logFilePath, 'SYSTEM_CACHE');
|
||||
$log->info('job started.');
|
||||
|
||||
// Configuration
|
||||
$config = [
|
||||
'cache_path' => __DIR__ . '/../cache', // Cache directory
|
||||
'cache_key' => 'tlds_alpha_by_domain',
|
||||
'file_url' => 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt',
|
||||
'cache_duration' => 86400 * 7, // Cache for 7 days
|
||||
'max_retries' => 3, // Retry up to 3 times
|
||||
];
|
||||
|
||||
// Set up Filesystem Cache
|
||||
$cachePath = __DIR__ . '/../cache'; // Cache directory
|
||||
$adapter = new LocalFilesystemAdapter($cachePath, null, LOCK_EX);
|
||||
$adapter = new LocalFilesystemAdapter($config['cache_path'], null, LOCK_EX);
|
||||
$filesystem = new Filesystem($adapter);
|
||||
$cache = new Pool(new ScrapbookFlysystem($filesystem));
|
||||
|
||||
// Define the cache key and the URL of the file you want to cache
|
||||
$cacheKey = 'tlds_alpha_by_domain';
|
||||
$fileUrl = 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt';
|
||||
|
||||
// Check if the file is already cached
|
||||
$cachedFile = $cache->getItem($cacheKey);
|
||||
if (!$cachedFile->isHit()) {
|
||||
// File is not cached, download it
|
||||
$httpClient = new Client();
|
||||
$response = $httpClient->get($fileUrl);
|
||||
$fileContent = $response->getBody()->getContents();
|
||||
$cachedFile = $cache->getItem($config['cache_key']);
|
||||
if ($cachedFile->isHit()) {
|
||||
$log->info('ICANN TLD List loaded from cache.');
|
||||
exit(0);
|
||||
}
|
||||
|
||||
// Save the file content to cache
|
||||
$cachedFile->set($fileContent);
|
||||
$cachedFile->expiresAfter(86400 * 7); // Cache for 7 days
|
||||
$cache->save($cachedFile);
|
||||
echo "ICANN TLD List downloaded and cached.".PHP_EOL;
|
||||
} else {
|
||||
// Retrieve the file content from the cache
|
||||
$fileContent = $cachedFile->get();
|
||||
echo "ICANN TLD List loaded from cache.".PHP_EOL;
|
||||
}
|
||||
// Download and cache the file
|
||||
$httpClient = new Client();
|
||||
$retryCount = 0;
|
||||
$success = false;
|
||||
|
||||
while ($retryCount < $config['max_retries'] && !$success) {
|
||||
try {
|
||||
$response = $httpClient->get($config['file_url']);
|
||||
$fileContent = $response->getBody()->getContents();
|
||||
|
||||
// Save the file content to cache
|
||||
$cachedFile->set($fileContent);
|
||||
$cachedFile->expiresAfter($config['cache_duration']);
|
||||
$cache->save($cachedFile);
|
||||
|
||||
$log->info('ICANN TLD list downloaded and cached successfully.');
|
||||
$success = true;
|
||||
} catch (RequestException $e) {
|
||||
$retryCount++;
|
||||
$log->error("Error downloading file (attempt $retryCount): " . $e->getMessage());
|
||||
if ($retryCount >= $config['max_retries']) {
|
||||
$log->error('Max retries reached. File download failed.');
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$log->info('job finished successfully.');
|
Loading…
Add table
Add a link
Reference in a new issue