public class JedisDemo {
//方式一
@Test
public void test1(){
//设置redis 主机ip和端口
Jedis jedis = new Jedis("127.0.0.1",6379);
//保存数据
jedis.set("name", "jedis");
//获取数据
String name = jedis.get("name");
System.out.println(name);
//释放资源
jedis.close();
}
//方式二
@Test
public void test2(){
//获取连接池的配置对象
JedisPoolConfig config = new JedisPoolConfig();
//设置最大连接数
config.setMaxIdle(30);
//设置最大空闲连接数
config.setMaxIdle(10);
//获取连接池
JedisPool jedisPool = new JedisPool(config,"127.0.0.1",6379);
//获取核心对象
Jedis jedis = null;
try{
//通过连接池获取连接
jedis = jedisPool.getResource();
//设置数据
jedis.set("name", "jedis");
//获取数据
String name = jedis.get("name");
System.out.println(name);
}catch (Exception e) {
e.printStackTrace();
}finally {
//释放资源
if(jedis!=null){
jedis.close();
}
if(jedisPool!=null){
jedisPool.close();
}
}
}
}
相关文章
- Linux使用Redis客户端连接Redis
- C++使用hiredis连接带密码的redis服务
- spring-data-redis用配置类连接时,抛异常Cannot get Jedis connection; nested exception is
- SpringBoot+Redisson连接使用Docker部署的Redis集群
- Jedis直接使用Redis原生命令操作Redis,解决某些命令没有封装
- SpringBoot使用Hutool进行最简单的连接Redis进行缓存操作
- 七. Redis 当中 Jedis 的详细刨析与使用
- 如何使用redis设计关系数据库
- Python 实现的风控系统(使用了kafka、Faust、模拟drools、redis、分布式数据库)
- springboot开发网站-使用redis数据库定时特征限制指定ip的访问次数