Adapter
The NSG-Adapter contains classes for Database (currently Redis and MongoDB).
It will also provide you credentials with the environment variables, making creating your own connection easier.
MongoDB#
MongoDB is a NoSQL database, in which you can store your data in a JSON-like format permanently.
You almost never need to initialize the MongoHandler, which only contains the database primarily used.
You can get them from the respective Initializer (e.g.: SpigotInitializer, VeloctyInitializer, or InitializerObject.initializer if unknown)
In there you can store permanent data in collections and query them, refer to The Quick Reference for documentation.
Redis#
Redis is a key-value store, which is used to store data for the API.
Currently we use it to cache data temporarily from the database.
You can initialize both with the RedisClient in the initializer or using the RedisConnectionConfig (depends on environment variables on the machine)
There are 2 ways to use Redis (currently):
1. Cache#
As said before, we use Redis to cache data temporarily from the database.
You can use the CachedRedisAdapter
to cache data, which other services can use.
It also has support for local caching, which can be toggled by the developer (recommended to use if you do multiple request every second).
If local caching is enabled, the data will be updated by using pub subs, which will be explained in the next section.
2. PubSub#
Pub Subs are like a PluginMessaging system in minecraft, you can announce a message by using key-value pairs.
The keys are channels, and the values are the messages.
Patterned channels are supported using wildcards.
You can use the PubSubConnector
class to create your own listener and publisher.
Examples:
// Bsp.: Proxyval pubSub = PubSubConnector(velocityInitializer.redisClient)pubSub.subscribe("testing") { channel, message -> println("$channel: $message")}pubSub.publish("testing", "Hello World")// Bsp.: Server + Gsondata class ExampleObject(val uuid: UUID, val name: String, val age: Int)val gson = GsonBuilder().disableHtmlEscaping().create()val pubSub = PubSubConnector(SpigotInitializer.instance.redisClient)pubSub.subscribe("testing") { channel, message -> val exampleObject = gson.fromJson(message, ExampleObject::class.java) println("$channel: ${exampleObject.toString()}")}pubSub.publish("testing", gson.toJson(ExampleObject(UUID.randomUUID(), "Test", 20)))