Skip to main content

Pulse.Cache

Pulse.Cache stores the result of a function call and returns the cached value until a timeout expires. Use it when you're calling something expensive (like GetChildren()) more often than necessary.


Simple wrapper

local getTitans = Pulse.Cache.wrap(2, function()
return workspace.Titans.Alive:GetChildren()
end)

-- Call it anywhere — only actually calls GetChildren() every 2 seconds
local titans = getTitans()

Key-value cache object

For more control, create a cache object with per-key TTLs:

local c = Pulse.Cache.new(5) -- 5-second TTL for all keys

c:set("shifters", shifterList)
local list = c:get("shifters") -- nil after 5 seconds

c:has("shifters") -- bool: is this key still valid?
c:del("shifters") -- invalidate one key early
c:clear() -- invalidate all keys

When to use it

Use Pulse.Cache.wrap when:

  • You call GetChildren(), GetDescendants(), or similar APIs more than once per second
  • The result doesn't change every frame
  • You're OK with up to N-second staleness

Don't use it when:

  • You need the absolute latest value every frame (just call it directly)
  • The cached value holds references to instances that might be destroyed (stale references cause errors)

Limitation

The cached value is a reference. If the underlying instances are destroyed, the cache still holds the old references. The globals.lua pattern (polling a live set) is better for tracking live entity lists.