Skip to content

Repository files navigation

@agrodt/astro-redis-cache-provider

CI Coverage Status npm version License: MIT npm downloads

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!

Install

pnpm add @agrodt/astro-redis-cache-provider

node-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.

Usage

// 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",
    }),
  },
});

Upgrading from Astro 6

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.

Options

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 example redis://localhost:6379 or () => 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: Additional Vary headers to ignore during cache matching (for example ["cookie"]). set-cookie is always ignored.
  • query: Same query normalization semantics as Astro memory provider (include/exclude are mutually exclusive).

Example

See example for a runnable Astro app that demonstrates MISS/HIT behavior and manual invalidation.

About

Custom Redis cache provider for Astro route caching, powered by node-redis

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages