1
1
---
2
-
3
2
summary : Control cache programmatically. Use this template to optimize performance and implement custom caching strategies.
4
- goal :
5
- - Other
6
- operation :
7
- - Cache
3
+ tags :
4
+ - Caching
8
5
products :
9
6
- Snippets
10
7
pcx_content_type : example
@@ -22,49 +19,54 @@ const USE_QUERY_STRING = true; // Include query string in the cache key
22
19
const INCLUDE_HEADERS = [" User-Agent" ]; // Headers to include in the cache key
23
20
24
21
export default {
25
- async fetch (request , env , ctx ) {
26
- // Generate a custom cache key based on user preferences
27
- const cacheKey = createCacheKey (request);
28
- console .log (` Retrieving cache for: ${ cacheKey .url } .` )
29
-
30
- // Access the default Cache API
31
- const cache = caches .default ;
32
-
33
- // Attempt to retrieve the cached response
34
- let response = await cache .match (cacheKey);
35
-
36
- if (! response) {
37
- // Cache miss: Fetch the asset from the origin
38
- console .log (` Cache miss for: ${ cacheKey .url } . Fetching from origin...` );
39
- response = await fetch (request);
40
-
41
- // Wrap the origin response for caching
42
- response = new Response (response .body , response);
43
-
44
- // Set Cache-Control headers to define the TTL
45
- response .headers .set (" Cache-Control" , ` s-maxage=${ CACHE_DURATION_SECONDS } ` );
46
- response .headers .set (" x-snippets-cache" , " stored" );
47
-
48
- // Store the response in the cache
49
- await cache .put (cacheKey, response .clone ());
50
- } else {
51
- // Cache hit: Return the cached response
52
- console .log (` Cache hit for: ${ cacheKey .url } .` );
53
- response = new Response (response .body , response);
54
- response .headers .set (" x-snippets-cache" , " hit" );
55
-
56
- // Optionally check if the cache should expire based on age
57
- const ageHeader = response .headers .get (" Age" );
58
- if (ageHeader && parseInt (ageHeader, 10 ) > CACHE_DURATION_SECONDS ) {
59
- console .log (` Cache expired for: ${ cacheKey .url } . Deleting cached response...` );
60
- await cache .delete (cacheKey);
61
- response .headers .set (" x-snippets-cache" , " deleted" );
62
- }
63
- }
64
-
65
- // Return the response to the client
66
- return response;
67
- },
22
+ async fetch (request , env , ctx ) {
23
+ // Generate a custom cache key based on user preferences
24
+ const cacheKey = createCacheKey (request);
25
+ console .log (` Retrieving cache for: ${ cacheKey .url } .` );
26
+
27
+ // Access the default Cache API
28
+ const cache = caches .default ;
29
+
30
+ // Attempt to retrieve the cached response
31
+ let response = await cache .match (cacheKey);
32
+
33
+ if (! response) {
34
+ // Cache miss: Fetch the asset from the origin
35
+ console .log (` Cache miss for: ${ cacheKey .url } . Fetching from origin...` );
36
+ response = await fetch (request);
37
+
38
+ // Wrap the origin response for caching
39
+ response = new Response (response .body , response);
40
+
41
+ // Set Cache-Control headers to define the TTL
42
+ response .headers .set (
43
+ " Cache-Control" ,
44
+ ` s-maxage=${ CACHE_DURATION_SECONDS } ` ,
45
+ );
46
+ response .headers .set (" x-snippets-cache" , " stored" );
47
+
48
+ // Store the response in the cache
49
+ await cache .put (cacheKey, response .clone ());
50
+ } else {
51
+ // Cache hit: Return the cached response
52
+ console .log (` Cache hit for: ${ cacheKey .url } .` );
53
+ response = new Response (response .body , response);
54
+ response .headers .set (" x-snippets-cache" , " hit" );
55
+
56
+ // Optionally check if the cache should expire based on age
57
+ const ageHeader = response .headers .get (" Age" );
58
+ if (ageHeader && parseInt (ageHeader, 10 ) > CACHE_DURATION_SECONDS ) {
59
+ console .log (
60
+ ` Cache expired for: ${ cacheKey .url } . Deleting cached response...` ,
61
+ );
62
+ await cache .delete (cacheKey);
63
+ response .headers .set (" x-snippets-cache" , " deleted" );
64
+ }
65
+ }
66
+
67
+ // Return the response to the client
68
+ return response;
69
+ },
68
70
};
69
71
70
72
/**
@@ -73,28 +75,30 @@ export default {
73
75
* @returns {Request} - A valid cache key based on the URL
74
76
*/
75
77
function createCacheKey (request ) {
76
- const url = new URL (request .url ); // Use the request's base URL
77
- const cacheKey = new URL (url .origin ); // Start with the origin (scheme + hostname)
78
-
79
- // Optionally include the path
80
- if (USE_PATH ) {
81
- cacheKey .pathname = url .pathname ;
82
- }
83
-
84
- // Optionally include the query string
85
- if (USE_QUERY_STRING ) {
86
- cacheKey .search = url .search ;
87
- }
88
-
89
- // Optionally include specific headers
90
- if (INCLUDE_HEADERS .length > 0 ) {
91
- const headerParts = INCLUDE_HEADERS .map (header => ` ${ header} =${ request .headers .get (header) || " " } ` ).join (" &" );
92
- cacheKey .searchParams .append (" headers" , headerParts);
93
- }
94
-
95
- // Return the constructed URL as the cache key
96
- return new Request (cacheKey .toString (), {
97
- method: " GET"
98
- });
78
+ const url = new URL (request .url ); // Use the request's base URL
79
+ const cacheKey = new URL (url .origin ); // Start with the origin (scheme + hostname)
80
+
81
+ // Optionally include the path
82
+ if (USE_PATH ) {
83
+ cacheKey .pathname = url .pathname ;
84
+ }
85
+
86
+ // Optionally include the query string
87
+ if (USE_QUERY_STRING ) {
88
+ cacheKey .search = url .search ;
89
+ }
90
+
91
+ // Optionally include specific headers
92
+ if (INCLUDE_HEADERS .length > 0 ) {
93
+ const headerParts = INCLUDE_HEADERS .map (
94
+ (header ) => ` ${ header} =${ request .headers .get (header) || " " } ` ,
95
+ ).join (" &" );
96
+ cacheKey .searchParams .append (" headers" , headerParts);
97
+ }
98
+
99
+ // Return the constructed URL as the cache key
100
+ return new Request (cacheKey .toString (), {
101
+ method: " GET" ,
102
+ });
99
103
}
100
104
```
0 commit comments