Implemented cache enabling and disabling at runtime

This commit is contained in:
krateng 2022-04-24 15:53:30 +02:00
parent 279499ad9f
commit 7c77474feb

View File

@ -10,10 +10,8 @@ from doreah.logging import log
from ..pkg_global.conf import malojaconfig from ..pkg_global.conf import malojaconfig
HIGH_NUMBER = 1000000 CACHE_SIZE = 1000
CACHE_SIZE = 10000 ENTITY_CACHE_SIZE = 100000
ENTITY_CACHE_SIZE = 1000000
CACHE_ADJUST_STEP = 100
cache = lru.LRU(CACHE_SIZE) cache = lru.LRU(CACHE_SIZE)
entitycache = lru.LRU(ENTITY_CACHE_SIZE) entitycache = lru.LRU(ENTITY_CACHE_SIZE)
@ -38,8 +36,10 @@ def print_stats():
def cached_wrapper(inner_func): def cached_wrapper(inner_func):
if not malojaconfig['USE_GLOBAL_CACHE']: return inner_func
def outer_func(*args,**kwargs): def outer_func(*args,**kwargs):
if not malojaconfig['USE_GLOBAL_CACHE']: inner_func(*args,**kwargs)
if 'dbconn' in kwargs: if 'dbconn' in kwargs:
conn = kwargs.pop('dbconn') conn = kwargs.pop('dbconn')
else: else:
@ -65,8 +65,9 @@ def cached_wrapper(inner_func):
# cache that's aware of what we're calling # cache that's aware of what we're calling
def cached_wrapper_individual(inner_func): def cached_wrapper_individual(inner_func):
if not malojaconfig['USE_GLOBAL_CACHE']: return inner_func
def outer_func(set_arg,**kwargs): def outer_func(set_arg,**kwargs):
if not malojaconfig['USE_GLOBAL_CACHE']: return inner_func(set_arg,**kwargs)
if 'dbconn' in kwargs: if 'dbconn' in kwargs:
@ -95,7 +96,6 @@ def cached_wrapper_individual(inner_func):
return outer_func return outer_func
def invalidate_caches(scrobbletime=None): def invalidate_caches(scrobbletime=None):
if malojaconfig['USE_GLOBAL_CACHE']:
cleared, kept = 0, 0 cleared, kept = 0, 0
for k in cache.keys(): for k in cache.keys():
# VERY BIG TODO: differentiate between None as in 'unlimited timerange' and None as in 'time doesnt matter here'! # VERY BIG TODO: differentiate between None as in 'unlimited timerange' and None as in 'time doesnt matter here'!
@ -121,8 +121,8 @@ def trim_cache():
#cache.set_size(targetsize) #cache.set_size(targetsize)
#cache.set_size(HIGH_NUMBER) #cache.set_size(HIGH_NUMBER)
cache.clear() cache.clear()
if cache.get_size() > CACHE_ADJUST_STEP: #if cache.get_size() > CACHE_ADJUST_STEP:
cache.set_size(cache.get_size() - CACHE_ADJUST_STEP) # cache.set_size(cache.get_size() - CACHE_ADJUST_STEP)
#log(f"New RAM usage: {psutil.virtual_memory().percent}%") #log(f"New RAM usage: {psutil.virtual_memory().percent}%")
print_stats() print_stats()