1- """ Helper for /Registry section
2- """
1+ """Helper for /Registry section"""
32
43import errno
4+ import inspect
5+ import sys
56
67from threading import Lock
8+ from collections .abc import Iterable
79
810from cachetools import TTLCache , cached
11+ from cachetools .keys import hashkey
912
1013
14+ from typing import Optional
15+ from collections .abc import Iterable
16+
1117from DIRAC import S_OK , S_ERROR
1218from DIRAC .ConfigurationSystem .Client .Config import gConfig
1319from DIRAC .ConfigurationSystem .Client .Helpers .CSGlobals import getVO
1420
1521ID_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
1928gBaseRegistrySection = "/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 )
2251def 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 ())
4272def 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 ())
422453def 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 ())
641669def getProxyProvidersForDN (userDN ):
642670 """Get proxy providers by user DN
643671
0 commit comments