Forked from will-lms/openai-compat-endpoint
src / file-cache.ts
// src/file-cache.ts
// Модуль для кэширования списка моделей в файл
import * as fs from 'fs';
import * as path from 'path';
const CACHE_FILE = 'models-cache.json';
export interface ModelCache {
timestamp: number;
allModels: string[];
freeModels: string[];
}
/**
* Получает путь к файлу кэша в директории плагина
*/
function getCachePath(): string {
// __dirname указывает на .lmstudio папку, кэш на уровень выше
return path.join(__dirname, '..', CACHE_FILE);
}
/**
* Загружает кэш из файла
* Кэш хранится бессрочно — TTL удалён для ручной загрузки пользователем
*/
export function loadCache(): ModelCache | null {
try {
const cachePath = getCachePath();
if (!fs.existsSync(cachePath)) {
return null;
}
const data = fs.readFileSync(cachePath, 'utf-8');
const cache: ModelCache = JSON.parse(data);
// TTL проверка удалена — кэш хранится бессрочно
return cache;
} catch (error) {
console.error('[Cache] Failed to load cache:', error);
return null;
}
}
/**
* Сохраняет кэш в файл
*/
export function saveCache(allModels: string[], freeModels: string[]): void {
try {
const cache: ModelCache = {
timestamp: Date.now(),
allModels,
freeModels
};
const cachePath = getCachePath();
fs.writeFileSync(cachePath, JSON.stringify(cache, null, 2), 'utf-8');
} catch (error) {
console.error('[Cache] Failed to save cache:', error);
}
}