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.
Code (HTML5):<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
Code (HTML5):<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.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.internal.jedis
</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Code (Java):public static JedisPool pool;
@Override
public void onEnable() {
pool = new JedisPool("some-fancy-ip", "port-as-string");
}
@Override
public void onDisable() {
pool.close();
}
Code (Java):Jedis j = null;
try {
j = pool.getResource();
// If you want to use a password, use
j.auth("some-secure-password");
j.set("key", "value");
getLogger.info(j.get("key"));
} finally {
// Be sure to close it! It can and will cause memory leaks.
j.close();
} - Loading...
- Loading...
XenCarta PRO
© Jason Axelrod from 8WAYRUN.COM