mirror of
https://github.com/krateng/maloja.git
synced 2025-07-09 03:04:07 -04:00
Removed previous ability, but this time clean and consistent
This commit is contained in:
parent
7c77474feb
commit
d5f5b48d85
@ -10,23 +10,27 @@ from doreah.logging import log
|
|||||||
|
|
||||||
from ..pkg_global.conf import malojaconfig
|
from ..pkg_global.conf import malojaconfig
|
||||||
|
|
||||||
CACHE_SIZE = 1000
|
|
||||||
ENTITY_CACHE_SIZE = 100000
|
|
||||||
|
|
||||||
cache = lru.LRU(CACHE_SIZE)
|
|
||||||
entitycache = lru.LRU(ENTITY_CACHE_SIZE)
|
|
||||||
|
|
||||||
hits, misses = 0, 0
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@runhourly
|
|
||||||
def maintenance():
|
if malojaconfig['USE_GLOBAL_CACHE']:
|
||||||
if malojaconfig['USE_GLOBAL_CACHE']:
|
CACHE_SIZE = 1000
|
||||||
|
ENTITY_CACHE_SIZE = 100000
|
||||||
|
|
||||||
|
cache = lru.LRU(CACHE_SIZE)
|
||||||
|
entitycache = lru.LRU(ENTITY_CACHE_SIZE)
|
||||||
|
|
||||||
|
hits, misses = 0, 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@runhourly
|
||||||
|
def maintenance():
|
||||||
print_stats()
|
print_stats()
|
||||||
trim_cache()
|
trim_cache()
|
||||||
|
|
||||||
def print_stats():
|
def print_stats():
|
||||||
log(f"Cache Size: {len(cache)} [{len(entitycache)} E], System RAM Utilization: {psutil.virtual_memory().percent}%, Cache Hits: {hits}/{hits+misses}")
|
log(f"Cache Size: {len(cache)} [{len(entitycache)} E], System RAM Utilization: {psutil.virtual_memory().percent}%, Cache Hits: {hits}/{hits+misses}")
|
||||||
#print("Full rundown:")
|
#print("Full rundown:")
|
||||||
#import sys
|
#import sys
|
||||||
@ -34,11 +38,9 @@ def print_stats():
|
|||||||
# print(f"\t{k}\t{sys.getsizeof(cache[k])}")
|
# print(f"\t{k}\t{sys.getsizeof(cache[k])}")
|
||||||
|
|
||||||
|
|
||||||
def cached_wrapper(inner_func):
|
def cached_wrapper(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')
|
||||||
@ -60,14 +62,13 @@ def cached_wrapper(inner_func):
|
|||||||
return outer_func
|
return outer_func
|
||||||
|
|
||||||
|
|
||||||
# cache for functions that call with a whole list of entity ids
|
# cache for functions that call with a whole list of entity ids
|
||||||
# we don't want a new cache entry for every single combination, but keep a common
|
# we don't want a new cache entry for every single combination, but keep a common
|
||||||
# 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):
|
||||||
|
|
||||||
|
|
||||||
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,7 @@ def cached_wrapper_individual(inner_func):
|
|||||||
|
|
||||||
return outer_func
|
return outer_func
|
||||||
|
|
||||||
def invalidate_caches(scrobbletime=None):
|
def invalidate_caches(scrobbletime=None):
|
||||||
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'!
|
||||||
@ -107,11 +108,11 @@ def invalidate_caches(scrobbletime=None):
|
|||||||
log(f"Invalidated {cleared} of {cleared+kept} DB cache entries")
|
log(f"Invalidated {cleared} of {cleared+kept} DB cache entries")
|
||||||
|
|
||||||
|
|
||||||
def invalidate_entity_cache():
|
def invalidate_entity_cache():
|
||||||
entitycache.clear()
|
entitycache.clear()
|
||||||
|
|
||||||
|
|
||||||
def trim_cache():
|
def trim_cache():
|
||||||
ramprct = psutil.virtual_memory().percent
|
ramprct = psutil.virtual_memory().percent
|
||||||
if ramprct > malojaconfig["DB_MAX_MEMORY"]:
|
if ramprct > malojaconfig["DB_MAX_MEMORY"]:
|
||||||
log(f"{ramprct}% RAM usage, clearing cache and adjusting size!")
|
log(f"{ramprct}% RAM usage, clearing cache and adjusting size!")
|
||||||
@ -129,6 +130,20 @@ def trim_cache():
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
else:
|
||||||
|
def cached_wrapper(func):
|
||||||
|
return func
|
||||||
|
def cached_wrapper_individual(func):
|
||||||
|
return func
|
||||||
|
def invalidate_caches(scrobbletime=None):
|
||||||
|
return None
|
||||||
|
def invalidate_entity_cache():
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def serialize(obj):
|
def serialize(obj):
|
||||||
try:
|
try:
|
||||||
return serialize(obj.hashable())
|
return serialize(obj.hashable())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user