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" "Moonshark/core/utils/logger"
"maps"
luajit "git.sharkk.net/Sky/LuaJIT-to-Go" 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") state.SetGlobal("__session_data")
// Reset modification flag // Reset modification flag and tracking
state.PushBoolean(false) state.PushBoolean(false)
state.SetGlobal("__session_modified") state.SetGlobal("__session_modified")
// Create empty modified keys table
state.NewTable()
state.SetGlobal("__session_modified_keys")
} else { } else {
// Initialize empty session // 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) 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) { if state.IsTable(-1) {
sessionData, err := state.ToTable(-1) sessionData, err := state.ToTable(-1)
if err == nil { if err == nil {
for k, v := range sessionData { maps.Copy(response.SessionData, sessionData)
response.SessionData[k] = v
}
} }
} }
state.Pop(1) state.Pop(1)

View File

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