Using Redis (Jedis) - Historical

Applied By Ricky_22: Aug 3, 2019 at 10:29 AM

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):
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):
Now, initialize the JedisPool in the onEnable() method. The JedisPool is a thread-safe method of accessing the Jedis resource.

Code (Java):
public static JedisPool pool;

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

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

public void onDisable() {
Finally, to use Jedis, do
Code (Java):
Jedis j = null;
try {
   j = pool.getResource();
   // If you want to use a password, use
   j.set("key", "value");"key"));
} finally {
   // Be sure to close it! It can and will cause memory leaks.
That's it! Now you are up and running with Jedis.