Skip to content

Redis Cache setup example

Conor McKnight edited this page May 5, 2026 · 15 revisions

This example is to show how with Redis you can Cache the results grabbed from a redis server what will help performance especially servers in different locations. https://github.com/C0nw0nk/Nginx-Lua-Anti-DDoS/wiki/Redis-setup-example

With Redis Cache Example

#http block
#Initialize a shared Lua cache for Redis queries
lua_shared_dict RedisQueries 10m;

    # you do not need the following line if you are using
    # the OpenResty bundle:
    lua_package_path "/path/to/lua-resty-redis/lib/?.lua;;";

    server {
        location /test {
            # need to specify the resolver to resolve the hostname
            resolver 8.8.8.8;

            rewrite_by_lua_block {
			
	local Query = ngx.shared.RedisQueries
	if not res then
	res = nil
	end
	local QResult = Query:get(res) -- disable this line disables the cache
	if not QResult then
			
                local redis = require "resty.redis"
                local red = redis:new()

                red:set_timeouts(1000, 1000, 1000) -- 1 sec

                -- or connect to a unix domain socket file listened
                -- by a redis server:
                --     local ok, err = red:connect("unix:/path/to/redis.sock")

                -- connect via ip address directly
                local ok, err = red:connect("127.0.0.1", 6379)

                if not ok then
                    ngx.say("failed to connect: ", err)
                    return
                end

				localized_global = {} --define global var that script can read

                ok, err = red:set("secret", " enigma") --res is a global var so cache can work
                if not ok then
                    ngx.say("failed to set secret key: ", err)
                    return
                end

                --ngx.say("set result: ", ok)

                res, err = red:get("secret")
                if not res then
                    ngx.say("failed to get dog: ", err)
                    return
				else
					--result found do something
					localized_global.secret = res --nginx will now set secret key via Redis Database
                end

                if res == ngx.null then
                    ngx.say("dog not found.")
                    return
                end

                --ngx.say("secret key: ", res)

                -- put it into the connection pool of size 100,
                -- with 10 seconds max idle time
                local ok, err = red:set_keepalive(10000, 100)
                if not ok then
                    ngx.say("failed to set keepalive: ", err)
                    return
                end

                -- or just close the connection right away:
                -- local ok, err = red:close()
                -- if not ok then
                --     ngx.say("failed to close: ", err)
                --     return
                -- end
			
	else
		localized_global = {} --define global var that script can read
		localized_global.secret = QResult --nginx will now set secret key via Cached Redis database result
	end
			
            }
        }
    }

Clone this wiki locally