aron
aron

Reputation: 91

CacheEventListener is not working Ehcache

I'm trying to log events coming from ehcache and in logs I see the creation of cache, cache manager but not custom event listener:

2022-09-05 14:49:42.218  INFO 29767 --- [  restartedMain] org.ehcache.jsr107.Eh107CacheManager     : Registering Ehcache MBean javax.cache:type=CacheStatistics,CacheManager=urn.X-ehcache.jsr107-default-config,Cache=primaryUserPhotoResponseCache

cache event listener implementation

@Slf4j
@Component
public class EhCacheListener implements CacheEventListener<String, DownloadPrimaryUserPhotoResponse> {
    @Override
    public void onEvent(CacheEvent<? extends String, ? extends DownloadPrimaryUserPhotoResponse> cacheEvent) {
        final var photoURL = cacheEvent.getNewValue().getPhotoURL();

        switch (cacheEvent.getType()) {
            case CREATED -> log.debug("{} been saved to cache",
                    photoURL);
            case REMOVED -> log.debug("{} been removed from cache",
                    photoURL);
            case EXPIRED -> log.debug("{} been has expired",
                    photoURL);
        }
    }
}

ehcache configuration

@Configuration
@RequiredArgsConstructor
public class EhCacheConfig {
    public static final String CACHE = "primaryUserPhotoResponseCache";
    private final EhCacheListener listener;

    @Bean
    public CacheManager cacheManager() {
        final var cacheManager = getCachingProvider().getCacheManager();

        javax.cache.configuration.Configuration<String, DownloadPrimaryUserPhotoResponse> config =
                fromEhcacheCacheConfiguration(getCacheConfig());

        cacheManager.createCache(CACHE, config);

        return cacheManager;
    }

    private CacheConfiguration<String, DownloadPrimaryUserPhotoResponse> getCacheConfig() {
        final var pool = newResourcePoolsBuilder()
                .offheap(20, MB)
                .build();

        return newCacheConfigurationBuilder(
                String.class, DownloadPrimaryUserPhotoResponse.class, pool)
                .withExpiry(timeToIdleExpiration(Duration.ofHours(1))) 
                .withService(getCacheEventListenerBuilder())
                .build();
    }

    private CacheEventListenerConfigurationBuilder getCacheEventListenerBuilder() {
        return newEventListenerConfiguration(listener, CREATED, EXPIRED, REMOVED)
                .asynchronous()
                .unordered();
    }
}

I ain't got any errors while running the project, the listener just simply not working

Upvotes: 0

Views: 2121

Answers (2)

user2071703
user2071703

Reputation: 324

   private DefaultCacheEventListenerConfiguration getCacheEventListener() {
        return newEventListenerConfiguration(listener, CREATED, EXPIRED, REMOVED)
                .asynchronous()
                .unordered()
                .build();
    }

Missing build()

.withService(getCacheEventListener())

Upvotes: 1

Puzer man
Puzer man

Reputation: 1

You need change offheaph(10, MB) and remove one CASE

Upvotes: -1

Related Questions