5

Redis--Jedis操作Redis

 2 years ago
source link: http://blog.linrty.com/2021/07/20/Redis--Jedis%E6%93%8D%E4%BD%9CRedis/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Redis Jedis测试

Jedis所需要的jar包

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>

连接Redis注意事项

禁用Linux的防火墙

Linux(CentOS7)里执行命令:

systemctl stop/disable firewalld.service

Ubuntu中

sudo ufw disable

redis.conf中注释掉bind 127.0.0.1 ,然后 protected-mode no

Jedis常用操作

image-20210720175413140

Jedis连接Redis连接池,可直接用

使用的是双重检验的单例模式

JedisPoolUtil.java

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisPoolUtil {
private static volatile JedisPool jedisPool = null;

private JedisPoolUtil() {
}

public static JedisPool getJedisPoolInstance() {
if (null == jedisPool) {
synchronized (JedisPoolUtil.class) {
if (null == jedisPool) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(200);
poolConfig.setMaxIdle(32);
poolConfig.setMaxWaitMillis(100*1000);
poolConfig.setBlockWhenExhausted(true);
poolConfig.setTestOnBorrow(true); // ping PONG

jedisPool = new JedisPool(poolConfig, "xxx.xxx.xxx.xxx", yourport, timeout:60000 );
}
}
}
return jedisPool;
}

public static void release(JedisPool jedisPool, Jedis jedis) {
if (null != jedis) {
jedisPool.returnResource(jedis);
}
}

}
本文作者: linrty
本文链接: http://blog.linrty.com/2021/07/20/Redis--Jedis%E6%93%8D%E4%BD%9CRedis/
版权声明: 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。转载请注明出处!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK