optimize sessions 1

This commit is contained in:
Sky Johnson 2025-04-09 20:54:49 -05:00
parent 85b0551e70
commit ac991f40a0
2 changed files with 22 additions and 21 deletions

View File

@ -9,6 +9,8 @@ import (
"Moonshark/core/utils/logger"
"maps"
luajit "git.sharkk.net/Sky/LuaJIT-to-Go"
)
@ -141,12 +143,16 @@ func (s *Sandbox) Execute(state *luajit.State, bytecode []byte, ctx *Context) (*
}
state.SetGlobal("__session_data")
// Reset modification flag
// Reset modification flag and tracking
state.PushBoolean(false)
state.SetGlobal("__session_modified")
// Create empty modified keys table
state.NewTable()
state.SetGlobal("__session_modified_keys")
} else {
// Initialize empty session
if err := state.DoString("__session_data = {}; __session_modified = false"); err != nil {
if err := state.DoString("__session_data = {}; __session_modified = false; __session_modified_keys = {}"); err != nil {
s.debugLog("Failed to initialize empty session data: %v", err)
}
}
@ -275,9 +281,7 @@ func (s *Sandbox) extractResponseData(state *luajit.State, response *Response) {
if state.IsTable(-1) {
sessionData, err := state.ToTable(-1)
if err == nil {
for k, v := range sessionData {
response.SessionData[k] = v
}
maps.Copy(response.SessionData, sessionData)
}
}
state.Pop(1)

View File

@ -14,6 +14,7 @@ __ready_modules = {}
__session_data = {}
__session_id = nil
__session_modified = false
__session_modified_keys = {}
-- ======================================================================
-- CORE SANDBOX FUNCTIONALITY
@ -299,12 +300,7 @@ local session = {
if type(key) ~= "string" then
error("session.get: key must be a string", 2)
end
if __session_data and __session_data[key] ~= nil then
return __session_data[key]
end
return nil
return __session_data and __session_data[key]
end,
-- Set a session value
@ -312,16 +308,9 @@ local session = {
if type(key) ~= "string" then
error("session.set: key must be a string", 2)
end
-- Ensure session data table exists
__session_data = __session_data or {}
-- Store value
__session_data[key] = value
-- Mark session as modified
__session_modified = true
return true
end,
@ -331,9 +320,10 @@ local session = {
error("session.delete: key must be a string", 2)
end
if __session_data then
if __session_data and __session_data[key] ~= nil then
__session_data[key] = nil
__session_modified = true
__session_modified_keys[key] = true
end
return true
@ -341,8 +331,15 @@ local session = {
-- Clear all session data
clear = function()
__session_data = {}
__session_modified = true
if __session_data and next(__session_data) then
-- Track all keys as modified
for k in pairs(__session_data) do
__session_modified_keys[k] = true
end
__session_data = {}
__session_modified = true
end
return true
end,