Skip to content

Commit ee284ab

Browse files
chaenfstagni
authored andcommitted
feat (Registry): cache getUsernameForDN and getVOForGroup
1 parent 5bd00eb commit ee284ab

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/DIRAC/ConfigurationSystem/Client/Helpers/Registry.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,53 @@
1-
""" Helper for /Registry section
2-
"""
1+
"""Helper for /Registry section"""
32

43
import errno
4+
import inspect
5+
import sys
56

67
from threading import Lock
8+
from collections.abc import Iterable
79

810
from cachetools import TTLCache, cached
11+
from cachetools.keys import hashkey
912

1013

14+
from typing import Optional
15+
from collections.abc import Iterable
16+
1117
from DIRAC import S_OK, S_ERROR
1218
from DIRAC.ConfigurationSystem.Client.Config import gConfig
1319
from DIRAC.ConfigurationSystem.Client.Helpers.CSGlobals import getVO
1420

1521
ID_DN_PREFIX = "/O=DIRAC/CN="
1622

23+
# 300 is the default CS refresh time
24+
25+
CACHE_REFRESH_TIME = 300
1726
# pylint: disable=missing-docstring
1827

1928
gBaseRegistrySection = "/Registry"
2029

2130

31+
def reset_all_caches():
32+
"""This method is called to clear all caches.
33+
It is necessary to reinitialize them after the central CS
34+
has been loaded
35+
"""
36+
for cache in [
37+
obj
38+
for name, obj in inspect.getmembers(sys.modules[__name__])
39+
if (inspect.isfunction(obj) and hasattr(obj, "cache_clear"))
40+
]:
41+
cache.cache_clear()
42+
43+
44+
def get_username_for_dn_key(dn: str, userList: Optional[Iterable[str]] = None):
45+
if userList:
46+
return hashkey(dn, *sorted(userList))
47+
return hashkey(dn)
48+
49+
50+
@cached(TTLCache(maxsize=1000, ttl=CACHE_REFRESH_TIME), lock=Lock(), key=get_username_for_dn_key)
2251
def getUsernameForDN(dn, usersList=None):
2352
"""Find DIRAC user for DN
2453
@@ -39,6 +68,7 @@ def getUsernameForDN(dn, usersList=None):
3968
return S_ERROR(f"No username found for dn {dn}")
4069

4170

71+
@cached(TTLCache(maxsize=1000, ttl=CACHE_REFRESH_TIME), lock=Lock())
4272
def getDNForUsername(username):
4373
"""Get user DN for user
4474
@@ -419,6 +449,7 @@ def getBannedIPs():
419449
return gConfig.getValue(f"{gBaseRegistrySection}/BannedIPs", [])
420450

421451

452+
@cached(TTLCache(maxsize=1000, ttl=CACHE_REFRESH_TIME), lock=Lock())
422453
def getVOForGroup(group):
423454
"""Search VO name for group
424455
@@ -634,10 +665,7 @@ def getDNProperty(userDN, value, defaultValue=None):
634665
return S_OK(defaultValue)
635666

636667

637-
_cache_getProxyProvidersForDN = TTLCache(maxsize=1000, ttl=60)
638-
639-
640-
@cached(_cache_getProxyProvidersForDN, lock=Lock())
668+
@cached(TTLCache(maxsize=1000, ttl=CACHE_REFRESH_TIME), lock=Lock())
641669
def getProxyProvidersForDN(userDN):
642670
"""Get proxy providers by user DN
643671

src/DIRAC/ConfigurationSystem/Client/LocalConfiguration.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,9 @@ def enableCS(self):
568568
objLoader = ObjectLoader()
569569
objLoader.reloadRootModules()
570570
self.__initLogger(self.componentName, self.loggingSection, forceInit=True)
571+
from DIRAC.ConfigurationSystem.Client.Helpers.Registry import reset_all_caches
572+
573+
reset_all_caches()
571574
return res
572575

573576
def isCSEnabled(self):

0 commit comments

Comments
 (0)