mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-19 07:56:02 +01:00
refactor(init): separate global and local cache
Store temporary data in the _cache local, separate from the global cache. This allows constants and other values to be added, rather than being wiped every minute.
This commit is contained in:
29
init.lua
29
init.lua
@@ -145,21 +145,14 @@ local ox = GetResourceState('ox_core') ~= 'missing' and setmetatable({}, {
|
|||||||
groups = GetResourceState('ox_groups') ~= 'missing',
|
groups = GetResourceState('ox_groups') ~= 'missing',
|
||||||
}
|
}
|
||||||
|
|
||||||
local cache = setmetatable({}, {
|
local _cache = setmetatable({}, {
|
||||||
__index = function(self, key)
|
__index = function(self, key)
|
||||||
return rawset(self, key, exports[ox_lib]['cache'](nil, key) or false)[key]
|
return rawset(self, key, exports[ox_lib].cache(nil, key) or false)[key]
|
||||||
end,
|
end,
|
||||||
|
|
||||||
__call = function(self)
|
__call = function(self)
|
||||||
table.wipe(self)
|
table.wipe(self)
|
||||||
|
|
||||||
if service == 'client' then
|
|
||||||
self.playerId = PlayerId()
|
|
||||||
self.serverId = GetPlayerServerId(self.playerId)
|
|
||||||
end
|
|
||||||
|
|
||||||
self.resource = GetCurrentResourceName()
|
|
||||||
|
|
||||||
if ox.groups then
|
if ox.groups then
|
||||||
self.groups = setmetatable({}, {
|
self.groups = setmetatable({}, {
|
||||||
__index = function(groups, index)
|
__index = function(groups, index)
|
||||||
@@ -171,9 +164,21 @@ local cache = setmetatable({}, {
|
|||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
|
||||||
|
cache = setmetatable({
|
||||||
|
resource = GetCurrentResourceName(),
|
||||||
|
}, {
|
||||||
|
__index = _cache,
|
||||||
|
__metatable = _cache
|
||||||
|
})
|
||||||
|
|
||||||
|
if service == 'client' then
|
||||||
|
cache.playerId = PlayerId()
|
||||||
|
cache.serverId = GetPlayerServerId(cache.playerId)
|
||||||
|
end
|
||||||
|
|
||||||
Citizen.CreateThreadNow(function()
|
Citizen.CreateThreadNow(function()
|
||||||
while true do
|
while true do
|
||||||
cache()
|
_cache()
|
||||||
Wait(60000)
|
Wait(60000)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
@@ -184,8 +189,6 @@ AddEventHandler('ox_lib:updateCache', function(data)
|
|||||||
lib.onCache[key](value)
|
lib.onCache[key](value)
|
||||||
end
|
end
|
||||||
|
|
||||||
cache[key] = value
|
_cache[key] = value
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
_ENV.cache = cache
|
|
||||||
|
|||||||
Reference in New Issue
Block a user