Springboot整合Jedis工具操作redis数据库

2 篇文章 0 订阅
2 篇文章 0 订阅

准备工作

  1. 让redis可以被远程连接,关闭保护模式,添加密码。
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
requirepass [这里设置自己想要的密码]

# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
# 这里设置为no
protected-mode no

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1 # 将这一行注释掉

  1. 添加依赖
<dependency>
    <groupId>redis.clients</groupId>
     <artifactId>jedis</artifactId>
     <version>3.1.0</version>
</dependency>
  1. application.yml文件配置相关参数
spring:
  redis:
    host: 127.0.0.1
    password: root
    port: 6379
    jedis:
      pool:
        max-idle: 8 #最大空闲数
        min-idle: 2 #最小空闲数
        max-active: 10 #最大连接数
    timeout: 2000 #连接超时
  1. 编写配置类
/**
 * @author Jinkang He
 * @version 1.0
 * @date 2020/5/21 17:14
 */

@Slf4j
@Configuration
public class JedisConfig {
    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.timeout}")
    private int timeout;

    @Value("${spring.redis.jedis.pool.max-active}")
    private int maxActive;

    @Value("${spring.redis.jedis.pool.max-idle}")
    private int maxIdle;

    @Value("${spring.redis.jedis.pool.min-idle}")
    private int minIdle;

    @Bean
    public JedisPool jedisPool() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMinIdle(minIdle);
        jedisPoolConfig.setMaxTotal(maxActive);

        JedisPool jedisPool = new JedisPool(jedisPoolConfig,host,port,timeout,password);
        log.info("JedisPool创建成功"+jedisPool);
        return jedisPool;
    }

}

  • 要注意需要把 jedisPool() 方法返回的对象交给spring的ioc容器管理,这样在其他地方获取连接池的时候就可以直接注入了
  1. 使用连接池获取连接进行操作
@Service
@Slf4j
public class LoginServiceImpl implements LoginService {

    private JedisPool jedisPool;

    @Autowired
    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    @Override   
    public String testRedis(String key) {
        String val = null;
        Jedis resource = jedisPool.getResource();
        if (resource.exists(key)) {
            log.info("redis中查找{}");
            val = resource.get(key);
        } else {
            String mysqlVal = "略略略";
            log.info("mysql中查找{}" , mysqlVal);
            val = mysqlVal;
            resource.set(key, val);
        }
        resource.close();
        return val;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值