discord-rich-presence-plex/utils/cache.py

31 lines
857 B
Python
Raw Permalink Normal View History

from .logging import logger
from config.constants import cacheFilePath
from typing import Any
import json
import os
import time
cache: dict[str, Any] = {}
def loadCache() -> None:
if not os.path.isfile(cacheFilePath):
return
try:
with open(cacheFilePath, "r", encoding = "UTF-8") as cacheFile:
cache.update(json.load(cacheFile))
except:
2023-11-05 05:24:45 +00:00
root, ext = os.path.splitext(cacheFilePath)
os.rename(cacheFilePath, f"{root}-{time.time():.0f}.{ext}")
logger.exception("Failed to parse the cache file. A new one will be created.")
def getCacheKey(key: str) -> Any:
return cache.get(key)
def setCacheKey(key: str, value: Any) -> None:
cache[key] = value
try:
with open(cacheFilePath, "w", encoding = "UTF-8") as cacheFile:
json.dump(cache, cacheFile, separators = (",", ":"))
except:
2023-11-05 05:24:45 +00:00
logger.exception("Failed to write to the cache file")