This repository was archived by the owner on Aug 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
KeyValueStore
rdev34 edited this page Aug 19, 2020
·
13 revisions
The KeyValueStore
singleton class is a server-side key-value storage utility with persistence, meaning that the values are kept between server executions in the database.
In this example, every time a player sends a message we store it using KeyValueStore:Set
.
To retrieve the value once it has been stored, we use KeyValueStore:Get
and supply a function that will be called with the value
Events:Subscribe("PlayerChat", function(args)
KeyValueStore:Set("MostRecentChatMessage", args.text)
KeyValueStore:Get("MostRecentChatMessage", function(value)
print("The most recent chat message is:", value)
end)
end)
Notes:
- If a key does not exist, then
KeyValueStore:Get
will returnnil
- Although internally we are interacting asynchronously with a database to implement the storage for this utility, caching is used so that all changes have the appearance of immediately taking effect. Caching is done whenever possible & multiple outstanding :Get requests for the same key are aggregated into one call to the database. In other words, the database usage is highly optimized and minimized.