-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_example.lua
More file actions
101 lines (80 loc) · 2.86 KB
/
cache_example.lua
File metadata and controls
101 lines (80 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
--- Cache Example
--- Run: lua ./examples/cache_example.lua
--- Test: curl http://localhost:8085/api/time (note the timestamp)
--- Test: curl http://localhost:8085/api/time (same timestamp = cached!)
--- Test: curl -X POST http://localhost:8085/api/data (POST not cached)
local PudimServer = require("PudimServer")
local Cache = require("PudimServer.cache")
local server = PudimServer:Create{
Port = 8085,
Address = "localhost",
ServiceName = "Cache Example",
Middlewares = {}
}
-- Create cache: max 50 entries, 10 second TTL
local cache = Cache.new{
MaxSize = 50,
DefaultTTL = 10
}
-- Add cache as pipeline handler (only caches GET requests)
server:UseHandler(Cache.createPipelineHandler(cache))
-- Pipeline handler: logger to show cache hits
server:UseHandler{
name = "logger",
Handler = function(req, res, next)
print(("[%s] %s %s"):format(os.date("%H:%M:%S"), req.method, req.path))
return next()
end
}
-- This route returns the current timestamp
-- On first call: handler runs, response is cached for 10s
-- On subsequent calls within 10s: cached response returned (same timestamp)
server:Routes("/api/time", function(req, res)
if req.method == "GET" then
print(" -> handler executed (not from cache)")
return res:Response(200, {
timestamp = os.time(),
msg = "If this timestamp stays the same, the response is cached!"
})
end
return res:Response(405, { error = "Method not allowed" })
end)
-- POST requests are never cached
server:Routes("/api/data", function(req, res)
if req.method == "POST" then
return res:Response(200, {
msg = "POST is never cached",
timestamp = os.time()
})
end
return res:Response(405, { error = "Method not allowed" })
end)
-- You can also use the cache directly for manual control
server:Routes("/api/cached-manual", function(req, res)
if req.method == "GET" then
local key = "manual:greeting"
local cached = cache:get(key)
if cached then
return cached
end
local response = res:Response(200, {
msg = "Manually cached for 30 seconds",
timestamp = os.time()
})
cache:set(key, response, 30)
return response
end
-- Invalidate cache on DELETE
if req.method == "DELETE" then
cache:invalidate("manual:greeting")
return res:Response(200, { msg = "Cache invalidated!" })
end
return res:Response(405, { error = "Method not allowed" })
end)
print("=== Cache Example running at http://localhost:8085 ===")
print("Try: curl http://localhost:8085/api/time (note timestamp)")
print("Try: curl http://localhost:8085/api/time (same timestamp = cached)")
print(" Wait 10s, try again (new timestamp = expired)")
print("Try: curl -X POST http://localhost:8085/api/data (POST never cached)")
print("Try: curl http://localhost:8085/api/cached-manual (manual cache, 30s TTL)")
server:Run()