From a1f8e96ae48e428fac1e12a82674f5fcb5a237f7 Mon Sep 17 00:00:00 2001 From: krateng Date: Tue, 15 Feb 2022 05:52:44 +0100 Subject: [PATCH] Reimplemented cache limitation --- maloja/__pkginfo__.py | 2 +- maloja/database/dbcache.py | 25 +++++++++++++++++++++---- maloja/globalconf.py | 3 ++- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/maloja/__pkginfo__.py b/maloja/__pkginfo__.py index 36b33e3..bf5cf8a 100644 --- a/maloja/__pkginfo__.py +++ b/maloja/__pkginfo__.py @@ -4,7 +4,7 @@ # you know what f*ck it # this is hardcoded for now because of that damn project / package name discrepancy # i'll fix it one day -VERSION = "3.0.0-alpha.1" +VERSION = "3.0.0-alpha.2" HOMEPAGE = "https://github.com/krateng/maloja" diff --git a/maloja/database/dbcache.py b/maloja/database/dbcache.py index d1508f2..ac01dab 100644 --- a/maloja/database/dbcache.py +++ b/maloja/database/dbcache.py @@ -3,26 +3,37 @@ # that changes very infrequently or not at all import lru +import psutil import json from doreah.regular import runhourly from doreah.logging import log +from ..globalconf import malojaconfig USE_CACHE = True -cache = lru.LRU(300000) +HIGH_NUMBER = 1000000 + +cache = lru.LRU(HIGH_NUMBER) +hits, misses = 0, 0 @runhourly def print_stats(): - log(f"Cache Size: {len(cache)}") + log(f"Cache Size: {len(cache)}, RAM Utilization: {psutil.virtual_memory().percent}%, Cache Hits: {hits}/{hits+misses}") + trim_cache() + def cached_wrapper(inner_func): + def outer_func(**kwargs): + global hits, misses key = (serialize(kwargs), inner_func, kwargs.get("since"), kwargs.get("to")) if USE_CACHE and key in cache: + hits += 1 return cache.get(key) else: + misses += 1 result = inner_func(**kwargs) cache[key] = result return result @@ -38,8 +49,14 @@ def invalidate_caches(scrobbletime): - - +def trim_cache(): + ramprct = psutil.virtual_memory().percent + if ramprct > malojaconfig["DB_MAX_MEMORY"]: + log(f"{ramprct}% RAM usage, reducing caches!",module="debug") + ratio = (malojaconfig["DB_MAX_MEMORY"] / ramprct) ** 3 + targetsize = max(int(len(cache) * ratio),100) + c.set_size(targetsize) + c.set_size(HIGH_NUMBER) def serialize(obj): diff --git a/maloja/globalconf.py b/maloja/globalconf.py index 87f610b..c70d541 100644 --- a/maloja/globalconf.py +++ b/maloja/globalconf.py @@ -148,7 +148,8 @@ malojaconfig = Configuration( }, "Technical":{ "cache_expire_positive":(tp.Integer(), "Image Cache Expiration", 300, "Days until images are refetched"), - "cache_expire_negative":(tp.Integer(), "Image Cache Negative Expiration", 30, "Days until failed image fetches are reattempted") + "cache_expire_negative":(tp.Integer(), "Image Cache Negative Expiration", 30, "Days until failed image fetches are reattempted"), + "db_max_memory":(tp.Integer(min=0,max=100), "Database Cache RAM Percentage soft limit", 80, "How much of your total memory Maloja should use for caching. If you don't run the application in a container or dedicated VM, you might want to set this lower.") }, "Fluff":{ "scrobbles_gold":(tp.Integer(), "Scrobbles for Gold", 250, "How many scrobbles a track needs to be considered 'Gold' status"),