Using Redis (Jedis)

Oct 3, 2016
Using Redis (Jedis)
  • What is Redis?

    Redis is a key-value store, so essentially a database based on hashmaps (but it supports many other data types). It's great for scoreboards, stats, user accounts, and more. BuildAPrefix is my primary example of the powers of Redis.

    So how do I use it?

    Glad you asked! I'm going to be using Apache Maven to handle my dependencies, but you can also just add Jedis to your build path. A great resource is the GitHub page. Please note some of the code used here is from my BuildAPrefix plugin (which is copyrighted), but I am giving you permission to use the provided code.

    Add Jedis as a dependency. It's pretty straightforward. No repo needed. You will also need to add the Apache Commons Pool2 for Jedis to work properly.

    Code (HTML5):
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.8.1</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.4.2</version>
    </dependency>
    Then, shade the Jedis code into your project. This is important because if you don't shade it, it will not be available during runtime and you will get errors.

    Code (HTML5):
    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <relocations>
                                    <relocation>
                                        <pattern>redis.clients.jedis</pattern>
                                        <shadedPattern>your.package.here.shaded.redis.clients.jedis</shadedPattern>
                                        <pattern>org.apache.commons</pattern>
                                        <shadedPattern>your.package.here.shaded.org.apache.commons</shadedPattern>
                                    </relocation>
                                </relocations>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    Now, initialize the JedisPool in the onEnable() method. The JedisPool is a thread-safe method of accessing the Jedis resource.

    Code (Java):
    private JedisPool pool;

    @Override
    public void onEnable() {
        /*
         * THANKS TO @Tux for the ClassLoader wizardry.
         */

        ClassLoader previous = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(RedisDatabase.class.getClassLoader());
        pool = new JedisPool("ip", 123 /* Port */);
        Thread.currentThread().setContextClassLoader(previous);
    }

    @Override
    public void onDisable() {
        pool.close();
    }
    Finally, to use Jedis, do
    Code (Java):
    // Try-with-resources will handle calling #close() for us
    try (Jedis jedis = pool.getResource()) {
       // If you want to use a password, use
       jedis.auth("some-secure-password");
       jedis.set("key", "value");
       getLogger().info(jedis.get("key"));
    }
    That's it! Now you are up and running with Jedis.
  • Loading...
  • Loading...