Skip to main content

Overview

The cache middleware sets Cache-Control headers to control HTTP caching behavior. It helps optimize performance by instructing browsers and CDNs how to cache responses.

Installation

Quick Start

Configuration

Examples

Basic Caching

Public Cache (CDN)

Private Cache (Browser Only)

Immutable Assets

Stale-While-Revalidate

Full Configuration

Different Cache Strategies

API Reference

Common Patterns

Technical Details

Implementation

The cache middleware operates by constructing and setting the Cache-Control HTTP header based on the provided configuration options. The middleware:
  1. Header Construction: Uses the buildCacheControl function to build the Cache-Control header string from the Options struct
  2. Non-Overriding Behavior: Only sets the Cache-Control header if it hasn’t been set by the handler or previous middleware
  3. Default Fallback: Returns no-cache when no options are specified
  4. Time Conversion: Converts Go time.Duration values to seconds for cache directives (max-age, s-maxage, etc.)

Cache-Control Directives

The middleware supports the following HTTP Cache-Control directives:
  • public: Response may be cached by any cache (browsers, CDNs, proxies)
  • private: Response is intended for a single user and should only be cached by the browser
  • no-cache: Cache must revalidate with the origin server before using cached content
  • no-store: Response must not be stored in any cache
  • no-transform: Intermediaries must not transform the response (e.g., image compression)
  • must-revalidate: Cache must verify the status of stale resources and expired ones should not be used
  • proxy-revalidate: Same as must-revalidate but only for shared caches
  • max-age=N: Maximum time (in seconds) a resource is considered fresh
  • s-maxage=N: Overrides max-age for shared caches (CDNs, proxies)
  • immutable: Resource will never change during its freshness lifetime
  • stale-while-revalidate=N: Cache may serve stale content while revalidating in the background
  • stale-if-error=N: Cache may serve stale content if an error occurs during revalidation

Helper Functions

The package provides several convenience functions that configure common caching strategies:
  • New(maxAge): Basic caching with max-age
  • Public(maxAge): Public caching with max-age
  • Private(maxAge): Private caching with max-age
  • Immutable(maxAge): Immutable public content with max-age
  • Static(maxAge): Alias for Immutable, suitable for static assets
  • SWR(maxAge, stale): Stale-while-revalidate pattern for improved perceived performance

Best Practices

  1. Use appropriate cache strategies for different content types
  2. Leverage stale-while-revalidate for improved user experience
  3. Set immutable for versioned static assets
  4. Use private for user-specific data
  5. Combine with CDN using s-maxage for better edge caching

Testing

The cache middleware includes comprehensive test coverage for all functionality:

Security Considerations

  • Sensitive data: Always use private or no-store for sensitive information
  • User-specific content: Never use public for personalized data
  • Authentication: Consider using no-cache or no-store for authenticated routes