安排不同缓存名称的性能和影响

Performance and impact to schedule different cachenames

我有一个 API 基于 spring 启动应用程序,它以不同的时间延迟缓存不同的方法,

@Cacheable(value = "findAllByIdType", key="#p0")
List<NomenclatureEntity> findAllByIdType(String type);

@Cacheable(value = "findByIdTypeAndIdKey", key="#p0 + #p1")
NomenclatureEntity findByIdTypeAndIdKey(String type,String key); 

缓存配置:

@Configuration
@ConditionalOnExpression("${cache.enable:true}")
@EnableCaching
@EnableScheduling
public class CacheConfig {
    private static final String findByIdTypeAndIdKey = "findByIdTypeAndIdKey";
    private static final String findAllByIdType = "findAllByIdType";
    private static final Logger LOGGER = LoggerFactory.getLogger(CacheConfig.class);


      @Bean
      public CacheManager cacheManager() {
          ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager(findByIdTypeAndIdKey,findAllByIdType);

          return cacheManager;
      }

    @CacheEvict(allEntries = true, value = {findByIdTypeAndIdKey})
    @Scheduled(fixedDelay =  24 * 60 * 60 * 1000 ,  initialDelay = 500)
    public void reportCacheEvict() {
        LOGGER.debug("## Flush Data Cache findByIdTypeAndIdKey");
    }


    @CacheEvict(allEntries = true, value = {findAllByIdType})
    @Scheduled(fixedDelay =  24 * 60 * 60 * 1000 ,  initialDelay = 500)
    public void reportCacheEvict2() {
        LOGGER.debug("## Flush Data Cache findAllByIdType");
    }

}

该应用程序运行完美,但我对性能存疑,这是一个很好的方法(练习)还是有更好的方法?

这取决于您的应用程序的大小。

ConcurrentMapCacheManager

顾名思义,您使用 Map 来缓存数据,因此堆的内存会像缓存一样增长。这在开发期间适用,但对于生产,请考虑使用其他类型的缓存,如 Redis。 对于缓存逐出,您可以像以前一样控制它,或者依靠缓存提供程序在达到分配的内存时逐出数据(默认情况下,Redis 使用 LRU (Least Recently Used) 算法逐出数据)。