caddyserver-cache-handler

repository·master·Indexed 18 days ago

https://github.com/caddyserver/cache-handler

A distributed HTTP cache module for Caddy (http.handlers.cache) based on the Souin cache engine. It provides RFC 7234 compliant caching, supports the Cache-Status response header, and includes a REST API for purging and listing resources. The module supports multiple storage providers including Redis, Etcd, Olric, Badger, NutsDB, and SimpleFS, and allows for customizable cache key generation and CDN integration.

Tokens
12.5K
Snippets
16
Records
22
Agent score
64%

What's inside cache-handler

  1. What is the cache-handler Caddy module?

    master

    The cache-handler module (http.handlers.cache) is a distributed HTTP cache for Caddy based on the Souin cache engine. It provides RFC 7234 compliant HTTP caching, supports the Cache-Status HTTP response header, includes a REST API for purging and listing resources, processes ESI tags, and supports distributed cache configurations.

    Important Note on Storage Providers: Since v1.7.0 of the underlying Souin engine, only one storage provider is implemented by default. To use a specific storage (like otter), you must include it during the Caddy build process using xcaddy and then configure it in your Caddyfile or JSON configuration.

    # Example: Building Caddy with the otter storage provider
    xcaddy build --with github.com/caddyserver/cache-handler --with github.com/darkweak/storages/otter/caddy
  2. Customize cache key generation

    master

    You can control how cache keys are generated using several directives:

    • key.template: Use Caddy templates to create custom keys. When enabled, disable_* directives are ignored.
    • key.disable_body: Disable the request body in the key (useful for GraphQL).
    • key.disable_host, key.disable_method, key.disable_query, key.disable_scheme: Disable these specific parts of the request from the key.
    • key.headers: Add specific headers (e.g., Authorization, Content-Type) to the key.
    • key.hash: Hash the resulting key to save storage space.
    • key.hide: Prevent the key from appearing in the Cache-Status response header.

    You can also define specific rules for certain URI patterns using cache_keys.{your regexp}.

  3. Configure the Etcd cache provider

    master

    The Etcd provider requires a configuration block to specify the connection details for your Etcd cluster.

    etcd-configuration.com {
        cache {
            etcd {
                configuration {
                    Endpoints etcd1:2379 etcd2:2379 etcd3:2379
                    AutoSyncInterval 1s
                    DialTimeout 1s
                }
            }
        }
    }
  4. Configure the Redis cache provider

    master

    The Redis provider can be configured using a url or a detailed configuration block. For advanced configuration, refer to the Souin documentation or rueidis client options.

    redis-url.com {
        cache {
            redis {
                url 127.0.0.1:6379
            }
        }
    }
  5. Configure cache-handler global options

    master

    Global options allow you to define default behaviors for the entire Caddy instance. Key configuration areas include:

    • api: Configures the REST API for cache management (basepath, Prometheus metrics, and Souin-specific endpoints).
    • cache_keys: Defines how cache keys are generated using regex patterns, allowing you to disable specific parts of the request (body, host, method, query) or include specific headers.
    • cdn: Configures CDN integration (e.g., Fastly) with settings for API keys, service IDs, and invalidation strategies.
    • distributed storage: Supports providers like etcd and olric for distributed caching.
    • ttl & stale: Sets the default Time-To-Live and stale period.
    • mode: Sets the operational mode (e.g., bypass).
    {
        cache {
            allowed_http_verbs GET POST PATCH
            api {
                basepath /some-basepath
                prometheus
                souin {
                    basepath /souin-changed-endpoint-path
                }
            }
            badger {
                path the_path_to_a_file.json
            }
            cache_keys {
                .*\.css {
                    disable_body
                    disable_host
                    disable_method
                    disable_query
                    headers X-Token Authorization
                    hide
                }
            }
            cache_name Another
            cdn {
                api_key XXXX
                dynamic
                email darkweak@protonmail.com
                hostname domain.com
                network your_network
                provider fastly
                strategy soft
                service_id 123456_id
                zone_id anywhere_zone
            }
            etcd {
                configuration {
                    # Your etcd configuration here
                }
            }
            key {
                disable_body
                disable_host
                disable_method
                headers Content-Type Authorization
            }
            log_level debug
            mode bypass
            nuts {
                path /path/to/the/storage
            }
            olric {
                url url_to_your_cluster:3320
                path the_path_to_a_file.yaml
                configuration {
                    # Your olric configuration here
                }
            }
            regex {
                exclude /test2.*
            }
            stale 200s
            ttl 1000s
            default_cache_control no-store
        }
    }
  6. Configure cache-handler directive options

    master

    The cache directive can be used within site blocks or matchers to apply specific caching logic to certain requests. This allows for granular control, such as using different cache_name values or different cache_keys patterns for specific URL paths or hostnames.

    @match path /path
    
    handle @match {
        cache {
            cache_name ChangeName
            cache_keys {
                (host1|host2).*\.css {
                    disable_body
                    disable_host
                    disable_method
                    disable_query
                    headers X-Token Authorization
                }
            }
            cdn {
                api_key XXXX
                dynamic
                email darkweak@protonmail.com
                hostname domain.com
                network your_network
                provider fastly
                strategy soft
                service_id 123456_id
                zone_id anywhere_zone
            }
            key {
                disable_body
                disable_host
                disable_method
                disable_query
                headers Content-Type Authorization
            }
            log_level debug
            regex {
                exclude /test2.*
            }
            stale 200s
            ttl 1000s
            default_cache_control no-store
        }
    }
  7. Configure the Olric cache provider

    master

    The Olric provider supports two modes:

    1. Client Mode: Requires the url directive to connect to an existing Olric cluster.
    2. Embedded Mode: Requires either a path or a configuration directive to run Olric within the Caddy process.
    # Client mode
    olric-url.com {
        cache {
            olric {
                url olric:3320
            }
        }
    }
    
    # Embedded mode
    olric-path.com {
        cache {
            olric {
                path /path/to/olricd.yml
            }
        }
    }
  8. Configure the Badger cache provider

    master

    The Badger provider allows you to use Badger as your cache storage. You can configure it using either a simple path or a detailed configuration block.

    Using a path: Specify the directory where Badger should store its data.

    Using configuration: You can provide fine-grained control over Badger settings (like ValueDir, SyncWrites, Compression, etc.) directly in the Caddyfile or JSON configuration.

    badger-path.com {
        cache {
            badger {
                path /tmp/badger/first-match
            }
        }
    }
    
    # Or with detailed configuration
    badger-configuration.com {
        cache {
            badger {
                configuration {
                    ValueDir /tmp/badger/data
                    SyncWrites true
                }
            }
        }
    }
  9. Configure the NutsDB cache provider

    master

    The NutsDB provider can be configured via a path or a configuration block.

    nuts-path.com {
        cache {
            nuts {
                path /tmp/nuts-path
            }
        }
    }
    
    nuts-configuration.com {
        cache {
            nuts {
                configuration {
                    Dir /tmp/nuts-configuration
                    SyncEnable true
                }
            }
        }
    }
  10. Configure the global Cache Configuration

    master

    The Configuration struct is the top-level container for the cache handler's settings. It holds the DefaultCache settings and allows for overrides based on specific URLs or cache key rules.

    Key fields:

    • DefaultCache: The fallback cache settings.
    • API: Configuration for enabling API endpoints (e.g., Prometheus, Souin, or Debug endpoints).
    • CacheKeys: Rules for overriding cache key generation.
    • URLs: A map of specific URL patterns to their own configurationtypes.URL overrides.
    • SurrogateKeys: A map of surrogate key configurations for cache invalidation.
    • LogLevel: The logging level for the handler.
    type Configuration struct {
    	DefaultCache      DefaultCache 
    	API               configurationtypes.API 
    	CacheKeys         configurationtypes.CacheKeys `json:"cache_keys"` 
    	URLs              map[string]configurationtypes.URL 
    	LogLevel          string 
    	SurrogateKeys     map[string]configurationtypes.SurrogateKeys 
    	SurrogateKeyDisabled bool 
    	logger            core.Logger 
    }
  11. Configure the Souin Caddy Cache Middleware

    master

    The SouinCaddyMiddleware (module ID http.handlers.cache) is a Caddy middleware that implements an RFC-7234 compliant HTTP cache system. It supports distributed storage (via Redis, Etcd, Olric, etc.), tag-based cache purging (surrogates), and customizable key generation.

    Key Configuration Options

    When configuring the middleware via JSON, you can use the following fields within the SouinCaddyMiddleware struct:

    FieldTypeDescription
    log_levelstringLogger level (falls back to Caddy's level if not set).
    allowed_http_verbs[]stringHTTP verbs allowed to be cached (e.g., GET, HEAD).
    allowed_additional_status_codes[]stringAdditional HTTP status codes that should be cached.
    headers[]stringHTTP headers to include in the cache key generation.
    timeoutconfigurationtypes.TimeoutTimeouts for backend and cache operations.
    ttlconfigurationtypes.DurationTime to live for a cached item.
    staleconfigurationtypes.DurationTime to live for a stale key.
    storers[]stringThe order and selection of storage providers to use.
    default_cache_controlstringThe default Cache-Control header value if none is provided by the upstream.
    cache_namestringThe name used in the Cache-Status response header.

    Supported Storage Providers

    You can configure various storage backends by providing their specific configuration objects:

    • badger: Badger storage.
    • etcd: Etcd distributed storage.
    • nats: Nats storage.
    • nuts: Nuts storage.
    • otter: Otter storage.
    • redis: Redis distributed storage.
    • olric: Olric distributed storage.
    • simplefs: SimpleFS storage.
    {
      "http": {
        "handlers": {
          "cache": {
            "log_level": "info",
            "allowed_http_verbs": ["GET"],
            "ttl": "5m",
            "redis": {
              "url": "redis://localhost:6379"
            },
            "storers": ["redis"]
          }
        }
      }
    }