// persistence
Database & cache
Database for durable key–value storage and Cache for in-memory TTL data.
Database
Lua
local Database = require("CrawlzUtils").Database
local db = Database.new("my_save", { mode = "memory", autosave = true })
Mode (Database.Backends) | Role |
|---|---|
auto | Automatic selection |
memory | RAM |
json | File-backed |
server | Server KV (when available) |
sqlite | SQLite (when available) |
Methods (selection): :get, :set, :delete, :has, :getRaw, :ensure, :update, :increment, :decrement, :append, :pull, :toggle, :getPath, :setPath, :deletePath, :keys, :values, :all, :count, :collection, :namespace, :flush, :reload, :markDirty, :batch, :backup, :export, :import, :close.
Example
local Database = require("CrawlzUtils").Database
local db = Database.new("economy", { mode = "memory" })
db:update("user:42", function(t)
t = type(t) == "table" and t or {}
t.coins = (tonumber(t.coins) or 0) + 10
return t
end, {})
db:flush()
Cache
Lua
local Cache = require("CrawlzUtils").Cache
local c = Cache.new({ maxSize = 5000, defaultTtl = 60 })
c:set("a", 1, 30, { "tag1" })
local v = c:get("a", 0)
c:remember("heavy", 120, function() return 42 end, { "tag1" })
c:flushTag("tag1")
| Area | Methods |
|---|---|
| Core | :set, :get, :has, :delete |
| Patterns | :remember, :touch, :ttl, :forever |
| Eviction | :tag, :flushTag, :clear, :sweep, :prune |
| Insight | :keys, :values, :size, :stats, :dump |
Static helpers target the default store.