strix

A simple web UI for motion
git clone https://www.brianlane.com/git/strix
Log | Files | Refs | LICENSE

commit 50cc0ace1ab7cdaa4d7547fb307c87d531337273
parent 686de2b096bcad00d61a29355ecdebd7f6d2beaf
Author: Brian C. Lane <bcl@brianlane.com>
Date:   Sat,  5 Mar 2022 08:34:37 -0800

Cache events in memory

NOTE: This does not expire so it will continue to consume RAM until
restarted or it crashes.

Diffstat:
Msrc/strix/events.py | 30++++++++++++++++++++++++++++--
1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/src/strix/events.py b/src/strix/events.py @@ -18,11 +18,28 @@ from datetime import datetime from glob import glob import json import os +import threading import structlog from typing import Dict, List +class EventCacheClass: + def __init__(self): + self._lock = threading.Lock() + self._cache = {} + + def get(self, key): + with self._lock: + return self._cache[key] + + def set(self, key, value): + with self._lock: + self._cache[key] = value + +# Singleton +EventCache = EventCacheClass() + def path_to_dt(path: str) -> datetime: # Use the last 2 elements of the path to construct a Datatime @@ -44,11 +61,18 @@ def image_to_dt(event_date: str, image: str) -> datetime: def event_details(log: structlog.BoundLogger, event_path: str) -> Dict: # log.info("event_details", path=event_path) - # Have the details already been created? If so read it and return. + # Check the cache for the details + try: + return EventCache.get(event_path) + except KeyError: + pass + + # Try the file cache next try: if os.path.exists(event_path+"/.details.json"): with open(event_path+"/.details.json") as f: - return json.load(f) + details = json.load(f) + EventCache.set(event_path, details) except json.decoder.JSONDecodeError: log.warn("Error reading .details.json from %s", event_path) @@ -103,8 +127,10 @@ def event_details(log: structlog.BoundLogger, event_path: str) -> Dict: "images": [], "saved": is_saved } + EventCache.set(event_path, details) with open(event_path+"/.details.json", "w") as f: json.dump(details, f) + return details