From 58faab6217416c08afcac4f53cc27e42a3232dbb Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Tue, 1 Mar 2022 13:23:47 +0000 Subject: [PATCH] feat(init): implement cache --- init.lua | 83 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 13 deletions(-) diff --git a/init.lua b/init.lua index a79c055..70cc22b 100644 --- a/init.lua +++ b/init.lua @@ -1,3 +1,20 @@ + +-- ox_lib +-- Copyright (C) 2021 Linden + +-- This program is free software: you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. + +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. + +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see + if not _VERSION:find('5.4') then error('^1Lua 5.4 must be enabled in the resource manifest!^0', 3) end @@ -75,7 +92,6 @@ lib = setmetatable({ return lualib end, }) -import = lib local intervals = {} --- Dream of a world where this PR gets accepted. @@ -116,18 +132,59 @@ function ClearInterval(id) intervals[id] = -1 end --- ox_lib --- Copyright (C) 2021 Linden --- This program is free software: you can redistribute it and/or modify --- it under the terms of the GNU General Public License as published by --- the Free Software Foundation, either version 3 of the License, or --- (at your option) any later version. +----------------------------------------------------------------------------------------------- +-- Cache +----------------------------------------------------------------------------------------------- --- This program is distributed in the hope that it will be useful, --- but WITHOUT ANY WARRANTY; without even the implied warranty of --- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --- GNU General Public License for more details. +local ox = GetResourceState('ox_core') ~= 'missing' and setmetatable({}, { + __index = function() + return true + end +}) or { + groups = GetResourceState('ox_groups') ~= 'missing', +} --- You should have received a copy of the GNU General Public License --- along with this program. If not, see +local cache_mt = { + __index = function(self, key) + return rawset(self, key, exports[lualib]['cache'](nil, key) or false) + end, + + __call = function(self) + table.wipe(self) + + if IsDuplicityVersion then + + else + self.playerId = PlayerId() + end + + if ox.groups then + self.groups = setmetatable({}, { + __index = function(groups, index) + groups[index] = GlobalState['group:'..index] + return groups[index] + end + }) + end + end +} + +local cache = setmetatable({}, cache_mt) + +Citizen.CreateThreadNow(function() + while true do + cache() + Wait(60000) + end +end) + +AddEventHandler('lualib:updateCache', function(data) + for key, value in pairs(data) do + if cache[key] ~= nil then + cache[key] = value + end + end +end) + +_ENV.cache = cache