ObjectCache
This module is a fork of https://github.com/Pyseph/ObjectCache
ObjectCaches provide an efficient way to manage and reuse BaseParts and Models in Roblox.
local ObjectCache = require(script.ObjectCache)
type ObjectCache<T> = ObjectCache.ObjectCache<T>
local myPartTemplate = Instance.new("Part")
myPartTemplate.Size = Vector3.new(1, 1, 1)
myPartTemplate.Anchored = true -- make sure your part is anchored!
local cache: ObjectCache<Part> = ObjectCache.new({
Name = "MyPartsCache",
Template = myPartTemplate,
})
local myPart = cache:Get()
myPart.CFrame = CFrame.new(0, 10, 0)
task.wait(1)
cache:Return(myPart)
Functions
setupModelForCacheMovement
Sets up a model for cache movement by welding all of its descendant BaseParts to its PrimaryPart.
isModelSetupForCacheMovement
Checks if a model is setup for cache movement.
new
Types
interface
CacheConfig {
Template:
T
|
(
)
→
T
--
The template object to use for the cache. Must be a PVInstance or a function that returns a PVInstance.
InitialSize:
number?
--
The initial size of the cache. Defaults to 10.
ExpansionSize:
number?
--
The amount to expand the cache by. Defaults to 50.
Name:
string?
--
The name of the cache.
}
Creates a new ObjectCache.
local myCache: ObjectCache<Part> = ObjectCache.new({
Template = function()
local part = Instance.new("Part")
part.Anchored = true
return part
end,
})
Anchored Parts
Make sure that your template object is anchored. Otherwise when it returns to the cache it will fall out of existence.
info
Luau LSP type inference for the template is not yet robust enough to properly infer the type of the template object. As a result, you should properly assign the right type to your cache object.
Get
Gets an object from the cache, moving it to the specified CFrame if provided.
Moving the returned object
If you provide a CFrame, the movement is deferred so it can be bulk moved. Keep this in mind if you need to do other operations on the object immediately after moving it.
Return
ObjectCache:
Return
(
obj:
T
) →
(
)
Returns an object to the cache.
ExpandCache
ObjectCache:
ExpandCache
(
Amount:
number
--
The amount to expand the cache by.
) →
(
)
Expands the cache by the specified amount.
SetExpandAmount
ObjectCache:
SetExpandAmount
(
Amount:
number
--
The amount to expand the cache by.
) →
(
)
Sets the default amount to expand the cache by.
IsInUse
Returns whether the specified object is currently in use.
BelongsTo
Checks if an object belongs to this cache.
Update
ObjectCache:
Update
(
) →
(
)
Forces an immediate position update for all objects in the cache.
Destroy
ObjectCache:
Destroy
(
) →
(
)
Destroys the cache and all objects within it.
ConnectOnReturn
ObjectCache:
ConnectOnReturn
(
fn:
(
obj:
T
)
→
(
)
--
The function to run.
) →
(
)
→
boolean
--
A cleaner function to disconnect the connection.
Sets a function to run when an object is returned to the cache. Passes the object that was returned as an argument.