Skip to content

Commit 974c9fe

Browse files
committed
Improved cache info logging and removed an unneeded cache
1 parent 69c250c commit 974c9fe

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

packet/context_processors.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212

1313
# pylint: disable=bare-except
14-
@lru_cache(maxsize=128)
1514
def get_csh_name(username):
1615
try:
1716
member = ldap_get_member(username)

packet/log_utils.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
from functools import wraps
66
from datetime import datetime
77

8-
from packet import context_processors, app
8+
from packet import app
9+
from packet.context_processors import get_rit_name
10+
from packet.ldap import ldap_get_member
11+
from packet.utils import is_freshman_on_floor
912

1013

1114
def log_time(func):
@@ -26,16 +29,28 @@ def wrapped_function(*args, **kwargs):
2629
return wrapped_function
2730

2831

32+
def _format_cache(func):
33+
"""
34+
:return: The output of func.cache_info() as a compactly formatted string
35+
"""
36+
info = func.cache_info()
37+
return "{}[hits={}, misses={}, size={}/{}]".format(func.__name__, info.hits, info.misses, info.currsize, info.maxsize)
38+
39+
40+
# Tuple of lru_cache functions to log stats from
41+
_caches = (get_rit_name, ldap_get_member, is_freshman_on_floor)
42+
43+
2944
def log_cache(func):
3045
"""
3146
Decorator for logging cache info
3247
"""
48+
3349
@wraps(func)
3450
def wrapped_function(*args, **kwargs):
3551
result = func(*args, **kwargs)
3652

37-
app.logger.info("get_csh_name(): {}, get_rit_name(): {}".format(context_processors.get_csh_name.cache_info(),
38-
context_processors.get_rit_name.cache_info()))
53+
app.logger.info("Cache stats: " + ", ".join(map(_format_cache, _caches)))
3954

4055
return result
4156

packet/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def wrapped_function(*args, **kwargs):
2222
info = {
2323
"realm": "intro",
2424
"uid": uid,
25-
"onfloor": is_on_floor(uid)
25+
"onfloor": is_freshman_on_floor(uid)
2626
}
2727
else:
2828
info = {
@@ -37,7 +37,7 @@ def wrapped_function(*args, **kwargs):
3737

3838

3939
@lru_cache(maxsize=128)
40-
def is_on_floor(rit_username):
40+
def is_freshman_on_floor(rit_username):
4141
"""
4242
Checks if a freshman is on floor
4343
"""

0 commit comments

Comments
 (0)