mirror of
https://github.com/phin05/discord-rich-presence-plex
synced 2024-11-22 01:23:02 +00:00
28 lines
852 B
Python
28 lines
852 B
Python
from store.constants import cacheFilePath
|
|
from typing import Any
|
|
from utils.logging import logger
|
|
import json
|
|
import os
|
|
import time
|
|
|
|
cache: dict[str, Any] = {}
|
|
|
|
def loadCache() -> None:
|
|
if os.path.isfile(cacheFilePath):
|
|
try:
|
|
with open(cacheFilePath, "r", encoding = "UTF-8") as cacheFile:
|
|
cache.update(json.load(cacheFile))
|
|
except:
|
|
os.rename(cacheFilePath, cacheFilePath.replace(".json", f"-{time.time():.0f}.json"))
|
|
logger.exception("Failed to parse the application's cache file. A new one will be created.")
|
|
|
|
def getKey(key: str) -> Any:
|
|
return cache.get(key)
|
|
|
|
def setKey(key: str, value: Any) -> None:
|
|
cache[key] = value
|
|
try:
|
|
with open(cacheFilePath, "w", encoding = "UTF-8") as cacheFile:
|
|
json.dump(cache, cacheFile, separators = (",", ":"))
|
|
except:
|
|
logger.exception("Failed to write to the application's cache file.")
|