fix(cache): auto-create cache directories when using FileStorage

The UnifiedCacheSystem was constructing directory paths for Nette FileStorage
but not creating them, causing "Directory not found" errors when accessing
caches like 'bb_login_err'. FileStorage expects directories to already exist.

Changes:
- Add automatic directory creation using bb_mkdir() before FileStorage init
- Handle both regular cache directories and SQLite parent directories
- Apply to both _buildStorage() and _buildDatastoreStorage() methods
- Add proper error handling with RuntimeException for failed creation
- Maintain consistency with TorrentPier's directory creation patterns

This ensures cache directories are created automatically when first accessed,
eliminating the need for manual directory creation during deployment.

Fixes: Cache initialization failures with missing directories
This commit is contained in:
Yury Pikhtarev 2025-06-20 03:47:43 +04:00
commit ce7e57977f
No known key found for this signature in database

View file

@ -165,10 +165,23 @@ class UnifiedCacheSystem
case 'redis':
// Some deprecated cache types will fall back to file storage
$dir = rtrim($this->cfg['db_dir'] ?? sys_get_temp_dir() . '/cache/', '/') . '/' . $cache_name . '/';
// Create directory automatically using TorrentPier's bb_mkdir function
if (!is_dir($dir) && !bb_mkdir($dir)) {
throw new \RuntimeException("Failed to create cache directory: $dir");
}
return new FileStorage($dir);
case 'sqlite':
$dbFile = rtrim($this->cfg['db_dir'] ?? sys_get_temp_dir() . '/cache/', '/') . '/' . $cache_name . '.db';
// Create parent directory for SQLite file
$dbDir = dirname($dbFile);
if (!is_dir($dbDir) && !bb_mkdir($dbDir)) {
throw new \RuntimeException("Failed to create cache directory for SQLite: $dbDir");
}
return new SQLiteStorage($dbFile);
case 'memory':
@ -183,6 +196,12 @@ class UnifiedCacheSystem
default:
// Fallback to file storage
$dir = rtrim($this->cfg['db_dir'] ?? sys_get_temp_dir() . '/cache/', '/') . '/' . $cache_name . '/';
// Create directory automatically using TorrentPier's bb_mkdir function
if (!is_dir($dir) && !bb_mkdir($dir)) {
throw new \RuntimeException("Failed to create cache directory: $dir");
}
return new FileStorage($dir);
}
}
@ -218,10 +237,23 @@ class UnifiedCacheSystem
case 'redis':
// Some deprecated cache types will fall back to file storage
$dir = rtrim($this->cfg['db_dir'] ?? sys_get_temp_dir() . '/cache/', '/') . '/datastore/';
// Create directory automatically using TorrentPier's bb_mkdir function
if (!is_dir($dir) && !bb_mkdir($dir)) {
throw new \RuntimeException("Failed to create datastore directory: $dir");
}
return new FileStorage($dir);
case 'sqlite':
$dbFile = rtrim($this->cfg['db_dir'] ?? sys_get_temp_dir() . '/cache/', '/') . '/datastore.db';
// Create parent directory for SQLite file
$dbDir = dirname($dbFile);
if (!is_dir($dbDir) && !bb_mkdir($dbDir)) {
throw new \RuntimeException("Failed to create datastore directory for SQLite: $dbDir");
}
return new SQLiteStorage($dbFile);
case 'memory':
@ -236,6 +268,12 @@ class UnifiedCacheSystem
default:
// Fallback to file storage
$dir = rtrim($this->cfg['db_dir'] ?? sys_get_temp_dir() . '/cache/', '/') . '/datastore/';
// Create directory automatically using TorrentPier's bb_mkdir function
if (!is_dir($dir) && !bb_mkdir($dir)) {
throw new \RuntimeException("Failed to create datastore directory: $dir");
}
return new FileStorage($dir);
}
}