diff --git a/CHANGELOG.md b/CHANGELOG.md index bc4d1541..c9802496 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Adding all english (**en**) i18n keys to help people with the application translation - AppImage - AppImage updater daemon replaced by a default Python thread to reduce memory usage +- Caching Snap and Flatpak suggestions (https://github.com/vinifmor/bauh/issues/23) +- i18n: + - Italian contributions by [albanobattistella](https://github.com/albanobattistella) ### Fixes - Flatpak diff --git a/bauh/gems/flatpak/controller.py b/bauh/gems/flatpak/controller.py index 2f6a688b..2227ae7a 100644 --- a/bauh/gems/flatpak/controller.py +++ b/bauh/gems/flatpak/controller.py @@ -29,6 +29,7 @@ class FlatpakManager(SoftwareManager): context.disk_loader_factory.map(FlatpakApplication, self.api_cache) self.enabled = True self.http_client = context.http_client + self.suggestions_cache = context.cache_factory.new() def get_managed_types(self) -> Set["type"]: return {FlatpakApplication} @@ -264,12 +265,18 @@ class FlatpakManager(SoftwareManager): sugs.sort(key=lambda t: t[1].value, reverse=True) for sug in sugs: - if limit <= 0 or len(res) < limit: - app_json = flatpak.search(cli_version, sug[0], app_id=True) + cached_sug = self.suggestions_cache.get(sug[0]) - if app_json: - res.append(PackageSuggestion(self._map_to_model(app_json[0], False, None), sug[1])) + if cached_sug: + res.append(cached_sug) + else: + app_json = flatpak.search(cli_version, sug[0], app_id=True) + + if app_json: + model = PackageSuggestion(self._map_to_model(app_json[0], False, None), sug[1]) + self.suggestions_cache.add(sug[0], model) + res.append(model) else: break diff --git a/bauh/gems/snap/controller.py b/bauh/gems/snap/controller.py index 24a5f42e..cc4a5ae3 100644 --- a/bauh/gems/snap/controller.py +++ b/bauh/gems/snap/controller.py @@ -35,6 +35,7 @@ class SnapManager(SoftwareManager): self.categories = {} self.categories_downloader = CategoriesDownloader('snap', self.http_client, self.logger, self, context.disk_cache, URL_CATEGORIES_FILE, SNAP_CACHE_PATH, CATEGORIES_FILE_PATH) + self.suggestions_cache = context.cache_factory.new() def map_json(self, app_json: dict, installed: bool, disk_loader: DiskCacheLoader, internet: bool = True) -> SnapApplication: app = SnapApplication(publisher=app_json.get('publisher'), @@ -222,7 +223,9 @@ class SnapManager(SoftwareManager): pkg['rev'] = pkg['revision'] pkg['name'] = pkg_name - out.append(PackageSuggestion(self.map_json(pkg, installed=False, disk_loader=None), priority)) + sug = PackageSuggestion(self.map_json(pkg, installed=False, disk_loader=None), priority) + self.suggestions_cache.add(pkg_name, sug) + out.append(sug) else: self.logger.warning("Could not retrieve suggestion '{}'".format(pkg_name)) @@ -239,10 +242,15 @@ class SnapManager(SoftwareManager): for sug in sugs: if limit <= 0 or len(res) < limit: - t = Thread(target=self._fill_suggestion, args=(sug[0], sug[1], res)) - t.start() - threads.append(t) - time.sleep(0.001) # to avoid being blocked + cached_sug = self.suggestions_cache.get(sug[0]) + + if cached_sug: + res.append(cached_sug) + else: + t = Thread(target=self._fill_suggestion, args=(sug[0], sug[1], res)) + t.start() + threads.append(t) + time.sleep(0.001) # to avoid being blocked else: break