Spring Boot 2 缓存注解 缓存工具类 缓存切换

1.配置文件

application.yml

server:
  port: 8899
  servlet:
    context-path: /

spring:
  profiles:
    active: dev
  cache:
    type: ehcache #ehcache caffeine redis
    ehcache:
      config: classpath:ehcache.xml
    cache-names: testName,captchaCache  #ehcache时cacheName由ehcache.xml定义
    caffeine:
      spec: maximumSize=500,expireAfterAccess=600s
  redis:
    host: 127.0.0.1
    port: 6379
    password:

 

ehcache.xml




    
    
    
    

2.缓存工具类 CacheUtil

package cn.flyinke.sb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import static javafx.scene.input.KeyCode.T;

/**
 * @author flyinke
 * @time 2018/12/21
 * @description
 */
@Component
public class CacheUitl {

    @Autowired
    private CacheManager cacheManager;

    private static CacheManager cm;

    @PostConstruct
    public void init(){
        cm = cacheManager;
    }

    public static void put(String cacheName,String key,Object value){
        Cache cache = cm.getCache(cacheName);
        cache.put(key,value);
    }

    public static Object get(String cacheName,String key){
        Cache cache = cm.getCache(cacheName);
        if(cache == null){
            return null;
        }
        return cache.get(key).get();
    }

    public static  T get(String cacheName,String key,Class clazz) {
        Cache cache = cm.getCache(cacheName);
        return cache.get(key, clazz);
    }

    public static void evict(String cacheName,String key){
        Cache cache = cm.getCache(cacheName);
        cache.evict(key);
    }
}

3.测试

IndexService

package cn.flyinke.sb;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @author flyinke
 * @time 2018/12/21
 * @description
 */
@Service
public class IndexService {

    @Cacheable(cacheNames = "captchaCache",key = "#p")
    public double random(String p){
        return Math.random();
    }
}

IndexController

package cn.flyinke.sb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;

/**
 * @author flyinke
 * @time 2018/12/20
 * @description
 */
@RestController
@RequestMapping("/api")
public class IndexController {

    @Autowired
    private CacheManager cacheManager;

    @Autowired
    private IndexService indexService;

    @RequestMapping("/index")
    public Object index(){

        return "index";
    }

    @RequestMapping("/cache")
    public Object cache(){
        System.out.println("cacheNames:"+cacheManager.getCacheNames());
        Cache cache = cacheManager.getCache("captchaCache");
        cache.put("key-1","value-1");
        System.out.println("cache:key-1="+cache.get("key-1",String.class));
        System.out.println("cacheNames:"+cacheManager.getCacheNames());
        return cacheManager.getCacheNames();
    }

    @RequestMapping("/cache2")
    public Object cache2(){
        CacheUitl.get("captchaCache","aaaaa",String.class);
        Map map = new HashMap<>();
        map.put("name","Tom");
        map.put("age",19);
        CacheUitl.put("captchaCache","key-2",map);

        Map re = (Map) CacheUitl.get("captchaCache","key-2");
        return re;
    }
    @RequestMapping("/cache3")
    public Object cache3(@RequestParam String p){
        return indexService.random(p);
    }
}

 

你可能感兴趣的:(java,springboot)