lua and Openresty, multi-level cache

1.nginx optimization

A simple nginx configuration file:

user nginx; #user
worker_processes auto;#auto is recommended for the number of processes, and the number of cpu cores that automatically go to the server
error_log /var/log/nginx/error.log  error;
pid /var/run/nginx.pid;
worker_rlimit_nofile 204800;
events
{
    use epoll;#Use the most efficient epoll in Linux
    worker_connections 204800;
}

http
{
    include mime.types;#File types supported by nginx
    default_type application/octet-stream;
    charset utf-8;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 2k;
    large_client_header_buffers 4 4k;
    client_max_body_size 8m;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 60;
    fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
    keys_zone=TEST:10m
    inactive=5m;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 4k;
    fastcgi_buffers 8 4k;
    fastcgi_busy_buffers_size 8k;
    fastcgi_temp_file_write_size 8k;
    fastcgi_cache TEST;
    fastcgi_cache_valid 200 302 1h;
    fastcgi_cache_valid 301 1d;
    fastcgi_cache_valid any 1m;
    fastcgi_cache_min_uses 1;
    fastcgi_cache_use_stale error timeout invalid_header http_500;

    open_file_cache max=204800 inactive=20s;
    open_file_cache_min_uses 1;
    open_file_cache_valid 30s;

    tcp_nodelay on;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

    include /etc/nginx/conf.d/*.conf;
}

2. Multi level cache

2.1 introduction to Lua and Openresty

lua is a small script language written in standard C, which can be compiled and run on almost all operating systems and platforms. Its design purpose is to embed in the application, so as to provide flexible expansion and customization functions for the application.

Application scenarios: game development, independent application scripts, nested calls in redis to realize the functions of similar transactions, web container summary processing, NGINX filtering cache and other logic

Introduction to Openresty

OpenResty is a high-performance web platform based on Nginx and Lua, initiated by Chinese Zhang Yichun. It integrates a large number of sophisticated Lua libraries, third-party modules and most dependencies. It is used to facilitate the construction of dynamic web applications, web services and dynamic gateways that can handle ultra-high concurrency and high scalability

OpenResty's simple understanding achievement is equivalent to encapsulating NGINX and integrating Lua scripts. Developers can implement relevant logic by simply using the modules provided by it. Unlike before, they also need to write Lua scripts in NGINX.

install

  1. Pull an Openresty image
docker pull openresty/openresty
  1. Just build a container
docker run -p 90:90 -d --name openresty openresty/openresty
  1. Enter the container and view the path of the configuration file
docker exec -it openresty bash

cd /etc/nginx/conf.d

  1. Exit the container and copy the configuration file in the container to the host
docker cp openresty:/etc/nginx/conf.d/default.conf /docker/openresty/conf/default.conf

docker stop openresty
docker rm openresty

docker run -p 90:90 -d --name openresty -v /docker/openresty/conf/default.conf:/etc/nginx/conf.d/default.conf --privileged=true openresty/openresty
  1. Modify profile
server
{
    listen       90;
    listen       [::]:90;
    server_name  localhost;
    root /docker/www/webserver;
    index index.html;

    location /lmrs_home_index {
       content_by_lua_file /docker/www/lua/lmrs_home_index.lua;
    }
    location /del_ngx_cache {
       content_by_lua_file /docker/www/lua/del_nginx_cache.lua;
    }
}

2.2 home page multi-level cache strategy (take the classified data on the home page of the mall as an example, and the actual scenario is: guess your favorite goods and hot goods)

1. Use Lua to query the Nginx cache. If there is a cache, the classification data in the cache will be returned directly

2. If there is no classified data in Nginx cache, query Redis through Lua script. If there is data in Redis, store the data in Nginx cache and return the queried data

3. If there is no cache in Redis, query Mysql through Lua script. If there is data in Mysql, store the classified data into Redis cache and return the data

ngx.header.content_type = "application/json;charset=utf8"
local cache_ngx = ngx.shared.dis_cache;
local contentCache = cache_ngx:get("lmrs_home_index");
if contentCache == "" or contentCache == nil then
    local redis = require("resty.redis");
    local red = redis:new()
    red:set_timeout(2000)
    red:connect("172.17.0.5", 6379)
    local rescontent = red:get("lmrs_home_index");
    if ngx.null == rescontent or false == rescontent or "" == rescontent then
        local cjson = require("cjson");
        local mysql = require("resty.mysql");
        local db = mysql:new();
        db:set_timeout(2000)
        local props = {
            host = "172.17.0.4",
            port = 3306,
            database = "lmrs_2008_shops",
            user = "root",
            password = "root"
        }
        local res = db:connect(props);
        local select_sql = "select * from lmrs_product_categorys"
        res = db:query(select_sql);
        local responsejson = cjson.encode(res);
        red:set("lmrs_home_index", responsejson);
        ngx.say(responsejson);
        db:close()
        else
        cache_ngx:set("lmrs_home_index", rescontent, 10 * 60);
        ngx.say(rescontent)
    end
    red:close()
    else
    ngx.say(contentCache)
end

nginx has been configured above, so you don't need to configure it here. You need to pay attention here.

Posted by carichod on Thu, 04 Nov 2021 21:53:02 -0700