🍮 PudimServer Docs

Module: PudimServer.cache

In-memory cache with TTL for HTTP responses.

Cache.new(config?)

Creates a cache with MaxSize and DefaultTTL.

Inputs: config? (optional table with MaxSize and DefaultTTL).

Output: Cache instance.

local Cache = require("PudimServer.cache")
local cache = Cache.new{ MaxSize = 100, DefaultTTL = 60 }

Cache:get(key)

Returns cached response or nil if expired or missing.

Inputs: key (string).

Output: cached string or nil.

local value = cache:get("GET:/users")
print(value)

Cache:set(key, response, ttl?)

Stores a response in cache with optional TTL.

Inputs: key (string), response (string), ttl? (optional number).

Output: no meaningful return value (entry stored in cache).

cache:set("GET:/users", "HTTP/1.1 200 OK", 30)

Cache:invalidate(key)

Invalidates a specific entry.

Inputs: key (string).

Output: no meaningful return value.

cache:invalidate("GET:/users")

Cache:clear()

Clears all cache entries.

Inputs: no parameters.

Output: no meaningful return value.

cache:clear()

Cache.createPipelineHandler(cache, ttl?)

Creates middleware to cache GET responses automatically.

Inputs: cache (Cache instance), ttl? (optional number).

Output: pipeline entry ({ name, Handler }).

local cacheHandler = Cache.createPipelineHandler(cache, 30)
server:UseHandler(cacheHandler)

CacheConfig

Fields accepted by Cache.new(config?).

FieldTypeDefaultDescription
MaxSizenumber?100Maximum number of entries stored in the cache. When exceeded, the oldest entry is evicted.
DefaultTTLnumber?60Time-to-live in seconds for each cache entry (used when ttl is not specified in set()).
local cache = Cache.new{
  MaxSize = 200,
  DefaultTTL = 120
}