Merged
Conversation
This change adds the ability to have kv stores that are not scoped
to a function, allowing data to be shared between multiple functions.
To make this possible, this change adds two new functions to the kv
interface: openNamed and closeNamed. The openNamed function takes a
string parameter that specifies a shared kv store to use for all futher
kv.get, kv.set and kv.delete calls until the kv.closeNamed function
is called, which returns to using to the default, function-scoped
kv store.
For example, let's say that I want to have an HTTP handler function
that increments a counter each time that it is called:
function handler(ctx, event)
kv.openNamed("shared")
local count = kv.get("visitCounter") or 0
count = count + 1
kv.set("visitCounter", count)
return {
statusCode = 200,
headers = { ["Content-Type"] = "application/json" },
body = json.encode({
message = "Visit count = " .. count
})
}
end
I also want to have a function that runs every minute to email a
report of the number of times that the first function has been
called:
function handler(ctx, event)
-- Get data to report
kv.openNamed("shared")
local count = kv.get("visitCounter") or 0
kv.closeNamed()
-- Send email via Resend
local result, err = email.send({
from = "reports@fakeurl.dev",
to = "reportreceivingperson@gmail.com",
subject = "Current visit counter value",
html = "<p>Current visit counter value is " .. count .. "</p>",
})
if err then
log.error("Email error: " .. err)
return {
statusCode = 500,
headers = { ["Content-Type"] = "application/json" },
body = json.encode({ error = err })
}
end
log.info("Email sent: " .. result.id)
return {
statusCode = 200,
headers = { ["Content-Type"] = "application/json" },
body = json.encode({
success = true,
email_id = result.id
})
}
end
By using kv.openNamed("shared") and kv.closeNamed(), both functions can
access the same kv store.
There shouldn't be any reason for that to be accessed outside of this file.
This change adds a global kv store that can be accessed by multiple
functions.
To make this possible, this change adds three new functions to the kv
interface: getGlobal, setGlobal and deleteGlobal. These functions map
the behavior of get, set, and delete.
For example, let's say that I want to have an HTTP handler function
that increments a counter each time that it is called:
function handler(ctx, event)
local count = kv.getGlobal("visitCounter") or 0
count = count + 1
kv.setGlobal("visitCounter", count)
return {
statusCode = 200,
headers = { ["Content-Type"] = "application/json" },
body = json.encode({
message = "Visit count = " .. count
})
}
end
I also want to have a function that runs every minute to email a
report of the number of times that the first function has been
called:
function handler(ctx, event)
-- Get data to report
local count = kv.getGlobal("visitCounter") or 0
-- Send email via Resend
local result, err = email.send({
from = "reports@fakeurl.dev",
to = "reportreceivingperson@gmail.com",
subject = "Current visit counter value",
html = "<p>Current visit counter value is " .. count .. "</p>",
})
if err then
log.error("Email error: " .. err)
return {
statusCode = 500,
headers = { ["Content-Type"] = "application/json" },
body = json.encode({ error = err })
}
end
log.info("Email sent: " .. result.id)
return {
statusCode = 200,
headers = { ["Content-Type"] = "application/json" },
body = json.encode({
success = true,
email_id = result.id
})
}
end
dimiro1
reviewed
Feb 22, 2026
Owner
|
Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change adds a global kv store that can be accessed by multiple
functions.
To make this possible, this change adds three new functions to the kv
interface: getGlobal, setGlobal and deleteGlobal. These functions map
the behavior of get, set, and delete.
For example, let's say that I want to have an HTTP handler function
that increments a counter each time that it is called:
I also want to have a function that runs every minute to email a
report of the number of times that the first function has been
called:
The kv.getGlobal, kv.setGlobal and kv.deleteGlobal functions allow access to a kv store that is shared by all function.