1. 程式人生 > >(三) springboot的本地redis簡要配置及使用案例

(三) springboot的本地redis簡要配置及使用案例

基於springboot開發中,對於快取資料獲取,重複或者延遲資料等,redis的使用就顯得方便很多。現在簡單的做本地redis的搭建。

(1) 下載window版本的redis安裝 (見我的資源下載)

 配置:port根據要求,可改變,啟動redis-service.exe即啟動,啟動redis-cli.exe可進行介面語句操作,查詢

(2) sprigboot配置

application.properties配置:

# Redis資料庫索引(預設為0)
spring.redis.database=0
## Redis伺服器地址
spring.redis.host=127.0.0.1
## Redis伺服器連線埠
spring.redis.port=6379
## Redis伺服器連線密碼(預設為空)
spring.redis.password=
## 連線池最大連線數(使用負值表示沒有限制)
spring.redis.jedis.pool.max-active=8
## 連線池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.jedis.pool.max-wait=-1ms
## 連線池中的最大空閒連線
spring.redis.jedis.pool.max-idle=8
## 連線池中的最小空閒連線
spring.redis.jedis.pool.min-idle=0
## 連線超時時間(毫秒)
spring.redis.jedis.timeout=0

 (3) 增加redis工具類RedisCacheUtil和泛型介面RedisCacheServer

   RedisCacheServer類:

package com.study.provider.cache;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

public interface RedisCacheServer {
    <T> void put(String key, T obj);
    <T> void put(String key, T obj, int timeout);
    <T> void put(String key, T obj, int timeout, TimeUnit unit);

    <T> T get(String key, Class<T> cls);

    <E,T extends Collection<E>> T get(String key, Class<E> cls, Class<T> collectionCls);
    <T> T putIfAbsent(String key, Class<T> cls, Supplier<T> supplier);
    <T> T putIfAbsent(String key, Class<T> cls, Supplier<T> supplier, int timeout);

    <E,T extends Collection<E>> T putIfAbsent(String key, Class<E> cls, Class<T> collectionCls, Supplier<T> supplier);

    boolean exists(String key);
    void del(String key);
    boolean expire(String key, long timeout, TimeUnit unit);
    boolean expire(String key, long timeout);

    void put(String key, String value);
    void put(String key, String value, int timeout);
    void put(String key, String value, int timeout, TimeUnit unit);
    String get(String key);

    void putHash(String key, Map<Object, Object> m);
    Map<Object, Object> getHash(String key);
}

工具類RedisCacheUtil:

package com.study.provider.cache;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

@Service
public class RedisCacheUtil implements RedisCacheServer{
    @Autowired
    private StringRedisTemplate redisTemplate;

    public <T> void put(String key, T obj) {
        redisTemplate.opsForValue().set(key, JSON.toJSONString(obj));
    }

    public <T> void put(String key, T obj, int timeout) {
        put(key,obj,timeout, TimeUnit.MINUTES);
    }

    public <T> void put(String key, T obj, int timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, JSON.toJSONString(obj),timeout,unit);
    }

    public <T> T get(String key, Class<T> cls) {
        return JSON.parseObject(JSON.toJSONString(redisTemplate.opsForValue().get(key)), cls);
    }

    @Override
    public <E, T extends Collection<E>> T get(String key, Class<E> cls, Class<T> collectionCls) {
        return null;
    }

    /***
    public <E, T extends Collection<E>> T get(String key, Class<E> cls, Class<T> collectionCls) {
        return JSON.parseArray(JSON.toJSONString(redisTemplate.opsForValue().get(key)), cls, collectionCls);
    }**/

    public <T> T putIfAbsent(String key, Class<T> cls, Supplier<T> supplier) {
        T t=get(key,cls);
        if(null==t){
            t=supplier.get();
            if(null!=t)
                put(key,t);
        }
        return t;
    }

    public <T> T putIfAbsent(String key, Class<T> cls, Supplier<T> supplier, int timeout) {
        T t=get(key,cls);
        if(null==t){
            t=supplier.get();
            if(null!=t)
                put(key,t,timeout);
        }
        return t;
    }

    public <E, T extends Collection<E>> T putIfAbsent(String key, Class<E> cls, Class<T> collectionCls,
                                                      Supplier<T> supplier) {
        T t=get(key,cls,collectionCls);
        if(null==t || t.isEmpty()){
            t=supplier.get();
            if(null!=t && t.size()>0)
                put(key,t);
        }
        return t;
    }

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

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

    public boolean expire(String key, long timeout, TimeUnit unit) {
        return redisTemplate.expire(key, timeout, unit);
    }

    public boolean expire(String key, long timeout) {
        return redisTemplate.expire(key, timeout, TimeUnit.MINUTES);
    }

    public void put(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public void put(String key, String value, int timeout) {
        put(key,value,timeout,TimeUnit.MINUTES);
    }

    public void put(String key, String value, int timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    public String get(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }

    public void putHash(String key, Map<Object,Object> m) {
        redisTemplate.opsForHash().putAll(key, m);
    }

    public Map<Object, Object> getHash(String key) {
        try{
            return redisTemplate.opsForHash().entries(key);
        }catch(Exception e){
            return null;
        }
    }
}

(4) 使用案例:

package com.study.provider.controller;

import com.study.provider.cache.RedisCacheUtil;
import com.study.provider.mapping.IDesignMapping;
import com.study.provider.util.ResultUtils;
import com.study.provider.vo.DesignVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class ProviderController {
     @Autowired
     private IDesignMapping designMapping;
    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @RequestMapping("/provider")
    @ResponseBody
    public Map<String, Object> provider(){
        System.out.println("welcome to provider");
        DesignVo arg0 = new DesignVo();
        arg0.setDesignDesc("xiaoxin");
        arg0.setDesignMethod("xiaoxin is a good boy");
        arg0.setDesignType("xiaoxin is a love boy");
        redisCacheUtil.put("testkey2",arg0);
        //designMapping.saveDesignModel(arg0)
        return ResultUtils.getSuccessResultData();
    }
}

(5) window可檢視