Skip to content

Standalone usage

Jiří Apjár edited this page Aug 22, 2022 · 2 revisions

Standalone usage

You can use the ForestRedisAPI as a standalone library. Then you need to initialize RedisManager yourself and provide it with required data.

Why shall (not) use standalone

If you want to make your plugin independent on any other plugins, this can be useful. It's also helpful if you want to provide multiple-connections support.

The main benefit of not using standalone version is that the API can be used by many plugins without any additional overhead.

Example plugin main class

import cz.foresttech.forestredis.shared.RedisManager;
import org.bukkit.plugin.java.JavaPlugin;

public class MyExamplePlugin extends JavaPlugin {

    private RedisManager redisManager;
    
    @Override
    public void onEnable() {
        // ...
        loadRedis();
        // ...
    }

    @Override
    public void onDisable() {
        //...
        // Close the RedisManager
        if (redisManager != null) {
            redisManager.close();
        }
        //...
    }

    public void loadRedis() {
        // Construct RedisConfiguration object
        RedisConfiguration redisConfiguration = new RedisConfiguration(
                "localhost", //hostname
                6379, //port
                null, //username (null if not any)
                null, //password (null if not any)
                false //ssl
        );

        // Initialize RedisManager instance (singleton)
        // Since init, use RedisManager#getAPI() to obtain the instance
        redisManager = new RedisManager(this, "MyServer", redisConfiguration);
        
        // Now setup the connection
        redisManager.setup(/*channels*/);

        // Now you can use #getAPI() call to get singleton instance
        redisManager.subscribe("MyChannel1");
    }

    public void reloadRedis() {
        // Just call reload function on the RedisManager object.
        // If you set something to "null", the already existing values are used.
        // In this case, the redis configuration is kept.
        redisManager.reload("MyNewServerName", null, true);
    }
}
Clone this wiki locally