@@ -10,6 +10,26 @@ const configOptions = {
10
10
expirationTime : 1000 * 60 * 5 ,
11
11
} ;
12
12
13
+ /**
14
+ * Check if a given URL has a cached response in memory.
15
+ *
16
+ * @param {string } url - The URL to check for a cache response.
17
+ * @returns {boolean } True if a memory cache exists for the URL, false otherwise.
18
+ */
19
+ export const hasCache = ( url ) => ! ! memoryCache . get ( url ) ;
20
+
21
+ /**
22
+ * Clear cache for a given url.
23
+ *
24
+ * @param {string } url - The URL for which to remove the cache.
25
+ */
26
+ export const clearCache = ( url ) => memoryCache . remove ( url ) ;
27
+
28
+ /**
29
+ * Clears all cached responses in the memory.
30
+ */
31
+ export const clearAllCache = ( ) => memoryCache . clear ( ) ;
32
+
13
33
/**
14
34
* Middleware that serves cached responses.
15
35
*
@@ -20,9 +40,8 @@ const configOptions = {
20
40
* Caching won't be applied if a path matches any one in excluded paths.
21
41
* @param {number } [timeout=300000] - Cache expiry in milliseconds. Default 5 minutes. Pass `0` for no expiry!
22
42
* @param {boolean } [cacheControl=true] - Should add a `cache-control` header. Default true. This header is not overridden if one already exists.
23
- * @returns {{ hasCache: function, clearCache: function, clearAllCache: function } }
24
43
*/
25
- export default function ( {
44
+ export function createApiCache ( {
26
45
excludes = [ ] ,
27
46
timeout = 300000 ,
28
47
cacheControl = true ,
@@ -32,27 +51,6 @@ export default function ({
32
51
configOptions . cacheControl = cacheControl ;
33
52
34
53
return {
35
- /**
36
- * Check if a given URL has a cached response in memory.
37
- *
38
- * @param {string } url - The URL to check for a cache response.
39
- * @returns {boolean } True if a memory cache exists for the URL, false otherwise.
40
- */
41
- hasCache : ( url ) => ! ! memoryCache . get ( url ) ,
42
-
43
- /**
44
- * Clear cache for a given url.
45
- *
46
- * @param {string } url - The URL for which to remove the cache.
47
- */
48
- clearCache : ( url ) => memoryCache . remove ( url ) ,
49
-
50
- /**
51
- * Clears all cached responses in the memory.
52
- */
53
- clearAllCache : ( ) => memoryCache . clear ( ) ,
54
-
55
- // internal middleware methods.
56
54
incoming : ( request , response ) => {
57
55
serveCacheIfAvailable ( request , response ) ;
58
56
} ,
0 commit comments