Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion] Cache setter #678

Open
chozr7 opened this issue Dec 10, 2024 · 1 comment
Open

[Suggestion] Cache setter #678

chozr7 opened this issue Dec 10, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@chozr7
Copy link

chozr7 commented Dec 10, 2024

Is your feature request related to a problem? Please describe.

  • When setting cache values, I personally don't really want to do cache:set('human', false) instead I like doing cache.human = false, although this does indeed set the value it doesn't trigger the event meaning onCache wouldn't be told the value changed!

Describe the solution you'd like

The cache metatable right now

local cache = setmetatable({ game = GetGameName(), resource = resourceName }, {
    __index = function(self, key)
        cacheEvents[key] = {}

        AddEventHandler(('ox_lib:cache:%s'):format(key), function(value)
            local oldValue = self[key]
            local events = cacheEvents[key]

            for i = 1, #events do
                Citizen.CreateThreadNow(function()
                    events[i](value, oldValue)
                end)
            end

            self[key] = value
        end)

        return rawset(self, key, export.cache(nil, key) or false)[key]
    end,

    __call = function(self, key, func, timeout)
        local value = rawget(self, key)

        if value == nil then
            value = func()

            rawset(self, key, value)

            if timeout then SetTimeout(timeout, function() self[key] = nil end) end
        end

        return value
    end
})

can we just add this into the metatable

    __newindex = function(self, key, value)
        if value ~= self[key] then
            TriggerEvent(('ox_lib:cache:%s'):format(key), value, self[key])
            rawset(self, key, value)
        end
    end

this should make it where the onCache works properly when setting with cache.human = true, unless im just a retard <3

Additional context
no..

@chozr7 chozr7 added the enhancement New feature or request label Dec 10, 2024
@Moozdzn
Copy link
Contributor

Moozdzn commented Dec 22, 2024

__newindex only triggers when the value is not found, meaning updating the human key if a value is already present won’t trigger __newindex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants