package com.cache.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
@CacheConfig(cacheNames="myCache")
public class UserService {
@Cacheable(key="'users'")
public List<String> findUsers() {
List<String> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
try {
Thread.currentThread().sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
list.add(""+i);
}
return list;
}
@CachePut(key="'users'")
public List<String> add(){
List<String> list = new ArrayList<>();
for (int i = 0; i < 11; i++) {
list.add(""+i);
}
return list;
}
@CacheEvict(key="'users'")
public void delete(){
System.out.println("删除list");
}
}