cache.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import json
  2. import os.path
  3. import threading
  4. import time
  5. from modules.paths import data_path, script_path
  6. cache_filename = os.path.join(data_path, "cache.json")
  7. cache_data = None
  8. cache_lock = threading.Lock()
  9. dump_cache_after = None
  10. dump_cache_thread = None
  11. def dump_cache():
  12. """
  13. Marks cache for writing to disk. 5 seconds after no one else flags the cache for writing, it is written.
  14. """
  15. global dump_cache_after
  16. global dump_cache_thread
  17. def thread_func():
  18. global dump_cache_after
  19. global dump_cache_thread
  20. while dump_cache_after is not None and time.time() < dump_cache_after:
  21. time.sleep(1)
  22. with cache_lock:
  23. with open(cache_filename, "w", encoding="utf8") as file:
  24. json.dump(cache_data, file, indent=4)
  25. dump_cache_after = None
  26. dump_cache_thread = None
  27. with cache_lock:
  28. dump_cache_after = time.time() + 5
  29. if dump_cache_thread is None:
  30. dump_cache_thread = threading.Thread(name='cache-writer', target=thread_func)
  31. dump_cache_thread.start()
  32. def cache(subsection):
  33. """
  34. Retrieves or initializes a cache for a specific subsection.
  35. Parameters:
  36. subsection (str): The subsection identifier for the cache.
  37. Returns:
  38. dict: The cache data for the specified subsection.
  39. """
  40. global cache_data
  41. if cache_data is None:
  42. with cache_lock:
  43. if cache_data is None:
  44. if not os.path.isfile(cache_filename):
  45. cache_data = {}
  46. else:
  47. try:
  48. with open(cache_filename, "r", encoding="utf8") as file:
  49. cache_data = json.load(file)
  50. except Exception:
  51. os.replace(cache_filename, os.path.join(script_path, "tmp", "cache.json"))
  52. print('[ERROR] issue occurred while trying to read cache.json, move current cache to tmp/cache.json and create new cache')
  53. cache_data = {}
  54. s = cache_data.get(subsection, {})
  55. cache_data[subsection] = s
  56. return s
  57. def cached_data_for_file(subsection, title, filename, func):
  58. """
  59. Retrieves or generates data for a specific file, using a caching mechanism.
  60. Parameters:
  61. subsection (str): The subsection of the cache to use.
  62. title (str): The title of the data entry in the subsection of the cache.
  63. filename (str): The path to the file to be checked for modifications.
  64. func (callable): A function that generates the data if it is not available in the cache.
  65. Returns:
  66. dict or None: The cached or generated data, or None if data generation fails.
  67. The `cached_data_for_file` function implements a caching mechanism for data stored in files.
  68. It checks if the data associated with the given `title` is present in the cache and compares the
  69. modification time of the file with the cached modification time. If the file has been modified,
  70. the cache is considered invalid and the data is regenerated using the provided `func`.
  71. Otherwise, the cached data is returned.
  72. If the data generation fails, None is returned to indicate the failure. Otherwise, the generated
  73. or cached data is returned as a dictionary.
  74. """
  75. existing_cache = cache(subsection)
  76. ondisk_mtime = os.path.getmtime(filename)
  77. entry = existing_cache.get(title)
  78. if entry:
  79. cached_mtime = entry.get("mtime", 0)
  80. if ondisk_mtime > cached_mtime:
  81. entry = None
  82. if not entry or 'value' not in entry:
  83. value = func()
  84. if value is None:
  85. return None
  86. entry = {'mtime': ondisk_mtime, 'value': value}
  87. existing_cache[title] = entry
  88. dump_cache()
  89. return entry['value']