如何在spring boot中获取spring缓存大小?
我有以下两种方法:如何在spring boot中获取spring缓存大小?,spring,spring-boot,spring-cache,Spring,Spring Boot,Spring Cache,我有以下两种方法: @Cacheable(cacheNames = "foos") public List<FooDto> getAllFoos() { return this.fooRepository.findAll().stream() .map(FooEntityDomainToDtoMapper::mapDomainToDto) // mapping entity to dto .collect(Collectors.
@Cacheable(cacheNames = "foos")
public List<FooDto> getAllFoos() {
return this.fooRepository.findAll().stream()
.map(FooEntityDomainToDtoMapper::mapDomainToDto) // mapping entity to dto
.collect(Collectors.toList());
}
@Cacheable(cacheNames = "foos",key = "#fooDto.Id")
public FooDto getFooById(FooDto fooDto) {
return this.fooRepository.findById(fooDto.getId()).stream()
.map(FooEntityDomainToDtoMapper::mapDomainToDto) // mapping entity to dto
.collect(Collectors.toList());
}
@Cacheable(cacheNames=“foos”)
公共列表getAllFoos(){
返回此.fooRepository.findAll().stream()
.map(FooEntityDomainTodMapper::mapDomainToDto)//将实体映射到dto
.collect(Collectors.toList());
}
@可缓存(cacheNames=“foos”,key=“#fooDto.Id”)
公共FooDto getFooById(FooDto FooDto){
返回此.foodrepository.findById(fooDto.getId()).stream()
.map(FooEntityDomainTodMapper::mapDomainToDto)//将实体映射到dto
.collect(Collectors.toList());
}
第一个getAllFoos()
将在系统启动期间调用,第二个将在用户按特定id请求对象时在启动系统后调用。我想知道第二个方法是否会占用任何单独的缓存空间,或者它只是在第一个方法获得的缓存中添加键?我想确认,即使我对第二个getFooById()
进行注释,缓存的大小是否相同?有没有办法获得缓存的大小
附言:我们没有使用任何缓存实现,只是使用
springbootstartercache
虽然没有直接的方法可以查看缓存数据并获取大小,但可以使用反射来实现。这就是我所做的
public Object getAllCache() {
//Autowire your cache manager first and then process the request using cache manager
ConcurrentMapCache cache = (ConcurrentMapCache) manager.getCache("yourCachename");
Object store1 = null;
try {
Field store = ConcurrentMapCache.class.getDeclaredField("store");
store.setAccessible(true);
store1 = store.get(cache);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return store1.toString();
}
这有帮助吗:?是的,考虑一下@smit。但是我想,也许他们在以后的版本中加入了一些新的东西