Cache
Cache class for storing data in a cache instance.
Based off node-cache.
Install with wally by adding the following to your wally.toml:
Cache = "dig1t/cache@1.0.10"
Types
CacheOptions
interface CacheOptions {defaultTTL?: number--
The default time-to-live for a cache entry. Defaults to 6000.
checkInterval?: number--
The interval in seconds to check for expired cache entries. Defaults to 600.
maxKeys?: number--
The maximum amount of keys to store in the cache. 0 for unlimited. Defaults to 0.
}CacheEntry
interface CacheEntry {key: Key--
The key of the cache entry.
value: any--
The value of the cache entry.
expires: number--
The unix timestamp of when the cache entry expires.
}Functions
new
Creates a new Cache instance.
Set
Cache:Set(key: string,--
The key to set.
value: any,--
The value to set.
ttl: number?--
The time-to-live for the cache entry. Defaults to the defaultTTL option. 0 for unlimited.
) → boolean--
Whether or not the value was set.
Store a key:
Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
local obj = { my = "Special", variable = 42 }
local success = myCache:Set( "myKey", obj, 10000 )
-- true
Note: If the key expires based on it's
ttlit will be deleted entirely from the internal data object.
SetMultiple
Cache:SetMultiple(cacheObjects: {CacheObject}--
The key to set.
) → boolean--
Whether or not the value was set.
Store multiple keys:
Sets multiple key value pairs. It is possible to define a ttl (in seconds). Returns true on success.
local obj = { my = "Special", variable = 42 }
local obj2 = { my = "other special", variable = 1337 }
local success = myCache:SetMultiple({
{ key = "myKey", value = obj, ttl = 10000 },
{ key = "myKey2", value = obj2 }
})
-- true
Get
Retrieve a key:
Gets a saved value from the cache. Returns nil if not found or expired. If the value was found it returns the value.
local value = myCache:Get("myKey")
Take
Take a key:
Get the cached value and remove the key from the cache.
Equivalent to calling get(key) + delete(key).
Useful for implementing single use mechanism, where once a value is read it will become obsolete.
myCache:Set("myKey", "myValue", 10000)
myCache:Has("myKey") -- true
local value = myCache:Take("myKey") -- "myValue"
myCache:Has("myKey") -- false
GetMultiple
Cache:GetMultiple(keys: {Key}--
The keys to retrieve.
) → {[Key]: any}--
The values of the keys.
Retrieve multiple keys:
Gets multiple saved values from the cache. Returns an empty table {} if not found or expired.
If the values were found it returns a table with the key value pair(s).
local values = myCache:GetMultiple({ "myKey", "myKey2" })
Delete
Cache:Delete(key: Key--
The key to delete.
) → number--
The number of deleted entries.
Deletes a key. Returns the number of deleted entries. A delete will not fail if the key is not existing.
local deleted: number = myCache:Delete("myKey")
MultipleDelete
Cache:MultipleDelete(keys: {Key}--
The keys to delete.
) → number--
The number of deleted entries.
Deletes multiple keys. Returns the number of deleted entries. This will not fail if a key is not existing.
local deleted: number = myCache:MultipleDelete({ "myKey", "myKey2" })
TTL
Cache:TTL(key: string,--
The key to update.
ttl: number--
The time-to-live for the cache entry.
) → boolean--
Whether or not the ttl was updated.
Updates the ttl of a key. Returns true on success.
local success = myCache:TTL("myKey", 10000)
GetTTL
Cache:GetTTL(key: string--
The key to retrieve.
) → number?--
The time-to-live for the cache entry.
Returns the ttl of a key. Returns nil if not found or expired.
local ttl = myCache:GetTTL("myKey")
Keys
Cache:Keys() → {Key}--
The keys in the cache.
Returns all keys in the cache.
local keys = myCache:Keys()
Has
Cache:Has(key: string--
The key to check.
) → boolean--
Whether or not the key exists.
Returns a boolean indicating whether an element with the specified key exists or not.
myCache:Has("myKey") -- true
Clear
Cache:Clear() → ()Clears the cache.
Destroy
CleanupCache:Destroy() → ()Deletes all keys and values from the cache.
Used for cleanup.
myCache:Destroy()