mirror of
https://github.com/krateng/maloja.git
synced 2025-07-09 03:04:07 -04:00
Implemented request-local DB cache
This commit is contained in:
parent
a1ef5a7791
commit
af57103300
@ -10,18 +10,24 @@ from doreah.logging import log
|
|||||||
|
|
||||||
from ..globalconf import malojaconfig
|
from ..globalconf import malojaconfig
|
||||||
|
|
||||||
USE_CACHE = True
|
|
||||||
HIGH_NUMBER = 1000000
|
HIGH_NUMBER = 1000000
|
||||||
|
|
||||||
cache = lru.LRU(HIGH_NUMBER)
|
cache = lru.LRU(HIGH_NUMBER)
|
||||||
hits, misses = 0, 0
|
hits, misses = 0, 0
|
||||||
|
|
||||||
|
|
||||||
|
if malojaconfig['USE_GLOBAL_CACHE']:
|
||||||
|
log("Using global DB Cache")
|
||||||
|
if malojaconfig['USE_REQUEST_CACHE']:
|
||||||
|
log("Using request-local DB Cache")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@runhourly
|
@runhourly
|
||||||
def maintenance():
|
def maintenance():
|
||||||
print_stats()
|
if malojaconfig['USE_GLOBAL_CACHE']:
|
||||||
trim_cache()
|
print_stats()
|
||||||
|
trim_cache()
|
||||||
|
|
||||||
def print_stats():
|
def print_stats():
|
||||||
log(f"Cache Size: {len(cache)}, System RAM Utilization: {psutil.virtual_memory().percent}%, Cache Hits: {hits}/{hits+misses}")
|
log(f"Cache Size: {len(cache)}, System RAM Utilization: {psutil.virtual_memory().percent}%, Cache Hits: {hits}/{hits+misses}")
|
||||||
@ -29,7 +35,7 @@ def print_stats():
|
|||||||
|
|
||||||
def cached_wrapper(inner_func):
|
def cached_wrapper(inner_func):
|
||||||
|
|
||||||
if not USE_CACHE: return inner_func
|
if not malojaconfig['USE_GLOBAL_CACHE']: return inner_func
|
||||||
def outer_func(*args,**kwargs):
|
def outer_func(*args,**kwargs):
|
||||||
if 'dbconn' in kwargs:
|
if 'dbconn' in kwargs:
|
||||||
conn = kwargs.pop('dbconn')
|
conn = kwargs.pop('dbconn')
|
||||||
@ -53,15 +59,16 @@ def cached_wrapper(inner_func):
|
|||||||
|
|
||||||
|
|
||||||
def invalidate_caches(scrobbletime):
|
def invalidate_caches(scrobbletime):
|
||||||
cleared, kept = 0, 0
|
if malojaconfig['USE_GLOBAL_CACHE']:
|
||||||
for k in cache.keys():
|
cleared, kept = 0, 0
|
||||||
# VERY BIG TODO: differentiate between None as in 'unlimited timerange' and None as in 'time doesnt matter here'!
|
for k in cache.keys():
|
||||||
if (k[3] is None or scrobbletime >= k[3]) and (k[4] is None or scrobbletime <= k[4]):
|
# VERY BIG TODO: differentiate between None as in 'unlimited timerange' and None as in 'time doesnt matter here'!
|
||||||
cleared += 1
|
if (k[3] is None or scrobbletime >= k[3]) and (k[4] is None or scrobbletime <= k[4]):
|
||||||
del cache[k]
|
cleared += 1
|
||||||
else:
|
del cache[k]
|
||||||
kept += 1
|
else:
|
||||||
log(f"Invalidated {cleared} of {cleared+kept} DB cache entries")
|
kept += 1
|
||||||
|
log(f"Invalidated {cleared} of {cleared+kept} DB cache entries")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,12 +1,19 @@
|
|||||||
|
|
||||||
from .. import database
|
from .. import database
|
||||||
from . sqldb import engine
|
from . sqldb import engine
|
||||||
|
|
||||||
|
from .dbcache import serialize
|
||||||
|
|
||||||
|
from ..globalconf import malojaconfig
|
||||||
|
|
||||||
|
|
||||||
# this is a wrapper object that provides a DB connection so that one jinja page
|
# this is a wrapper object that provides a DB connection so that one jinja page
|
||||||
# (with all its included partials) can use it for all functions
|
# (with all its included partials) can use it for all functions
|
||||||
# it also translates the non-unpacked calls to unpacked calls that the DB wants
|
# it also translates the non-unpacked calls to unpacked calls that the DB wants
|
||||||
|
# it also maintains a request-local cache since many webpages use the same stats
|
||||||
|
# several times
|
||||||
class JinjaDBConnection:
|
class JinjaDBConnection:
|
||||||
|
def __init__(self):
|
||||||
|
self.cache = {}
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self.conn = engine.connect()
|
self.conn = engine.connect()
|
||||||
return self
|
return self
|
||||||
@ -19,6 +26,17 @@ class JinjaDBConnection:
|
|||||||
kwargs = {}
|
kwargs = {}
|
||||||
for k in keys:
|
for k in keys:
|
||||||
kwargs.update(k)
|
kwargs.update(k)
|
||||||
return originalmethod(**kwargs,dbconn=self.conn)
|
if malojaconfig['USE_REQUEST_CACHE']:
|
||||||
|
cachekey = serialize((id(originalmethod),kwargs))
|
||||||
|
if cachekey in self.cache:
|
||||||
|
return self.cache[cachekey]
|
||||||
|
else:
|
||||||
|
result = originalmethod(**kwargs,dbconn=self.conn)
|
||||||
|
self.cache[cachekey] = result
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
result = originalmethod(**kwargs,dbconn=self.conn)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
return packedmethod
|
return packedmethod
|
||||||
|
@ -149,7 +149,9 @@ malojaconfig = Configuration(
|
|||||||
"Technical":{
|
"Technical":{
|
||||||
"cache_expire_positive":(tp.Integer(), "Image Cache Expiration", 60, "Days until images are refetched"),
|
"cache_expire_positive":(tp.Integer(), "Image Cache Expiration", 60, "Days until images are refetched"),
|
||||||
"cache_expire_negative":(tp.Integer(), "Image Cache Negative Expiration", 5, "Days until failed image fetches are reattempted"),
|
"cache_expire_negative":(tp.Integer(), "Image Cache Negative Expiration", 5, "Days until failed image fetches are reattempted"),
|
||||||
"db_max_memory":(tp.Integer(min=0,max=100), "RAM Percentage soft limit", 80, "RAM Usage in percent at which Maloja should no longer increase its database cache.")
|
"db_max_memory":(tp.Integer(min=0,max=100), "RAM Percentage soft limit", 80, "RAM Usage in percent at which Maloja should no longer increase its database cache."),
|
||||||
|
"use_request_cache":(tp.Boolean(), "Use request-local DB Cache", True),
|
||||||
|
"use_global_cache":(tp.Boolean(), "Use global DB Cache", True)
|
||||||
},
|
},
|
||||||
"Fluff":{
|
"Fluff":{
|
||||||
"scrobbles_gold":(tp.Integer(), "Scrobbles for Gold", 250, "How many scrobbles a track needs to be considered 'Gold' status"),
|
"scrobbles_gold":(tp.Integer(), "Scrobbles for Gold", 250, "How many scrobbles a track needs to be considered 'Gold' status"),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user