Skip to main content

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

ObjectCache.setupModelForCacheMovement(modelModel) → ()

Sets up a model for cache movement by welding all of its descendant BaseParts to its PrimaryPart.

isModelSetupForCacheMovement

ObjectCache.isModelSetupForCacheMovement(modelModel) → boolean

Checks if a model is setup for cache movement.

new

ObjectCache.new(configCacheConfig) → ()

Types

interface CacheConfig {
TemplateT | () → T--

The template object to use for the cache. Must be a PVInstance or a function that returns a PVInstance.

InitialSizenumber?--

The initial size of the cache. Defaults to 10.

ExpansionSizenumber?--

The amount to expand the cache by. Defaults to 50.

ObjectsParentInstance?--

The parent to put the objects in.

CacheParentInstance?--

The parent to put the cache in.

Namestring?--

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

ObjectCache:Get(moveToCFrame?) → T

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(objT) → ()

Returns an object to the cache.

ExpandCache

ObjectCache:ExpandCache(
Amountnumber--

The amount to expand the cache by.

) → ()

Expands the cache by the specified amount.

SetExpandAmount

ObjectCache:SetExpandAmount(
Amountnumber--

The amount to expand the cache by.

) → ()

Sets the default amount to expand the cache by.

IsInUse

ObjectCache:IsInUse(objPVInstance) → boolean

Returns whether the specified object is currently in use.

BelongsTo

ObjectCache:BelongsTo(objPVInstance) → boolean

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(objT) → ()--

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.

Show raw api
{
    "functions": [
        {
            "name": "_GetNew",
            "desc": "",
            "params": [
                {
                    "name": "Amount",
                    "desc": "",
                    "lua_type": "number"
                },
                {
                    "name": "Warn",
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "returns": [],
            "function_type": "method",
            "private": true,
            "source": {
                "line": 157,
                "path": "lib/objectcache/src/init.lua"
            }
        },
        {
            "name": "Get",
            "desc": "Gets an object from the cache, moving it to the specified CFrame if provided.\n:::caution Moving the returned object\nIf you provide a CFrame, the movement is deferred so it can be bulk moved.\nKeep this in mind if you need to do other operations on the object immediately after moving it.\n:::",
            "params": [
                {
                    "name": "moveTo",
                    "desc": "",
                    "lua_type": "CFrame?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "T\r\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 199,
                "path": "lib/objectcache/src/init.lua"
            }
        },
        {
            "name": "Return",
            "desc": "Returns an object to the cache.",
            "params": [
                {
                    "name": "obj",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 220,
                "path": "lib/objectcache/src/init.lua"
            }
        },
        {
            "name": "ExpandCache",
            "desc": "Expands the cache by the specified amount.",
            "params": [
                {
                    "name": "Amount",
                    "desc": "The amount to expand the cache by.",
                    "lua_type": "number"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 246,
                "path": "lib/objectcache/src/init.lua"
            }
        },
        {
            "name": "SetExpandAmount",
            "desc": "Sets the default amount to expand the cache by.",
            "params": [
                {
                    "name": "Amount",
                    "desc": "The amount to expand the cache by.",
                    "lua_type": "number"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 255,
                "path": "lib/objectcache/src/init.lua"
            }
        },
        {
            "name": "IsInUse",
            "desc": "Returns whether the specified object is currently in use.",
            "params": [
                {
                    "name": "obj",
                    "desc": "",
                    "lua_type": "PVInstance"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean\r\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 263,
                "path": "lib/objectcache/src/init.lua"
            }
        },
        {
            "name": "BelongsTo",
            "desc": "Checks if an object belongs to this cache.",
            "params": [
                {
                    "name": "obj",
                    "desc": "",
                    "lua_type": "PVInstance"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean\r\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 270,
                "path": "lib/objectcache/src/init.lua"
            }
        },
        {
            "name": "Update",
            "desc": "Forces an immediate position update for all objects in the cache.",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 277,
                "path": "lib/objectcache/src/init.lua"
            }
        },
        {
            "name": "Destroy",
            "desc": "Destroys the cache and all objects within it.",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 284,
                "path": "lib/objectcache/src/init.lua"
            }
        },
        {
            "name": "ConnectOnReturn",
            "desc": "Sets a function to run when an object is returned to the cache. Passes the object that was returned as an argument.",
            "params": [
                {
                    "name": "fn",
                    "desc": "The function to run.",
                    "lua_type": "(obj: T) -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "A cleaner function to disconnect the connection.",
                    "lua_type": "() -> boolean"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 293,
                "path": "lib/objectcache/src/init.lua"
            }
        },
        {
            "name": "setupModelForCacheMovement",
            "desc": "Sets up a model for cache movement by welding all of its descendant BaseParts to its PrimaryPart.",
            "params": [
                {
                    "name": "model",
                    "desc": "",
                    "lua_type": "Model"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 358,
                "path": "lib/objectcache/src/init.lua"
            }
        },
        {
            "name": "isModelSetupForCacheMovement",
            "desc": "Checks if a model is setup for cache movement.",
            "params": [
                {
                    "name": "model",
                    "desc": "",
                    "lua_type": "Model"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 367,
                "path": "lib/objectcache/src/init.lua"
            }
        },
        {
            "name": "new",
            "desc": "Creates a new ObjectCache.\n\n```lua\nlocal myCache: ObjectCache<Part> = ObjectCache.new({\n    Template = function()\n        local part = Instance.new(\"Part\")\n        part.Anchored = true\n        return part\n    end,\n})\n```\n\n:::warning Anchored Parts\nMake sure that your template object is anchored. Otherwise when it returns to the cache it will fall out of existence.\n:::\n\n:::info\nLuau LSP type inference for the template is not yet robust enough to properly infer the type of the template object.\nAs a result, you should properly assign the right type to your cache object.\n:::",
            "params": [
                {
                    "name": "config",
                    "desc": "",
                    "lua_type": "CacheConfig"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 407,
                "path": "lib/objectcache/src/init.lua"
            }
        }
    ],
    "properties": [],
    "types": [
        {
            "name": "CacheConfig",
            "desc": "",
            "fields": [
                {
                    "name": "Template",
                    "lua_type": "T | () -> T",
                    "desc": "The template object to use for the cache. Must be a PVInstance or a function that returns a PVInstance."
                },
                {
                    "name": "InitialSize",
                    "lua_type": "number?",
                    "desc": "The initial size of the cache. Defaults to 10."
                },
                {
                    "name": "ExpansionSize",
                    "lua_type": "number?",
                    "desc": "The amount to expand the cache by. Defaults to 50."
                },
                {
                    "name": "ObjectsParent",
                    "lua_type": "Instance?",
                    "desc": "The parent to put the objects in."
                },
                {
                    "name": "CacheParent",
                    "lua_type": "Instance?",
                    "desc": "The parent to put the cache in."
                },
                {
                    "name": "Name",
                    "lua_type": "string?",
                    "desc": "The name of the cache."
                }
            ],
            "source": {
                "line": 380,
                "path": "lib/objectcache/src/init.lua"
            }
        }
    ],
    "name": "ObjectCache",
    "desc": "This module is a fork of https://github.com/Pyseph/ObjectCache\n\nObjectCaches provide an efficient way to manage and reuse BaseParts and Models in Roblox.\n\n```lua\nlocal ObjectCache = require(script.ObjectCache)\ntype ObjectCache<T> = ObjectCache.ObjectCache<T>\n\nlocal myPartTemplate = Instance.new(\"Part\")\nmyPartTemplate.Size = Vector3.new(1, 1, 1)\nmyPartTemplate.Anchored = true -- make sure your part is anchored!\n\nlocal cache: ObjectCache<Part> = ObjectCache.new({\n    Name = \"MyPartsCache\",\n    Template = myPartTemplate,\n})\n\nlocal myPart = cache:Get()\n\nmyPart.CFrame = CFrame.new(0, 10, 0)\n\ntask.wait(1)\n\ncache:Return(myPart)\n```",
    "source": {
        "line": 32,
        "path": "lib/objectcache/src/init.lua"
    }
}