Custom Redis cache provider for Astro route caching,
powered by node-redis.
Caution
Disclaimer: This project is 102% generated by an LLM. The author assumes absolutely no responsibility for any consequences of using it - especially, but not limited to, the mysterious harm that may befall nearby kittens once you deploy this in production. Proceed at your own risk!
pnpm add @agrodt/astro-redis-cache-providernode-redis already comes as a dependency of this package. You only need
a reachable Redis server and Astro 7.3 or later within Astro 7.
// astro.config.mjs
import { defineConfig } from "astro/config";
import { redisCache } from "@agrodt/astro-redis-cache-provider/config";
export default defineConfig({
cache: {
provider: redisCache({
url: () => process.env.REDIS_URL,
keyPrefix: "astro:cache",
}),
},
});Move experimental.cache to the top-level cache option. This provider uses
Astro's public astro/cache/provider-utils helpers for response headers and
path/tag invalidation. URL normalization, response serialization, Vary, and
background revalidation remain provider-owned because Astro does not export
runtime helpers for them.
Redis keys now use the v2 namespace: paths are indexed using Astro's
astro-path: tags. Existing v1 entries are not reused, so the cache starts
cold after upgrading. Old entries and indexes expire according to their TTL;
old v1:vary:* metadata has no TTL and may be removed separately.
The astro-path: tag prefix is reserved for path invalidation.
type RedisCacheProviderOptions = {
url?: string | (() => string | undefined);
keyPrefix?: string;
revalidateLockTtl?: number;
ignoredVaryHeaders?: Iterable<string>;
query?: {
include?: string[];
exclude?: string[];
sort?: boolean;
};
};url: Redis URL string or runtime factory (() => string | undefined), for exampleredis://localhost:6379or() => process.env.REDIS_URL.keyPrefix: Prefix for all Redis keys. Default:astro:cache.revalidateLockTtl: Lock lifetime in seconds for stale background revalidation. Default:30.ignoredVaryHeaders: AdditionalVaryheaders to ignore during cache matching (for example["cookie"]).set-cookieis always ignored.query: Same query normalization semantics as Astro memory provider (include/excludeare mutually exclusive).
See example for a runnable Astro app that
demonstrates MISS/HIT behavior and manual invalidation.