first fixes of dxclustercache since new cache driver handling

This commit is contained in:
HB9HIL
2026-02-02 22:07:00 +01:00
parent bfca2c48a1
commit 26d2a46c08
3 changed files with 28 additions and 126 deletions

View File

@@ -77,35 +77,20 @@ class DxclusterCache {
if (empty($logbook_key)) return;
// Delete callsign cache
$this->deleteFile($this->getWorkedCallKey($logbook_key, $callsign));
$this->_delete_from_cache($this->getWorkedCallKey($logbook_key, $callsign));
// Look up DXCC and continent from callsign
$dxccobj = new Dxcc(null);
$dxcc_info = $dxccobj->dxcc_lookup($callsign, date('Y-m-d'));
if (!empty($dxcc_info['adif'])) {
$this->deleteFile($this->getWorkedDxccKey($logbook_key, $dxcc_info['adif']));
$this->_delete_from_cache($this->getWorkedDxccKey($logbook_key, $dxcc_info['adif']));
}
if (!empty($dxcc_info['cont'])) {
$this->deleteFile($this->getWorkedContKey($logbook_key, $dxcc_info['cont']));
$this->_delete_from_cache($this->getWorkedContKey($logbook_key, $dxcc_info['cont']));
}
}
/**
* Invalidate all worked cache for current user (bulk operations)
*/
public function invalidateAllWorkedForCurrentUser() {
// Skip if worked cache is disabled
if ($this->CI->config->item('enable_dxcluster_file_cache_worked') !== true) return;
$logbook_key = $this->getCurrentUserLogbookKey();
if (empty($logbook_key)) return;
$this->invalidateByPrefix("dxcluster_worked_call_{$logbook_key}_");
$this->invalidateByPrefix("dxcluster_worked_dxcc_{$logbook_key}_");
$this->invalidateByPrefix("dxcluster_worked_cont_{$logbook_key}_");
}
/**
* Get current user's logbook key from session
*/
@@ -129,97 +114,12 @@ class DxclusterCache {
// INTERNAL HELPERS
// =========================================================================
protected function deleteFile($cache_key) {
$cache_path = $this->getCachePath();
if (!$cache_path) return;
@unlink($cache_path . $cache_key);
}
protected function invalidateByPrefix($prefix) {
$cache_path = $this->getCachePath();
if (!$cache_path) return;
$handle = @opendir($cache_path);
if (!$handle) return;
while (($filename = readdir($handle)) !== false) {
if (strpos($filename, $prefix) === 0) {
@unlink($cache_path . $filename);
}
}
closedir($handle);
}
protected function getCachePath() {
$cache_path = $this->CI->config->item('cache_path');
$cache_path = ($cache_path === '' || $cache_path === false) ? APPPATH . 'cache/' : $cache_path;
$cache_path = rtrim($cache_path, '/\\') . DIRECTORY_SEPARATOR;
return (is_dir($cache_path) && is_writable($cache_path)) ? $cache_path : false;
}
// =========================================================================
// GARBAGE COLLECTION
// =========================================================================
/**
* Run garbage collection with probability check (1% chance)
* Call this on each request when worked cache is enabled
*/
public function maybeRunGc() {
if (mt_rand(1, 100) === 1) {
$this->cleanExpiredCache();
}
}
/**
* Clean expired dxcluster cache files
* Uses file mtime for fast pre-filtering before reading file contents
*/
public function cleanExpiredCache() {
$cache_path = $this->getCachePath();
if (!$cache_path || !is_readable($cache_path)) return;
$handle = @opendir($cache_path);
if (!$handle) return;
$now = time();
$deleted = 0;
// Max TTL for dxcluster files: raw=59s, worked=900s - use 900s + buffer
$max_ttl = 1000;
while (($filename = readdir($handle)) !== false) {
// Only process dxcluster cache files
if (strpos($filename, 'dxcluster_') !== 0) continue;
$file = $cache_path . $filename;
if (!is_file($file)) continue;
// Fast pre-filter: skip files modified recently (can't be expired yet)
$mtime = @filemtime($file);
if ($mtime !== false && ($now - $mtime) < $max_ttl) {
continue;
}
// File is old enough to potentially be expired - read and verify
$data = @unserialize(@file_get_contents($file));
if (!is_array($data) || !isset($data['time'], $data['ttl'])) {
@unlink($file);
$deleted++;
continue;
}
// Check if expired
if ($data['ttl'] > 0 && $now > $data['time'] + $data['ttl']) {
@unlink($file);
$deleted++;
}
}
closedir($handle);
if ($deleted > 0) {
log_message('debug', "DXCluster cache GC: deleted {$deleted} expired files");
}
protected function _delete_from_cache($cache_key) {
$this->CI->load->driver('cache', [
'adapter' => $this->CI->config->item('cache_adapter') ?? 'file',
'backup' => $this->CI->config->item('cache_backup') ?? 'file',
'key_prefix' => $this->CI->config->item('cache_key_prefix') ?? ''
]);
$this->CI->cache->delete($cache_key);
}
}