Java配置
@Configuration
@EnableCaching
public class CacheConfig {
private static final int DEFAULT_MAXSIZE = 1000;
private static final int DEFAULT_TTL = 3600;
public enum Caches {
user(60, 2),
info(5),
role;
Caches() {
}
Caches(int ttl) {
this.ttl = ttl;
}
Caches(int ttl, int maxSize) {
this.ttl = ttl;
this.maxSize = maxSize;
}
private int maxSize = DEFAULT_MAXSIZE; //最大數量
private int ttl = DEFAULT_TTL; //过期时间(秒)
public int getMaxSize() {
return maxSize;
}
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
public int getTtl() {
return ttl;
}
public void setTtl(int ttl) {
this.ttl = ttl;
}
}
@Bean
public CacheManager cacheManager() {
SimpleCacheManager manager = new SimpleCacheManager();
//把各个cache注册到cacheManager中,CaffeineCache实现了org.springframework.cache.Cache接口
ArrayList caches = new ArrayList<>();
for (Caches c : Caches.values()) {
caches.add(new CaffeineCache(c.name(),
Caffeine.newBuilder().recordStats()
.expireAfterWrite(c.getTtl(), TimeUnit.SECONDS)
.maximumSize(c.getMaxSize())
.build())
);
}
manager.setCaches(caches);
return manager;
}
}
service
public interface UserService {
List list();
User findUserById(Long id);
User findInfoById(Long id);
User update(User user);
void remove(Long id);
}
impl
@Service
@Slf4j
public class UserServiceImpl implements UserService {
private Map userMap = new HashMap<>();
private Map infoMap = new HashMap<>();
public UserServiceImpl() {
userMap.put(1L, new User(1L, "aaa", "666666"));
userMap.put(2L, new User(2L, "bbb", "666666"));
userMap.put(3L, new User(3L, "ccc", "666666"));
infoMap.put(1L, new Info("18559198715", "福州市"));
}
@Override
public List list() {
return Arrays.asList(userMap.values().toArray());
}
@Override
@Cacheable(value = "user", key = "'user'.concat(#id.toString())")
public User findUserById(Long id) {
log.info("findUserById query from db, id: {}", id);
return userMap.get(id);
}
@Override
@Cacheable(value = "info", key = "'info'.concat(#id.toString())")
public User findInfoById(Long id) {
log.info("findInfoById query from db, id: {}", id);
return userMap.get(id);
}
@Override
@CachePut(value = "user", key = "'user'.concat(#user.id.toString())")
public User update(User user) {
log.info("update db, user: {}", user.toString());
userMap.put(user.getId(), user);
return user;
}
@Override
@CacheEvict(value = "user", key = "'user'.concat(#id.toString())",allEntries = true)
public void remove(Long id) {
log.info("remove from db, id: {}", id);
userMap.remove(id);
}
controller
@RestController
public class IndexController {
@Autowired
private UserService userService;
@GetMapping("/users")
@ResponseBody
public List users() {
return userService.list();
}
@GetMapping("/user/{id}")
@ResponseBody
public User findUserById(@PathVariable("id") Long id) {
return userService.findUserById(id);
}
@GetMapping("/info/{id}")
@ResponseBody
public User findInfoById(@PathVariable("id") Long id) {
return userService.findInfoById(id);
}
@GetMapping("/user/{id}/{name}")
@ResponseBody
public Map update(@PathVariable("id") Long id, @PathVariable("name") String name) {
User user = userService.findUserById(id);
user.setName(name);
userService.update(user);
Map result = new HashMap<>();
result.put("ret", 0);
result.put("msg", "ok");
return result;
}
@GetMapping("/remove/{id}")
@ResponseBody
public Map remove(@PathVariable("id") Long id) {
Map result = new HashMap<>();
userService.remove(id);
result.put("ret", 0);
result.put("msg", "ok");
return result;
}



