崛起於Springboot2.X + Redis單機(15)

《SpringBoot2.X心法總綱》redis

一、pom添加依賴

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

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

<dependency>
   <groupId>commons-lang</groupId>
   <artifactId>commons-lang</artifactId>
   <version>2.5</version>
</dependency>
<dependency>
   <groupId>com.google.collections</groupId>
   <artifactId>google-collections</artifactId>
   <version>1.0</version>
</dependency>

    這四個依賴的做用,第一個是集成redis,第二個集成jedis,第三個和第四個是封裝jedis工具類的實現,之後咱們能夠直接調用操做redis就好,很是方便,大多數公司開發用的就是這種方案。spring

二、配置文件redis

    記住timeout和wait後面要加ms,這是springboot2.0的特性,否則會出現紅色標記錯誤,不和之前1.X同樣只寫一個數字就能夠了springboot

spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.timeout=10000ms

spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1ms
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.pool.max-idle=8

三、配置類config

@Configuration
@EnableCaching
public class RedisCacheConfiguration extends CachingConfigurerSupport {

    Logger logger = LoggerFactory.getLogger(RedisCacheConfiguration.class);

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

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

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

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

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

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

    @Bean
    public JedisPool redisPoolFactory() {
        
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        
        jedisPoolConfig.setMaxIdle(maxIdle);

        jedisPoolConfig.setMaxWaitMillis(Long.valueOf(maxWaitMillis.substring(0,maxWaitMillis.length()-2)));

        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, Integer.valueOf(timeout.substring(0,timeout.length()-2)));

        return jedisPool;
    }
}

四、jedis的service的封裝

public interface ICacheService {

 /**
 * 設置鍵值
 * @param key
 * @param value
 * @return
 */
String set(String key, String value);

/**
 *
 * @param key
 * @param increament
 * @return
 */
Long incrBy(String key, long increament);

}
@Component
public class CacheSingleService implements ICacheService {

    @Autowired
    private JedisPool jedisPool;

  public String set(String key, String value){
    Jedis jedis = null;
    try {
        jedis = getResource();
        return jedis.set(key, value);
    } finally {
        returnResource(jedis);
    }
}

 public Long incrBy(String key, long increament) {
    Long i = null;
    Jedis jedis = null;
    try {
        jedis = getResource();
        i = jedis.incrBy(key, increament);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        returnResource(jedis);
    }
    return i;
       }
  }
}
@Component
public class CacheService {

    private static final String PLATFORM_KEY_PRE = "DTB_";

    @Autowired
    private CacheSingleService cacheSingleService;

    private ICacheService cacheService;

    @PostConstruct
    private void init(){
        this.cacheService = cacheSingleService;
    }

    protected String getStrKey(String key){
        String result = PLATFORM_KEY_PRE+key;
        return result;

    }

 public void set(String key, String value) throws Exception{
    key = getStrKey(key);
    cacheService.set(key, value);
 }
 public Long incrBy(String key, long increament){
    key = getStrKey(key);
    return cacheService.incrBy(key, increament);
 }

}

並無把全部的servie裏面設計redis的全部方法寫出來,寫出兩個大概步驟就行了app

五、測試

寫controller,之後咱們直接調用CacheService就行了,直接使用redis的方法,由於咱們剛剛在service中封裝了設計redis的全部方法,上面是兩個,可是公司中會封裝幾十個spring-boot

@Autowired
CacheService cacheService;

@RequestMapping(value = "/redis/test2")
public String test2() throws Exception {
    cacheService.set("mjt01","測試jedis");

    return cacheService.get("mjt01");
}

測試成功!工具

相關文章
相關標籤/搜索