@@ -8,30 +8,59 @@ class ResponseCache:
88 """Simple in-memory cache for API responses."""
99
1010 def __init__ (self ):
11+ """Initialise the ResponseCache with an empty cache dictionary."""
12+
1113 self ._cache = {}
1214
1315 @staticmethod
1416 def _generate_key (url : str , params : dict = None ) -> str :
15- """Generate a unique cache key from URL and params."""
17+ """
18+ Generate a unique cache key from URL and parameters.
19+
20+ :param url: The URL to generate a key for.
21+ :param params: Optional dictionary of parameters to include in the key.
22+ :return: MD5 hash string representing the unique cache key.
23+ """
24+
1625 cache_data = f"{ url } :{ json .dumps (params or {}, sort_keys = True )} "
1726 return hashlib .md5 (cache_data .encode ()).hexdigest ()
1827
1928 def get (self , url : str , params : dict = None ):
20- """Get cached response."""
29+ """
30+ Retrieve a cached response if it exists.
31+
32+ :param url: The URL to retrieve cached data for.
33+ :param params: Optional dictionary of parameters used in the original request.
34+ :return: Cached response data if found, None otherwise.
35+ """
36+
2137 key = self ._generate_key (url , params )
2238 return self ._cache .get (key )
2339
2440 def set (self , url : str , data , params : dict = None ):
25- """Cache a response."""
41+ """
42+ Store a response in the cache.
43+
44+ :param url: The URL to cache data for.
45+ :param data: The response data to cache.
46+ :param params: Optional dictionary of parameters used in the request.
47+ """
48+
2649 key = self ._generate_key (url , params )
2750 self ._cache [key ] = data
2851
2952 def clear (self ):
30- """Clear all cached responses."""
53+ """Clear all cached responses from memory ."""
3154 self ._cache .clear ()
3255
3356 def remove (self , url : str , params : dict = None ):
34- """Remove a specific cached response."""
57+ """
58+ Remove a specific cached response.
59+
60+ :param url: The URL to remove cached data for.
61+ :param params: Optional dictionary of parameters used in the original request.
62+ """
63+
3564 key = self ._generate_key (url , params )
3665 self ._cache .pop (key , None )
3766
@@ -40,5 +69,6 @@ def remove(self, url: str, params: dict = None):
4069
4170
4271def clear_cache ():
43- """Clear all cached API responses."""
72+ """Clear all cached API responses from the global cache instance."""
73+
4474 cache .clear ()
0 commit comments