2024-11-01 11:11:21 +08:00

41 lines
1.2 KiB
Java

package com.yimaogo.demojet.controller;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.yimaogo.demojet.cache.CacheService;
import com.yimaogo.demojet.model.UserDO;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
@RestController
public class TestController {
@Resource
private CacheService cacheService;
@Autowired
private Cache<Long, Object> userCache;
@GetMapping("/test")
public String test() {
userCache.PUT(1212L, "fadfasdfas");
return "hello";
}
@GetMapping("getRemote")
@Cached(name="userCache:", key = "#id", expire = 3600, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.REMOTE)
public UserDO getRemote(Long id){
// 直接新建用户,模拟从数据库获取数据
UserDO user = new UserDO();
user.setName("用户remote"+id);
System.out.println("第一次获取数据,未走缓存:"+id);
return user;
}
}