1010import  warnings 
1111from  contextlib  import  suppress 
1212from  pathlib  import  Path 
13- from  typing  import  TYPE_CHECKING , Literal , overload 
13+ from  typing  import  TYPE_CHECKING , Any ,  Literal ,  Protocol ,  cast , overload 
1414
1515from  ._cache  import  CACHE_DISABLED , _SVGCache , cache_key , svg_cache 
1616
2020
2121    import  requests 
2222
23+     class  _lru_cache_wrapper (Protocol ):
24+         __wrapped__ : Callable [..., Any ]
25+ 
26+         def  cache_clear (self ) ->  None : ...
27+ 
2328    F  =  TypeVar ("F" , bound = Callable )
2429
2530    from  .iconify_types  import  (
3540
3641ROOT  =  "https://api.iconify.design" 
3742
43+ API_FUNCTIONS : set [str ] =  {"collections" , "collection" , "last_modified" , "css" }
44+ 
45+ 
46+ def  clear_api_cache () ->  None :
47+     """Clear all cached responses to the iconify API from this session.""" 
48+     for  func_name  in  API_FUNCTIONS :
49+         wrapper  =  cast ("_lru_cache_wrapper" , globals ()[func_name ])
50+         wrapper .cache_clear ()
51+ 
52+ 
53+ def  set_api_cache_maxsize (maxsize : int  |  None ) ->  None :
54+     """Set the `lru_cache` maxsize for all calls to the iconify API. 
55+ 
56+     This is NOT the same thing as the on-disk SVG cache 
57+ 
58+     This will also clear all cached responses to the iconify API from this session. 
59+     """ 
60+     import  pyconify 
61+ 
62+     if  maxsize  is  not None :
63+         if  not  isinstance (maxsize , int ):  # pragma: no cover 
64+             raise  TypeError (
65+                 f"maxsize must be an integer, not { type (maxsize ).__name__ }  
66+             )
67+         if  maxsize  <  1 :  # pragma: no cover 
68+             maxsize  =  0 
69+ 
70+     for  func_name  in  API_FUNCTIONS :
71+         # get the lrue_cache-wrapped function and clear it 
72+         wrapper  =  cast ("_lru_cache_wrapper" , globals ()[func_name ])
73+         wrapper .cache_clear ()
74+         # get the original function and wrap it with the new maxsize 
75+         func  =  wrapper .__wrapped__ 
76+         new_func  =  functools .lru_cache (maxsize = maxsize )(func )
77+         # update the names in both this module and top-level pyconify 
78+         globals ()[func_name ] =  new_func 
79+         setattr (pyconify , func_name , new_func )
80+ 
3881
3982@functools .cache  
4083def  _session () ->  requests .Session :
@@ -46,7 +89,7 @@ def _session() -> requests.Session:
4689    return  session 
4790
4891
49- @functools .cache  
92+ @functools .lru_cache ( maxsize = 128 )  
5093def  collections (* prefixes : str ) ->  dict [str , IconifyInfo ]:
5194    """Return collections where key is icon set prefix, value is IconifyInfo object. 
5295
@@ -67,7 +110,7 @@ def collections(*prefixes: str) -> dict[str, IconifyInfo]:
67110    return  resp .json ()  # type: ignore 
68111
69112
70- @functools .cache  
113+ @functools .lru_cache ( maxsize = 128 )  
71114def  collection (
72115    prefix : str ,
73116    info : bool  =  False ,
@@ -260,7 +303,7 @@ def _cached_svg_path(svg_cache_key: str) -> Path | None:
260303    return  None   # pragma: no cover 
261304
262305
263- @functools .cache  
306+ @functools .lru_cache ( maxsize = 128 )  
264307def  svg_path (
265308    * key : str ,
266309    color : str  |  None  =  None ,
@@ -327,7 +370,7 @@ def _remove_tmp_svg() -> None:
327370    return  Path (tmp_name )
328371
329372
330- @functools .cache  
373+ @functools .lru_cache ( maxsize = 128 )  
331374def  css (
332375    * keys : str ,
333376    selector : str  |  None  =  None ,
0 commit comments