A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.cloudflare.com/rules/snippets/examples/custom-cache/ below:

Custom cache · Cloudflare Rules docs

// Define configurable cache duration in seconds (default: 30 days)

const CACHE_DURATION_SECONDS = 30 * 24 * 60 * 60;

// Define which parts of the request to include in the cache key

const USE_PATH = true; // Include path in the cache key

const USE_QUERY_STRING = true; // Include query string in the cache key

const INCLUDE_HEADERS = ["User-Agent"]; // Headers to include in the cache key

async fetch(request, env, ctx) {

// Generate a custom cache key based on user preferences

const cacheKey = createCacheKey(request);

console.log(`Retrieving cache for: ${cacheKey.url}.`)

// Access the default Cache API

const cache = caches.default;

// Attempt to retrieve the cached response

let response = await cache.match(cacheKey);

// Cache miss: Fetch the asset from the origin

console.log(`Cache miss for: ${cacheKey.url}. Fetching from origin...`);

response = await fetch(request);

// Wrap the origin response for caching

response = new Response(response.body, response);

// Set Cache-Control headers to define the TTL

response.headers.set("Cache-Control", `s-maxage=${CACHE_DURATION_SECONDS}`);

response.headers.set("x-snippets-cache", "stored");

// Store the response in the cache

await cache.put(cacheKey, response.clone());

// Cache hit: Return the cached response

console.log(`Cache hit for: ${cacheKey.url}.`);

response = new Response(response.body, response);

response.headers.set("x-snippets-cache", "hit");

// Optionally check if the cache should expire based on age

const ageHeader = response.headers.get("Age");

if (ageHeader && parseInt(ageHeader, 10) > CACHE_DURATION_SECONDS) {

console.log(`Cache expired for: ${cacheKey.url}. Deleting cached response...`);

await cache.delete(cacheKey);

response.headers.set("x-snippets-cache", "deleted");

// Return the response to the client

* Function to create a custom cache key based on request properties

* @param {Request} request - The incoming request object

* @returns {Request} - A valid cache key based on the URL

function createCacheKey(request) {

const url = new URL(request.url); // Use the request's base URL

const cacheKey = new URL(url.origin); // Start with the origin (scheme + hostname)

// Optionally include the path

cacheKey.pathname = url.pathname;

// Optionally include the query string

cacheKey.search = url.search;

// Optionally include specific headers

if (INCLUDE_HEADERS.length > 0) {

const headerParts = INCLUDE_HEADERS.map(header => `${header}=${request.headers.get(header) || ""}`).join("&");

cacheKey.searchParams.append("headers", headerParts);

// Return the constructed URL as the cache key

return new Request(cacheKey.toString(), {


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4