Implement cache

This commit is contained in:
Rikko 2022-08-06 01:01:55 +05:30
parent 2eb44620b7
commit 637f1cf1b1

View file

@ -20,6 +20,8 @@ HEADERS = {
PLUGIN_DIRECTORY = _env["python_directory_user"] PLUGIN_DIRECTORY = _env["python_directory_user"]
PLUGIN_ENTRYPOINT = "Main" PLUGIN_ENTRYPOINT = "Main"
_CACHE = {}
def setup_config(): def setup_config():
is_config_updated = False is_config_updated = False
@ -48,7 +50,7 @@ class Category:
self.name = name self.name = name
self.base_download_url = base_download_url self.base_download_url = base_download_url
self.meta_url = meta_url self.meta_url = meta_url
self._plugins = None self._plugins = _CACHE.get("categories", {}).get(self.name)
async def get_plugins(self): async def get_plugins(self):
if self._plugins is None: if self._plugins is None:
@ -60,10 +62,23 @@ class Category:
plugins_info = json.loads(response.read()) plugins_info = json.loads(response.read())
self._plugins = ([Plugin(plugin_info, self.base_download_url) self._plugins = ([Plugin(plugin_info, self.base_download_url)
for plugin_info in plugins_info.items()]) for plugin_info in plugins_info.items()])
self.set_category_plugins_global_cache(self._plugins)
return self._plugins return self._plugins
def set_category_plugins_global_cache(self, plugins):
if "categories" not in _CACHE:
_CACHE["categories"] = {}
_CACHE["categories"][self.name] = plugins
def unset_category_plugins_global_cache(self):
try:
del _CACHE["categories"][self.name]
except KeyError:
pass
async def refresh(self): async def refresh(self):
self._plugins = {} self._plugins = {}
self.unset_category_plugins_global_cache()
return await self.get_plugins() return await self.get_plugins()
@ -349,33 +364,35 @@ class PluginWindow(popup.PopupWindow):
class PluginManager: class PluginManager:
def __init__(self): def __init__(self):
self.request_headers = HEADERS self.request_headers = HEADERS
self._index = {} self._index = _CACHE.get("index", {})
# @property
# def index(self):
# if not self._index:
# loop = asyncio.get_event_loop()
# self._index = loop.create_task(self.plugin_index())
# return self._index
# def plugin_index(self):
# loop = asyncio.get_event_loop()
# loop.create_task(self.plugin_index())
async def get_index(self): async def get_index(self):
global _INDEX
if not self._index: if not self._index:
request = urllib.request.Request( request = urllib.request.Request(
INDEX_META, INDEX_META,
headers=HEADERS headers=HEADERS
) )
print(INDEX_META)
response = await send_network_request(request) response = await send_network_request(request)
self._index = json.loads(response.read()) self._index = json.loads(response.read())
self.set_index_global_cache(self._index)
return self._index return self._index
async def refresh(self): async def refresh(self):
self._index = {} self._index = {}
self.unset_index_global_cache()
return await self.get_index() return await self.get_index()
def set_index_global_cache(self, index):
_CACHE["index"] = index
def unset_index_global_cache(self):
try:
del _CACHE["index"]
except KeyError:
pass
async def soft_refresh(self): async def soft_refresh(self):
pass pass
@ -681,10 +698,10 @@ class PluginManagerWindow(ba.Window, PluginManager):
color = (0, 1, 0) color = (0, 1, 0)
else: else:
color = (1, 0.6, 0) color = (1, 0.6, 0)
else:
color = (0.5, 0.5, 0.5)
else: else:
color = (1, 1, 1) color = (1, 1, 1)
else:
color = (0.5, 0.5, 0.5)
plugin_to_update = self.plugins_in_current_view.get(plugin.name) plugin_to_update = self.plugins_in_current_view.get(plugin.name)
if plugin_to_update: if plugin_to_update: