xfy 4562dd5a7d docs: 添加子目录 AGENTS.md 文档
deepinit 生成的子目录级 AGENTS.md 文件,帮助 AI agents 理解各功能模块:
- docs/config/: advanced, basic, caching, load-balancing, lua, rewriting, security, ssl
- docs/lua/: api-gateway, authentication, caching, dynamic-routing, logging-monitoring, middleware, rate-limiting, websocket

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-23 10:35:49 +08:00
..

Nginx Lua Caching Example

Demonstrates response caching using ngx.shared.DICT with TTL and cache invalidation support.

Components

File Purpose
nginx.conf Nginx configuration wiring Lua handlers
cache_handler.lua Shared cache logic with TTL and invalidation

Features

  • Cache responses with configurable TTL
  • Manual cache invalidation via purge endpoint
  • X-Cache header (HIT/MISS) for debugging
  • LRU eviction when memory is full

Usage

  1. Define a shared dict in nginx.conf:

    lua_shared_dict response_cache 10m;
    
  2. Use cache_handler.lua in your location blocks:

    content_by_lua_file /path/to/cache_handler.lua;
    
  3. Purge a specific cache key:

    curl -X POST http://example.com/cache/purge -d '{"key": "my_key"}'
    
  4. Purge all cached entries:

    curl -X POST http://example.com/cache/purge_all