10
10
import warnings
11
11
from contextlib import suppress
12
12
from pathlib import Path
13
- from typing import TYPE_CHECKING , Literal , overload
13
+ from typing import TYPE_CHECKING , Any , Literal , Protocol , cast , overload
14
14
15
15
from ._cache import CACHE_DISABLED , _SVGCache , cache_key , svg_cache
16
16
20
20
21
21
import requests
22
22
23
+ class _lru_cache_wrapper (Protocol ):
24
+ __wrapped__ : Callable [..., Any ]
25
+
26
+ def cache_clear (self ) -> None : ...
27
+
23
28
F = TypeVar ("F" , bound = Callable )
24
29
25
30
from .iconify_types import (
35
40
36
41
ROOT = "https://api.iconify.design"
37
42
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
+
38
81
39
82
@functools .cache
40
83
def _session () -> requests .Session :
@@ -46,7 +89,7 @@ def _session() -> requests.Session:
46
89
return session
47
90
48
91
49
- @functools .cache
92
+ @functools .lru_cache ( maxsize = 128 )
50
93
def collections (* prefixes : str ) -> dict [str , IconifyInfo ]:
51
94
"""Return collections where key is icon set prefix, value is IconifyInfo object.
52
95
@@ -67,7 +110,7 @@ def collections(*prefixes: str) -> dict[str, IconifyInfo]:
67
110
return resp .json () # type: ignore
68
111
69
112
70
- @functools .cache
113
+ @functools .lru_cache ( maxsize = 128 )
71
114
def collection (
72
115
prefix : str ,
73
116
info : bool = False ,
@@ -260,7 +303,7 @@ def _cached_svg_path(svg_cache_key: str) -> Path | None:
260
303
return None # pragma: no cover
261
304
262
305
263
- @functools .cache
306
+ @functools .lru_cache ( maxsize = 128 )
264
307
def svg_path (
265
308
* key : str ,
266
309
color : str | None = None ,
@@ -327,7 +370,7 @@ def _remove_tmp_svg() -> None:
327
370
return Path (tmp_name )
328
371
329
372
330
- @functools .cache
373
+ @functools .lru_cache ( maxsize = 128 )
331
374
def css (
332
375
* keys : str ,
333
376
selector : str | None = None ,
0 commit comments