First of all, sorry for my poor english...
I have some problems with my spring boot application.
I want to apply redis cache in my specific method. So I created CacheConfig and coded RedisCacheManagerBuilderCustomizer
This is my Redis CacheConfig file
@Configuration(proxyBeanMethods = false)public class CacheConfig { /** * GenericJackson2JsonRedisSerializer * list 역직렬화시에러발생할수도..? * @return */ @Bean public RedisCacheConfiguration redisCacheConfiguration() { return RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofSeconds(60)) .disableCachingNullValues() .serializeKeysWith( RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()) ) .serializeValuesWith( RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()) ); } @Bean public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() { System.out.println("CacheType.PERFORMANCE_LIST.cacheName = " + CacheType.PERFORMANCE_LIST.cacheName); System.out.println("CacheType.DETAIL.cacheName = " + CacheType.PERFORMANCE_DETAIL.cacheName); return (builder) -> builder .withCacheConfiguration(CacheType.PERFORMANCE_LIST.cacheName, RedisCacheConfiguration.defaultCacheConfig() .prefixCacheNameWith("cache") .entryTtl(Duration.ofHours(CacheType.PERFORMANCE_LIST.getExpireTime())) .disableCachingNullValues() .serializeKeysWith( RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()) ) .serializeValuesWith( RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()) )) .withCacheConfiguration(CacheType.PERFORMANCE_DETAIL.cacheName, RedisCacheConfiguration.defaultCacheConfig() .prefixCacheNameWith("cache") .entryTtl(Duration.ofHours(CacheType.PERFORMANCE_DETAIL.getExpireTime())) .disableCachingNullValues() .serializeKeysWith( RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()) ) .serializeValuesWith( RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()) )); } @Getter private enum CacheType{ PERFORMANCE_LIST("performanceList", 12), PERFORMANCE_DETAIL("performanceDetail", 12); private String cacheName; private Integer expireTime; CacheType(String cacheName, Integer expireTime) { this.cacheName = cacheName; this.expireTime = expireTime; } private static final Map<String, CacheType> CACHE_TYPE_MAP; static{ Map<String, CacheType> map = new ConcurrentHashMap<>(); for (CacheType cacheType : CacheType.values()) { map.put(cacheType.name(), cacheType); } CACHE_TYPE_MAP = Collections.unmodifiableMap(map); } public static CacheType get(String name){ if (!StringUtils.hasText(name)){ throw new CustomException(ErrorCode.BAD_REQUEST.getMessage(), ErrorCode.BAD_REQUEST); } return Optional.ofNullable(CACHE_TYPE_MAP.get(name)).orElseThrow(() -> new CustomException(ErrorCode.CACHE_TYPE_NOT_FOUND.getMessage(), ErrorCode.CACHE_TYPE_NOT_FOUND)); } }}
And This is my specific method that i want to use caching
@Cacheable(value = "performanceList", key = "{#sort, #pageable.pageNumber, #pageable.pageSize}") public List<CommonPerformanceDto> getPerformanceBySortAndPage(Pageable pageable, String sort) { return performanceRepositoryPort.findAllBySortAndPaging(pageable, sort).stream().map(CommonPerformanceDto::domainToDto).collect(Collectors.toList()); }
And My ProjectApplication Setting is
@EnableAsync@EnableCaching@EnableJpaAuditing@EnableScheduling@SpringBootApplication(exclude = {ElastiCacheAutoConfiguration.class})public class BackendApplication { public static void main(String[] args) { SpringApplication.run(BackendApplication.class, args); } @Profile("dev") @Bean public TestDataInit testDataInit(ArtistRepository artistRepository, PerformanceRepository performanceRepository, UserRepository userRepository, FollowRepository followRepository, ReviewRepository reviewRepository, UserImageRepository userImageRepository, PasswordEncoder passwordEncoder, UserSearchRepository userSearchRepository, ZzimRepositoryPort zzimRepositoryPort, PerformerRepository performerRepository) { return new TestDataInit(artistRepository, performanceRepository, userRepository, followRepository, reviewRepository, userImageRepository, passwordEncoder, userSearchRepository, zzimRepositoryPort, performerRepository); }}
But When i requested some api end-point that i applied @Cacheable Method, then I got error like
Cannot find cache named 'performanceList' for Builder[public] caches=[performanceList] | key='{#sort, #pageable.pageNumber, #pageable.pageSize}' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false']
Why this error happened?? Please help me...