CacheService - A Data Module

CacheService is a small data module, that provides cache storing, and datastore support.

Features:

  • Multi-script usage, since the service provides a function which allows you to get a CacheService instance, and get the original instance that you made in the other script (Basically, the instances don’t clone)
  • Store player data and add cache to it with a CacheBin instance, and store cache data with the CacheData instance.

Examples:

--// Example 1 //--
-- Making a new cache bin and setting data to it.
local CacheService = require(path.to.module)
local cacheHandler = CacheService.Init("Your Store Name") -- CacheService is an OOP module, which means you can create as many stores as you want to.

-- Remember to also change your datastore from the module script, or you can do it from the 'SetDataStore' function
cacheHandler:SetDataStore("Your Datastore Name") -- You can either set it by it's name.
-- cacheHandler:SetDataStore(GlobalDataStore object here) -- Or give a GlobalDataStore instance to change it to.

-- CacheBin uses the player object as the parameter, to get the user's UserId and set it inside the CacheBin.
local playerBin = cacheHandler:NewBin(path.to.player.object)
playerBin:Set("foo", 123) -- Usually, the data functions for the bin are dictionary-related, however, 
                          -- you can add your own data that is not a dictionary by using `playerBin.Data`, as that's where all the data is set.
print(playerBin:Get("foo")) -- Prints out the value "123". If there no value, prints out nil.
print(playerBin:Has("foo")) -- Prints out "true", since the key is inside the data table.
playerBin:Remove("foo") -- You can remove keys with this function.
print(playerBin:Get("foo")) -- nil
print(playerBin:Has("foo")) -- false
playerBin:Set("bar", 456)
--// Example 2 //--
-- Getting the player's CacheBin from datastore, storing data into it.
local CacheService = require(path.to.module)
local cacheHandler = CacheService.Init("myCacheHandler")
cacheHandler:SetDataStore("TestDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	local playerBin, justCreated = cacheHandler:GetBin(plr, true) -- Second parameter is called "isData"
                                                 -- if its true, it gets it from the datastore. If false, it gets it from the session data.
                                                 -- so its recommended to keep it true when a player is joining.

	-- CacheService instances also return a bool value when getting an instance from the SessionData.
	if justCreated then -- If the datastore didn't find the cache bin, it means it just created it inside the service.
		print("CacheBin not found! It was created just now!")
		-- You can also set the data in here if it was just created.
		playerBin.Data = {
			-- your data here, or use the `:Set` function to add key,value pairs.
		}
	else -- The datastore found the cache bin.
		print("The datastore found the CacheBin.")
	end

	print(playerBin.Data)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local playerBin = cacheHandler:GetBin(plr, false) -- Now, you are getting it from the SessionData
													   -- and not datastore, so make it false.

	playerBin:Save() -- You can save the data with the `:Save` function.
					 -- You can also define the datastore key you want to set your data to. (Leaving it nil ,defaults to the player's userid.
					-- And, you can also define another datastore to save it to. (Leaving it nil defaults to your one in the module script)
end)
--// Example 3 //--
-- Making stores and using them in other scripts.

-- Now, when doing these kinds of stuff, theres a thing you want to watch out for.
-- Sometimes, when defining a store in a script, and immediately using it in another (just as its created),
-- it will arise an error. So make sure to have some starter script that initializes them, or check if they aren't nil before doing anything with them.

--/// SCRIPT 1 ///--
	local CacheService = require(path.to.module)
	local myStore = CacheService.Init("Store1")

	-- The CacheData instance doesn't require a player object. It's basically the main part
	-- of the service where you store your server data.
	local newCacheData = CacheHandler:NewCache("myCache")
	newCacheData:Set("foo", 123)
	newCacheData:Set("bar", 456)

--/// SCRIPT 2 ///--
	local CacheService = require(path.to.module)
	local foundStore = CacheService.GetStore("Store1") -- Using the `GetStore` function will get a store
														-- with the specified name. And no, it doesn't get cloned. 
														-- It returns the same store we made in the first script above.

	if foundStore then -- Just do these checks to be sure, since the function returns nil if it doesn't find the store.
		print("Found the store!")
		local myCacheData = foundStore:GetCache("myCache")
		--if myCacheData then
			--print("Cache Data Found!")
			print(myCacheData:Get("foo")) -- 123
			print(myCacheData:Get("bar")) -- 456
		--else
			--print("Cache Data not found!")
		--end
	else
		print("Store doesn't exist!")
	end

More info is on the documentation inside the module script.

6 Likes

What is cache storing, what is that for, what is a cache

1 Like

Me too, I have no idea what is cache and cache storing

Caching is a concept in which potentially expensive operations are “stored” for future recall in case you’re fetching the same data.

3 Likes

It’s a term I made up that means “implement and retrieve data”, which corresponds to what the word “cache” also means - “short-term computer memory where information is stored for easy retrieval.”

So wait let me get this straight, So if I am making a system that spawns models and I decide to keep track of these models by storing them in a table for later use, Would that make it a cache?

not really, it’s more for a input - output usage, one example i used very basic caching for making a path to table script

“Folder/Module/BasePart”

It traverses a table via a path, kinda like JS does it

If you have a non-mutable table (meaning, that the elements don’t change) you can cache the path string to an element, which means that the script doesn’t have to go through the whole table to find your element, and it can get the element you’re looking for automatically

Although this stuff is made for NON-MUTABLE DATA! Caching isn’t meant to update frequently

For more information, go online and look up the term Caching - it will explain to you in detail when and how the system is used