Cacher - A Cache Module for Instances

Hello everybody my name is Marketplier,

today we will be looking at my module made for saving Instances in a specific Place, which works similar to the PartCache module made by @Xan_TheDragon (PartCache, for all your quick part-creation needs), but it work all for Instances.

Example use cases are such as when you need like 50 frames for some reason in an instant (totally couldn’t relate) and since it is not that performant you could use this module instead; create a Cache and when you need those Instance, you can just get them from the Cache and return them back to the Cache when you don’t need them anymore.

API

Cacher.Create = function(template : Instance, storingPoint : Instance, amount : number) : Cache

Creates a Cache, creates a duplicate of the “template” Instance, which is parented to the “storingPoint” and creates duplicates of the “template” Instance equal to the “amount” and returns the Cache Instance.

Cache

Properties

OriginalTemplate : Instance

The original Instance that got passed into the “Cacher.Create” function as the “template” Instance.


Template : Instance

The Instance the objects are cloned off of — a clone of the “OriginalTemplate” Property, which is parented to the “StoringPoint” Property.


Objects : { Occupied : { Instance }, Unoccupied : { Instance } }

Where the Occupied and Unoccupied objects are stored.


StoringPoint : Instance

Where the Unoccupied Objects (Instances) are parented to/stored.


Functions

Fill : (self : Cache, amount : number) → nil

Clones/Creates new Instances based off of the Template Instance, which are then put into the “Unoccupied” table under the “Objects” table and under the “StoringPoint” property.


Destroy : (self : Cache, occupiedObjectsToo: boolean) → nil

Destroys and cleans up the Cache (I hope properly), and destroys the objects in the “Unoccupied” table and if the “destroyOccupiedToo” statement is true, it will also destroy the Instances in the “Occupied” table.


GetObject : (self : Cache) → Instance

Returns exactly 1 unoccupied Instance and puts it into occupied, and creates exactly 1 Instance if theres none and returns 1.


ReturnObject : (self : Cache, object : Instance) → nil

Puts the given “object” back into the “Unoccupied” table from “Occupied” of the Cache and parents the “object” to the “StoringPoint” Property.


GetObjects : (self : Cache, amount : number) → Instance

Returns as many unoccupied Instances as the “amount” and puts them into occupied, and creates as many needed Instances if theres not enough and as many Instances as “amount”.


ReturnObjects : (self : Cache, objects : {Instance}) → nil

Puts the given Instances inside of the “objects” table back into the “Unoccupied” table from “Occupied” of the Cache and parents the “objects” Instances to the “StoringPoint” Property.


You can get the module here: https://create.roblox.com/store/asset/93751263097927

If you find a bug, please report it thanks.
You can also suggest some features, if you want to do so.
Feedback is welcome ig.

5 Likes

Update 1

I updated the module; fixed/improved the code and added 2 function to the “Cache”; “ReturnObjects” and “GetObjects”, which work like “ReturnObject” and “GetObject”, but with multiple Instances.

Update 2

Fixed the cloned “Template” Instance not being destroyed upon :Destroy() call and added “OriginalTemplate” Property, which is the Instance that gets passed as the “template” Instance (not the cloned template Instance).