Skip to content
This repository was archived by the owner on Aug 20, 2022. It is now read-only.

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. To delete the value, we could do KeyValueStore:Delete("MostRecentChatMessage")

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)

Types supported by KeyValueStore are string, number, boolean, table (json serialization), and nil

Notes:

  1. If a key does not exist, then KeyValueStore:Get will return nil
  2. Although internally we are interacting asynchronously with a database to implement the storage for this utility, eager caching is used so that all changes have the appearance of immediately taking effect. Caching is done whenever possible and multiple outstanding :Get requests for the same key are aggregated into one call to the database. In other words, the database interaction is minimized as much as possible.
Clone this wiki locally