Cachet was made due to personal fustration with repeated code responsible for caching. I designed it to be as powerful and robust as possible to be a one-fits-all solution for my caching needs.
Cachet ended up being a really nice module, so I decided to make it open-source.
Code Examples
Hello, Cachet!
This is a very basic example showing how simple it is to get set-up with Cachet.
-- Import Cachet
local Cachet = require(path.to.cachet)
-- Create a new cache, setting 'favouriteNumber' to 32 right off the bat
local cache = Cachet.new({ favouriteNumber = 32 })
-- Output the current value of 'favouriteNumber' (32)
print(cache:retrieve("favouriteNumber"))
-- Connect a callback to when the cache is updated
-- and output details about the change.
cache:connect(function(...)
print(...)
end)
-- Update 'favouriteNumber' to 64 and invalidate this entry afte 10 seconds
-- This means that after 10 seconds, 'favouriteNumber' will be invalidated
-- and set to nil.
cache:store("favouriteNumber", 64, 10)
-- Output the current value of favouriteNumber (now 64)
print(cache:retrieve("favouriteNumber"))
Using this code, we should get the following output immediately:
32 table: 0x6bf449a775e95c23
favouriteNumber 32 64 12951698-3505-4A42-B704-8742B74462E6
64 table: 0xe33151b518f7cd33
And then ten seconds later:
favouriteNumber 64 nil nil
This shows:
- Where we define our original
favouriteNumber
to32
. - It then shows where we change our
favouriteNumber
to64
. - And finally, it shows where the cache is invalidated ten seconds later
As you can see, it is very easy to get started with Cachet.
Cooldown System
Cachet is designed to be a very powerful system and can be used in very specific use cases.
In this example, we’ll create a basic (but inefficient) command-cooldown system using Cachet’s features
-- Import Cachet
local Cachet = require(workspace.Cachet)
-- Create a new cache. We don't need any default values yet
local cooldowns = Cachet.new()
local commandCooldowns = {
kill = 5, -- Give the 'kill' command a 5 second cooldown
kick = 20, -- Give the 'kick' command a 20 second cooldown
}
local function checkCooldown(player, command)
-- Check the cooldown. Note that keys must be strings.
if cooldowns:retrieve(tostring(player.UserId)) then
-- The user is within the cooldown. This command should not run.
return false
end
-- Update the cooldown based on the command
cooldowns:store(tostring(player.UserId), true, commandCooldowns[command])
return true
--[[
While this is not an ideal system, it still is a pretty good example.
Additionally, you could instead cache timestamps (or use the cache entrys's 'stored' meta-property).
]]
end
checkCooldown(player1, "kick") --> true
checkCooldown(player1, "kick") --> false
wait(20)
checkCooldown(player1, "kick") --> true
checkCooldown(player1, "kill") --> false
If you have an example you’d like to show off here, DM me on the DevForum with it!
Documentation & Guide
All relevant information can be found on the README
. In the future, I’ll probably set up a handy website containing all details and guides, instead of a basic API reference.
Changelog
I will be posting news, changelogs, help requests, etc on this thread. Please mark it as “watching” so you get notifications whenever I post on it.
Please do not hesitate to ask if you have any questions or concerns! <3