spring boot 配置redis

Posted 这很周锐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot 配置redis相关的知识,希望对你有一定的参考价值。

导入pom文件

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>

1、redis 连接 端口配置

 

 

 2、使用的是redistemplate 加载redis,写了个加载配置类并且 让DefaultCacheService注入了缓存服务

package com.fyun.tewebcore.config;

import com.fyun.common.utils.CacheService;
import com.fyun.common.utils.impl.DefaultCacheService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author zhourui
 * @create 2020/1/7
 */
@Configuration
@PropertySource(value = {"classpath:spring/redis-${spring.profiles.active}.properties"})
public class RedisConfig {
    @Value("${redis.database}")
    private Integer database;
    @Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private Integer port;
    @Value("${redis.timeout}")
    private Integer timeout;
    //注入刚才的cacheservice
    @Bean
    public CacheService cacheService() {
        //注入spring
        return new DefaultCacheService(getredisTemplate());
    }
    @Bean
    public RedisTemplate getredisTemplate(){
        RedisTemplate redisTemplate = new RedisTemplate();
        //redis设置工厂配置
        redisTemplate.setConnectionFactory(this.redisConnection());
        return redisTemplate;
    }
    @Bean
    public RedisConnectionFactory redisConnection(){
        JedisConnectionFactory jedisFactory = new JedisConnectionFactory();
        jedisFactory.setDatabase(database);
        jedisFactory.setHostName(host);
        jedisFactory.setPort(port);
        jedisFactory.setTimeout(timeout);
        return jedisFactory;
    }
}

3、缓存接口 :操作redis 的服务接口

package com.fyun.common.utils;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * 缓存服务
 */
public interface CacheService {

    void setKey(String key, Serializable obj);

    void setKey(String key, Serializable obj, long expireSecond);

    void setKey(String key, Serializable obj, long expire, TimeUnit unit);

    <T> T getKey(String key);

    boolean hasKey(String key);

    void delKey(String key);
}

4、缓存实现

package com.fyun.common.utils.impl;
        import com.fyun.common.utils.CacheService;
        import org.springframework.data.redis.core.RedisTemplate;
        import org.springframework.stereotype.Component;

        import java.io.Serializable;
        import java.util.concurrent.TimeUnit;
@Component
public class DefaultCacheService implements CacheService {

    private RedisTemplate redisTemplate;

    public DefaultCacheService(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public void setKey(String key, Serializable obj) {
        redisTemplate.opsForValue().set(key, obj);
    }

    public void setKey(String key, Serializable obj, long expireSecond) {
        setKey(key, obj, expireSecond, TimeUnit.SECONDS);
    }

    public void setKey(String key, Serializable obj, long expire, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, obj, expire, unit);
    }

    public <T> T getKey(String key) {
        return (T) redisTemplate.opsForValue().get(key);
    }

    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    public void delKey(String key) {
        redisTemplate.delete(key);
    }
}

搞定!!!

以上是关于spring boot 配置redis的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot配置Redis工具类

聊聊spring-boot-starter-data-redis的配置变更

Spring Boot配置redis集群

spring boot配置信息详解

spring-boot-starter-redis配置详解

【原创】Springboot Redis配置总结( 基于spring-boot-data-redis-stater )