AsyncCache V2.0
Added:
- CacheObject (Object that keep your cached data, loads it and etc)
- TTL (Time to live - basically this is time to keep your data before clean)
- TTI (Time to idle - basically if you didn’t read cached data, then it will be cleared after time)
- CacheObject:GetStatus() (Returns status of CacheObject (Empty/Loading/Loaded))
- AsyncCache:GetCacheObject()
- Option argument (More about it below)
Changed:
- AsyncCache is more like wrapper above CacheObjects
- AsyncCache.new(name, handler, globalOptions?) - new signature of method
- AsyncCache:Load(key, options, otherArguments) - returns CacheObject or creates new if not exist
- AsyncCacheObject keep cache objects inside it
- AsyncCacheObject:Remove(key) clears CacheObject and removes key (:Clear() not removes key)
Okay, now let’s talk about some things.
AsyncCache:Method(key, otherArguments) call just do something like: AsyncCache:GetCacheObject(key):Method(otherArguments)
Below is signature of options (same for global options):
local options = {
["TTL"] = { -- or number
["Time"] = number,
["Reload"] = true -- true for reload, function for callback(cacheObject)
},
["TTI"] = { -- or number
["Time"] = number,
["Reload"] = true -- true for reload, function for callback(cacheObject)
}
}
}
options will override globalOptions.
options is for CacheObject:Load(options, otherArguments).
globalOptions is for AsyncCache.new(name, globalOptions)
New examples are below:
P.S: For examples below Imagine that :IsInGroup() method always returns true
TTL
local PlayersService = game:GetService("Players")
local AsyncCacheObject = require(script.AsyncCache).new("IsInGroupCache", function(player)
task.wait(0.15) --test purpose
return player:IsInGroup(14416268) --Yields
end)
PlayersService.PlayerAdded:Connect(function(player)
local CacheObject = AsyncCacheObject:Load(player, {
["TTL"] = {
["Time"] = 3,
["Reload"] = true -- or function(CacheObject, key)
},
})
local isInGroup = select(2, CacheObject:GetAsync():await())
print(isInGroup) -- true
task.wait(3)
print(CacheObject:Get(player)) -- nil (because of TTL) TTL = Time To Live, basically it is just auto clear data after time
-- Let's try AsyncCacheObject to call CacheObject methods!
isInGroup = select(2, AsyncCacheObject:GetAsync(player):await())
print(isInGroup) -- true (because we had set ["Reload"] = true in options for TTL)
task.wait(3)
print(AsyncCacheObject:Get(player)) --nil (again, because of TTL)
end)
PlayersService.PlayerRemoving:Connect(function(player)
AsyncCacheObject:Remove(player) --[[
call's CacheObject:Clear() and removes key, like:
AsyncCacheObject["CacheObjects"][key] = nil
]]
end)
TTI
local PlayersService = game:GetService("Players")
local AsyncCacheObject = require(script.AsyncCache).new("IsInGroupCache", function(player)
task.wait(0.15) --test purpose
return player:IsInGroup(14416268) --Yields
end)
PlayersService.PlayerAdded:Connect(function(player)
local CacheObject = AsyncCacheObject:Load(player, {
["TTI"] = {
["Time"] = 3,
["Reload"] = true -- or function(CacheObject, key)
},
})
local isInGroup = select(2, CacheObject:GetAsync():await())
task.wait(2)
print(CacheObject:Get()) -- true
task.wait(2)
print(CacheObject:Get()) -- true
task.wait(3.1)
print(CacheObject:Get()) -- nil because of TTI (Time to idle)
print(select(2, CacheObject:GetAsync():await())) -- true, because Reload property in TTI is set to true
end)
PlayersService.PlayerRemoving:Connect(function(player)
AsyncCacheObject:Remove(player) --[[
call's CacheObject:Clear() and removes key, like:
AsyncCacheObject["CacheObjects"][key] = nil
]]
end)
I hope you will like it, there’s shouldn’t be errors, but anyway I will fix them if they exist.
You can get module from here: Link
Do you have features to add on your mind? Let me know