栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot如何快速的使用redis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot如何快速的使用redis

package com.xxxx.core.redis;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Component
@Data
public class RedisConfiguration {

    @Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private int port;
    @Value("${redis.maxTotal}")
    private int maxTotal;
    @Value("${redis.password}")
    private String password;
    @Value("${redis.timeout}")
    private int timeout;
    @Value("${redis.maxIdle}")
    private int maxIdle;
    @Value("${redis.maxWaitMillis}")
    private int maxWaitMillis;
    @Value("${redis.enabled}")
    private Boolean enabled;



    public JedisPoolConfig jedisPoolConfig(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(maxTotal);
        return jedisPoolConfig;
    }
    @Bean
    public JedisPool jedisPool(){
        JedisPoolConfig jedisPoolConfig  = jedisPoolConfig();
        if(password==null||password.length()==0){
            return new JedisPool(jedisPoolConfig,host,port,timeout);
        }else{
            return new JedisPool(jedisPoolConfig,host,port,timeout,password);
        }

    }


}

package com.xxxx.core.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
@Slf4j
public class RedisUtil{

    @Autowired
    private RedisConfiguration redisConfiguration;

    private JedisPool getJedisPool(){
        return redisConfiguration.jedisPool();
    }
    
    public String get(String key){
        if(!redisConfiguration.getEnabled()){return null;}
        String value = null;
        Jedis jedis = null;
       try{
           jedis = getJedisPool().getResource();
           value =  jedis.get(key);
       }catch (Exception e){
           log.error(e.getMessage());
       }finally {
           returnResource(jedis);
       }
       return value;
    }

    
    public String set(String key, String value) {
        if(!redisConfiguration.getEnabled()){return null;}
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return jedis.set(key, value);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "0";
        } finally {
            returnResource(jedis);
        }
    }

    
    public static void returnResource(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }



}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678635.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号