Skip to content

Cache

Source: shiva-core/server/sv_cache.lua

In-process key/value cache with optional TTL. Does not persist across resource restarts.

Server only

Cache is a server-side global injected by shiva-core. It is not available on the client.

Availability

Cache is a global in every shiva module running server-side. No import required.

Methods

Cache.set(key, value, ttl?)

Store a value. Pass ttl (seconds) to auto-expire.

lua
Cache.set('player:1:name', 'John')
Cache.set('session:token', token, 300)  -- expires in 5 minutes

Cache.get(key)

Retrieve a value. Returns nil if absent or expired.

lua
local name = Cache.get('player:1:name')

Cache.has(key)

Return true if the key exists and has not expired.

lua
if Cache.has('player:1:name') then ... end

Cache.forget(key)

Remove a single key.

lua
Cache.forget('player:1:name')

Cache.forgetPrefix(prefix)

Remove all keys starting with prefix.

lua
Cache.forgetPrefix('player:1:')  -- removes all cached data for player 1

Cache.remember(key, fn, ttl?)

Get a cached value, or compute and cache it if absent.

lua
local data = Cache.remember('economy:rates', function()
    return DB.table('exchange_rates'):get()
end, 60)

Cache.flush()

Remove all cache entries.

lua
Cache.flush()

Cache.gc()

Purge all expired entries. Returns the number of entries removed.

lua
local purged = Cache.gc()

Released under the MIT License.