lua-resty-cache-stats

"用于在 Nginx 中跟踪和报告缓存统计信息的 Lua 模块"

$ opm get iakuf/lua-resty-cache-stats

lua-resty-cache-stats

一个用于在 Nginx 中跟踪和报告缓存统计信息的 Lua 模块,支持使用定时器进行周期性统计记录。

安装

要安装此模块,可以使用 OpenResty 包管理器 (opm)

    opm get iakuf/lua-resty-cache-stats

依赖项

此模块需要 lua-cjson 库才能正常工作。 确保 lua-cjson 已安装并在 OpenResty 环境中可用。

用法

Nginx 配置

将以下配置添加到您的 nginx.conf 文件中

  1. 定义共享字典:

    http {
        lua_shared_dict cache_stats 10m;
    }
  1. 在 worker 中初始化模块:

    http {
        init_worker_by_lua_block {
            local cache_stats = require "resty.cache_stats"
            cache_stats.init()
        }
    }
  1. 跟踪缓存统计信息:

    log_by_lua_block 中添加以下块以记录请求状态

    http {
        server {
            listen 80;
            server_name example.com;
    
            location / {
                log_by_lua_block {
                    local cache_stats = require "resty.cache_stats"
                    cache_stats.track()
                }
                # Other configurations...
            }
        }
    }
  1. 显示缓存统计信息:

    添加以下 location 块以显示缓存统计信息,类似于 squidclient

    http {
        server {
            listen 80;
            server_name example.com;
    
            location /cache_status {
                content_by_lua_block {
                    local cache_stats = require "resty.cache_stats"
                    cache_stats.get_stats()
                }
            }
        }
    }

它将显示

    Cache Stats Overview:
    
    Request Hit Rates:
    1 Minute Request Hit Rate: 86.24
    5 Minutes Request Hit Rate: 93.99
    60 Minutes Request Hit Rate: 99.20
    
    Traffic Hit Rates:
    1 Minute Traffic Hit Rate: 66.96
    5 Minutes Traffic Hit Rate: 77.38
    60 Minutes Traffic Hit Rate: 94.31
    
    
    Detailed Stats(1min):
    1min_hits: 1385
    1min_misses: 221
    1min_requests: 1606
    1min_hit_bytes: 118331709
    1min_missed_bytes: 58399549
    1min_bytes: 176731258
    
    
    Detailed Stats(5min):
    5min_hits: 6408
    5min_misses: 410
    5min_requests: 6818
    5min_hit_bytes: 744406471
    5min_missed_bytes: 217558265
    5min_bytes: 961964736
    
    
    Detailed Stats(60min):
    60min_hits: 589097
    60min_misses: 4758
    60min_requests: 593855
    60min_hit_bytes: 35882471518
    60min_missed_bytes: 2166475562
    60min_bytes: 38048947080

示例

这是一个带有必要配置的示例 nginx.conf 文件

    http {
        lua_shared_dict cache_stats 10m;
    
        init_worker_by_lua_block {
            local cache_stats = require "resty.cache_stats"
            cache_stats.init()
        }
    
        server {
            listen 80;
            server_name example.com;
    
            location / {
                log_by_lua_block {
                    local cache_stats = require "resty.cache_stats"
                    cache_stats.track()
                }
                # Other configurations...
            }
    
            location /cache_status {
                content_by_lua_block {
                    local cache_stats = require "resty.cache_stats"
                    cache_stats.get_stats()
                }
            }
        }
    }

许可证

MIT

作者

"Fu Kai <iakuf@163.com>"

许可证

mit

版本