1
- """ Helper for /Registry section
2
- """
1
+ """Helper for /Registry section"""
3
2
4
3
import errno
4
+ import inspect
5
+ import sys
5
6
6
7
from threading import Lock
8
+ from collections .abc import Iterable
7
9
8
10
from cachetools import TTLCache , cached
11
+ from cachetools .keys import hashkey
9
12
10
13
14
+ from typing import Optional
15
+ from collections .abc import Iterable
16
+
11
17
from DIRAC import S_OK , S_ERROR
12
18
from DIRAC .ConfigurationSystem .Client .Config import gConfig
13
19
from DIRAC .ConfigurationSystem .Client .Helpers .CSGlobals import getVO
14
20
15
21
ID_DN_PREFIX = "/O=DIRAC/CN="
16
22
23
+ # 300 is the default CS refresh time
24
+
25
+ CACHE_REFRESH_TIME = 300
17
26
# pylint: disable=missing-docstring
18
27
19
28
gBaseRegistrySection = "/Registry"
20
29
21
30
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 )
22
51
def getUsernameForDN (dn , usersList = None ):
23
52
"""Find DIRAC user for DN
24
53
@@ -39,6 +68,7 @@ def getUsernameForDN(dn, usersList=None):
39
68
return S_ERROR (f"No username found for dn { dn } " )
40
69
41
70
71
+ @cached (TTLCache (maxsize = 1000 , ttl = CACHE_REFRESH_TIME ), lock = Lock ())
42
72
def getDNForUsername (username ):
43
73
"""Get user DN for user
44
74
@@ -419,6 +449,7 @@ def getBannedIPs():
419
449
return gConfig .getValue (f"{ gBaseRegistrySection } /BannedIPs" , [])
420
450
421
451
452
+ @cached (TTLCache (maxsize = 1000 , ttl = CACHE_REFRESH_TIME ), lock = Lock ())
422
453
def getVOForGroup (group ):
423
454
"""Search VO name for group
424
455
@@ -634,10 +665,7 @@ def getDNProperty(userDN, value, defaultValue=None):
634
665
return S_OK (defaultValue )
635
666
636
667
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 ())
641
669
def getProxyProvidersForDN (userDN ):
642
670
"""Get proxy providers by user DN
643
671
0 commit comments