spring boot中使用redis -Jedis

  • Post author:
  • Post category:其他


1、加入jar包依赖 使用maven形式

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-redis</artifactId>
   <version>1.4.7.RELEASE</version>
</dependency>

2、项目配置文件设置redis信息

redis:
 host: 
 port: 
 password: 
 pool:
  max-active: 8
  min-idle: 0
  max-idle: 8
  max-wait: 10000
 timeout: 0
database: 9

注意:一定要在spring节点下

3、新建初始化 JedisPool的bean 让spring boot启动加载该bean的配置信息

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

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

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

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

    @Value("${spring.redis.pool.max-wait}")
    private long maxWaitMillis;

    @Value("${spring.redis.password}")
    private String password;
    @Bean
    public JedisPool redisPoolFactory() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
        return jedisPool;
    }
}

4、新建  JedisUtil类

public class JedisUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;

    private static JedisPool jedisPool = null;

    private static volatile Jedis jedis = null;
    public JedisUtil(){}

    public static Jedis getJedis(){
        if (jedis ==null){
            synchronized (Jedis.class){
                if (jedis ==null){
                    jedis = getJedisPool().getResource();
                }
            }
        }
        return jedis;
    }

    public static JedisPool getJedisPool(){
        if (jedisPool ==null){
            synchronized (JedisPool.class){
                if (jedisPool==null){
                    jedisPool = applicationContext.getBean(JedisPool.class);
                }
            }
        }
        return jedisPool;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(JedisUtil.applicationContext == null){
            JedisUtil.applicationContext  = applicationContext; //初始化 spring applicationContext
        }
    }

    /**
     * 根据key查看是否存在
     * @param key
     * @return
     */
    public static boolean hasKey(String key){
        return getJedis().exists(key);
    }

    /**
     * 设置key -value 形式数据
     * @param key
     * @param value
     * @return
     */
    public static String set(String key,String value){
        String result =  getJedis().set(key,value);
        return result;
    }

    /**
     * 设置 一个过期时间
     * @param key
     * @param value
     * @param timeOut 单位秒
     * @return
     */
    public static String set(String key,String value,int timeOut){
        return getJedis().setex(key,timeOut,value);
    }

    /**
     * 根据key获取value
     * @param key
     * @return
     */
    public static String getByKey(String key){
        return getJedis().get(key);
    }

    /**
     * 根据通配符获取所有匹配的key
     * @param pattern
     * @return
     */
    public static Set<String> getKesByPattern(String pattern){
        return getJedis().keys(pattern);
    }

    /**
     * 根据key删除
     * @param key
     */
    public static void delByKey(String key){
        getJedis().del(key);
    }

    /**
     * 根据key获取过期时间
     * @param key
     * @return
     */
    public static long getTimeOutByKey(String key){
        return getJedis().ttl(key);
    }

    /**
     * 清空数据 【慎用啊!】
     */
    public static void flushDB(){
        getJedis().flushDB();
    }

    /**
     * 刷新过期时间
     * @param key
     * @param timeOut
     * @return
     */
    public static long refreshLiveTime(String key,int timeOut){
        return getJedis().expire(key,timeOut);
    }
}

注意有用这里没有使用注解将该类注入到spring中去 而是使用工具类的形式 这里需要 implements  ApplicationContextAware接口 然后重写setApplicationContext方法初始化 applicationContext ,当时用某bean时不是使用@Autowired等注解注入,而是需要手动使用getBean获取所需的bean对象,最后在spring boot启动类中加入

@Import({YmlConfigUtil.class, JedisUtil.class})



版权声明:本文为xiao______xin原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。