package com.zhaoyang.controller;
import com.zhaoyang.domain.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
//注意:RedisConfig2注销不启动
//使用多级缓存架构,通过增加一层代理层来解决。
@RestController
public class RC3 {
????@Autowired
????private RedisTemplate<String, Object> redisTemplate;
????@Autowired
????private CacheManager ehCacheManager;
????@GetMapping("/person3/{id}")
????@Cacheable(value="userCache", key="#id")
????public Person getUserById(@PathVariable String id){
????????id = "Cache::" + id;
//先从Ehcache缓存中获取
????????Person person = (Person) ehCacheManager.getCache("userCache").get(id);
????????if(person == null){
//再从Redis缓存中获取
????????????person = (Person) redisTemplate.opsForValue().get(id);
????????????if(person != null){
????????????????ehCacheManager.getCache("userCache").put(id, person);
????????????}
????????}
????????return person;
????}
}