# 8.2 RedisPlugin

# 1. Basic Configuration

RedisPlugin exists as a plugin for JFinal. Therefore, it needs to be configured in JFinalConfig when used. Below is the sample code for configuring RedisPlugin:

public class DemoConfig extends JFinalConfig {
  public void configPlugin(Plugins me) {
    // Redis service used for caching the bbs module
    RedisPlugin bbsRedis = new RedisPlugin("bbs", "localhost");
    me.add(bbsRedis);
 
    // Redis service used for caching the news module
    RedisPlugin newsRedis = new RedisPlugin("news", "192.168.3.9");
    me.add(newsRedis);
  }
}
1
2
3
4
5
6
7
8
9
10
11

The above code creates two RedisPlugin objects, namely bbsRedis and newsRedis. The first created RedisPlugin object will become the primary cache object, which can be directly accessed through Redis.use(). Otherwise, a cacheName parameter is required, such as Redis.use("news").

# 2. Maven Dependencies

To use RedisPlugin, add the following Maven dependencies:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.6.3</version>
</dependency>
 
<dependency>
    <groupId>de.ruedigermoeller</groupId>
    <artifactId>fst</artifactId>
    <version>2.57</version><!-- Note: Higher versions do not support JDK 8 -->
</dependency>
1
2
3
4
5
6
7
8
9
10
11

# 3. Remote Connection

If the Redis server is on a remote machine, you'll need to modify some settings in the /etc/redis.conf configuration file:

# Change the existing bind value from 127.0.0.1 to 0.0.0.0
bind 0.0.0.0
 
# Add requirepass configuration to set a password
requirepass your-connection-password-here
1
2
3
4
5

After making these changes, don't forget to restart Redis:

# For CentOS, the restart command is as follows
service redis restart
1
2

If you are using a cloud server, make sure to open the corresponding port; the default port for Redis is 6379.

Finally, when creating a RedisPlugin for remote connection, you must provide the appropriate password:

RedisPlugin rp = new RedisPlugin("main", "xxx.com", 6379, 10000, "your-password-here");
me.add(rp);
1
2

The last parameter in the above RedisPlugin is the password configured in redis.conf. More parameters are supported, such as the database:

RedisPlugin rp = new RedisPlugin("main", "xxx.com", 6379, 10000, "your-password-here", database);
me.add(rp);
1
2

Choose parameters as needed.

Last Updated: 9/22/2023, 7:03:51 AM