Fix server lag when loading scripts

This was my bad, set up functions that requested the cache from server, didn't realise it was duplicating it and spamming it

This moves the requests to a separate file and uses an export instead

Also hopefully fixes ox_lib images being an arse
This commit is contained in:
Jim Shield
2025-06-26 23:15:33 +01:00
parent e204e6153d
commit e87aa8d74d
3 changed files with 68 additions and 43 deletions

32
clientFrameworkCache.lua Normal file
View File

@@ -0,0 +1,32 @@
if _G.__bridge_cache_loaded then return end
_G.__bridge_cache_loaded = true
_G.BridgeCache = {
hasCache = false,
data = nil,
queue = {},
}
RegisterNetEvent("jim_bridge:receiveCache", function(data)
if _G.BridgeCache.hasCache then return end
_G.BridgeCache.data = data
_G.BridgeCache.hasCache = true
for _, cb in ipairs(_G.BridgeCache.queue) do
cb(data)
end
_G.BridgeCache.queue = nil
end)
-- Only send the request ONCE per client session
TriggerServerEvent("jim_bridge:requestCache")
-- Exported async function
function GetBridgeCache(callback)
if _G.BridgeCache.hasCache then
callback(_G.BridgeCache.data)
else
table.insert(_G.BridgeCache.queue, callback)
end
end
exports("GetBridgeCache", GetBridgeCache)

View File

@@ -22,5 +22,6 @@ server_scripts {
} }
client_scripts { client_scripts {
'clientFrameworkCache.lua',
'ui_modules/*.lua' 'ui_modules/*.lua'
} }

View File

@@ -64,49 +64,41 @@ if IsDuplicityVersion() then
debugPrint("^6Bridge^7: ^2Shared cache successfully loaded from export^7.") debugPrint("^6Bridge^7: ^2Shared cache successfully loaded from export^7.")
--print(countTable(Items), countTable(Vehicles), countTable(Jobs)) --print(countTable(Items), countTable(Vehicles), countTable(Jobs))
else else
local hasCache = false -- Shared bridge cache - prevent duplicate listeners
-- 🔹 Client Side: Request from server exports["jim_bridge"]:GetBridgeCache(function(cache)
cache = {} Items = cache.Items or {}
Vehicles = cache.Vehicles or {}
Jobs = cache.Jobs or {}
Gangs = cache.Gangs or {}
InventoryWeight = cache.InventoryWeight or InventoryWeight
InventorySlots = cache.InventorySlots or 50
RegisterNetEvent("jim_bridge:receiveCache", function(data) CreateThread(function()
if not hasCache then if isStarted(ESXExport) then
cache = data for _, v in pairs(Vehicles) do
hasCache = true Vehicles[v.model] = {
else model = v.model,
return hash = v.hash,
end price = v.price,
end) name = v.name,
brand = GetMakeNameFromVehicleModel(v.model):lower():gsub("^%l", string.upper)
TriggerServerEvent("jim_bridge:requestCache") }
end
while not cache or not next(cache) do Wait(50) end
Items = cache.Items or {}
Vehicles = cache.Vehicles or {}
Jobs = cache.Jobs or {}
Gangs = cache.Gangs or {}
InventoryWeight = cache.InventoryWeight or InventoryWeight
InventorySlots = cache.InventorySlots or 50
if isStarted(ESXExport) then
for _, v in pairs(Vehicles) do
Vehicles[v.model] = {
model = v.model,
hash = v.hash,
price = v.price,
name = v.name,
brand = GetMakeNameFromVehicleModel(v.model):lower():gsub("^%l", string.upper)
}
end
end
if isStarted(OXInv) then
for k, v in pairsByKeys(Items) do
local tempInfo = exports[OXInv]:Items(k)
if tempInfo and tempInfo.client then
Items[k].image = (tempInfo.client and tempInfo.client.image) and tempInfo.client.image:gsub("nui://"..OXInv.."/web/images/", "") or k..".png"
Items[k].hunger = tempInfo.client and tempInfo.client.hunger
Items[k].thirst = tempInfo.client and tempInfo.client.thirst
end end
end
end if isStarted(OXInv) then
debugPrint("^6Bridge^7: ^2Shared cache successfully loaded from export^7.") for k, v in pairs(Items) do
local tempInfo = exports[OXInv]:Items(k)
Items[k].image = k..".png" -- Set default image
if tempInfo and tempInfo.client then -- if client info, check for hard set image
Items[k].image = (tempInfo.client.image) and tempInfo.client.image:gsub("nui://"..OXInv.."/web/images/", "") or Items[k].image
Items[k].hunger = tempInfo.client.hunger
Items[k].thirst = tempInfo.client.thirst
end
end
end
end)
debugPrint("^6Bridge^7: ^2Shared cache successfully loaded from export^7.")
end)
end end