/
O.S.Prog
/
InstrumentProtocol
Обзор
Документация
Войти
/
O.S.Prog
/
InstrumentProtocol
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
core/cache_manager.py
78 строк
3 KB
O.S.Prog
Initial commit: InstrumentProtocol v2.0
17 июл 2026, 15:11
17 июл 2026, 15:11
1e845bd
Код
Авторство
О чём код?
import os import json from utils.file_utils import get_config_folder, ensure_folder class CacheManager: def __init__(self): self.cache_file = os.path.join(get_config_folder(), "protocols_cache.json") self.cache = {} self.load_cache() def load_cache(self): """Загружает кэш из файла""" if os.path.exists(self.cache_file): try: with open(self.cache_file, 'r', encoding='utf-8') as f: self.cache = json.load(f) except: self.cache = {} else: self.cache = {} def save_cache(self): """Сохраняет кэш в файл""" ensure_folder(os.path.dirname(self.cache_file)) with open(self.cache_file, 'w', encoding='utf-8') as f: json.dump(self.cache, f, ensure_ascii=False, indent=2) def get_all(self): """Возвращает все протоколы из кэша""" return list(self.cache.values()) def get(self, filepath): """Возвращает информацию о протоколе по пути""" return self.cache.get(filepath) def add(self, filepath, info): """Добавляет или обновляет протокол в кэше""" self.cache[filepath] = info self.save_cache() def add_many(self, protocols): """Добавляет множество протоколов в кэш""" for filepath, info in protocols.items(): self.cache[filepath] = info self.save_cache() def remove(self, filepath): """Удаляет протокол из кэша""" if filepath in self.cache: del self.cache[filepath] self.save_cache() return True return False def clear(self): """Очищает кэш""" self.cache = {} self.save_cache() def sync(self, protocol_manager, parent_widget=None): """ Синхронизирует кэш с реальными файлами в папке. Новые файлы добавляются, старые метаданные сохраняются. """ protocols = protocol_manager.get_all_protocols() for p in protocols: filepath = p['filepath'] if filepath not in self.cache: # Только новые файлы добавляем self.cache[filepath] = p # Удаляем из кэша файлы, которых больше нет на диске existing_files = {p['filepath'] for p in protocols} for f in list(self.cache.keys()): if f not in existing_files: del self.cache[f] self.save_cache() return len(self.cache)