1)Jedis相关的API:
/**
* 1.不使用连接池
*/
public class JedisDemo {
public static void main(String[] args) {
// 构造redis客户端
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.set("mytest", "123");
System.out.println(jedis.get("mytest"));
jedis.close(); // 关闭连接
}
}
/**
* 2.使用普通的连接池
*/
public class JedisPoolDemo {
public static void main(String[] args) {
// 连接池配置信息
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(50); // 设置最大连接数
// 构建连接池
JedisPool jedisPool = new JedisPool(jedisPoolConfig, "127.0.0.1", 6379);
// 从连接池中获取连接
Jedis jedis = jedisPool.getResource();
// 读取数据
System.out.println(jedis.get("mytest"));
// 将连接还回到连接池中
jedisPool.returnResource(jedis);
// 释放连接池
jedisPool.close();
}
}
/**
* 3.使用分片式集群的连接池
*/
public class ShardedJedisPoolDemo {
public static void main(String[] args) {
// 连接池配置信息
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(50); // 设置最大连接数
// 定义集群信息
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo("127.0.0.1", 6379));
shards.add(new JedisShardInfo("192.168.1.100", 6379));
// 构建集群连接池
ShardedJedisPool shardedJedisPool = new ShardedJedisPool(poolConfig, shards);
ShardedJedis shardedJedis = null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
String value = shardedJedis.get("mytest");
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != shardedJedis) {
// close()方法:检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
// 关闭连接池
shardedJedisPool.close();
}
}
2)实战:
【1】pom文件:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
【2】spring配置文件:
<!-- 定义分片式连接池 -->
<bean class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0">
<bean class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
</constructor-arg>
<constructor-arg index="1">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<!--
<constructor-arg index="0" value="${redis.node1.host}"/>
<constructor-arg index="1" value="${redis.node1.port}"/>
-->
<constructor-arg name="host" value="${redis.node1.host}" />
<constructor-arg name="port" value="${redis.node1.port}" />
<constructor-arg name="timeout" value="${redis.timeout}" />
<constructor-arg name="weight" value="1" />
</bean>
</list>
</constructor-arg>
</bean>
redis.properties文件:
redis.node1.host=172.0.0.1
redis.node1.port=6379
redis.database=0
redis.timeout=300
# redis.maxActive=300
redis.maxTotal=300
redis.maxIdle=300
redis.maxWait=3000
# 向调用者输出"连接"资源时,是否进行有效性检查,如果无效则从连接池中移除,并尝试获取继续获取。默认为false
redis.testOnBorrow=true
【3】实现:
/**
* 自定义回调函数
*/
public interface Function<T, E> {
public T callback(E e);
}
/**
* redis服务类
*/
@Service
public class RedisService {
@Autowired(required = false)
private ShardedJedisPool shardedJedisPool;
private <T> T execute(Function<T, ShardedJedis> fun) {
ShardedJedis shardedJedis = null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
return fun.callback(shardedJedis);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != shardedJedis) {
// close()方法:检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
return null;
}
/**
* 执行SET操作
*
* @param key
* @param value
* @return
*/
public String set(final String key, final String value) {
return this.execute(new Function<String, ShardedJedis>() {
@Override
public String callback(ShardedJedis e) {
return e.set(key, value);
}
});
}
/**
* 执行GET操作
*
* @param key
* @return
*/
public String get(final String key) {
return this.execute(new Function<String, ShardedJedis>() {
@Override
public String callback(ShardedJedis e) {
return e.get(key);
}
});
}
/**
* 删除key
*
* @param key
* @return
*/
public Long del(final String key) {
return this.execute(new Function<Long, ShardedJedis>() {
@Override
public Long callback(ShardedJedis e) {
return e.del(key);
}
});
}
/**
* 设置生存时间,单位为:秒
*
* @param key
* @param seconds
* @return
*/
public Long expire(final String key, final Integer seconds) {
return this.execute(new Function<Long, ShardedJedis>() {
@Override
public Long callback(ShardedJedis e) {
return e.expire(key, seconds);
}
});
}
/**
* 设置String类型的值,并且指定生存时间,单位为:秒
*
* @param key
* @param value
* @param seconds
* @return
*/
public String set(final String key, final String value, final Integer seconds) {
return this.execute(new Function<String, ShardedJedis>() {
@Override
public String callback(ShardedJedis e) {
String result = e.set(key, value);
// 设置生存时间
e.expire(key, seconds);
return result;
}
});
}
}
redis客户端--Jedis的使用
最新推荐文章于 2024-04-03 00:37:10 发布