Below is my Simple Config Class
package com.mcd.restaurantmanager.product.config;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
@EnableCaching
@Configuration
public class CacheConfig {
}
Below is my ehcache.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.ehcache.org/v3"
xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
xsi:schemaLocation="
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
<cache alias="hierarchyLevelByRestaurantGroupId">
<key-type>java.math.BigDecimal</key-type>
<value-type>java.lang.String</value-type>
<expiry>
<ttl unit="seconds">30</ttl>
</expiry>
<listeners>
<listener>
<class>com.mcd.restaurantmanager.product.service.CustomCacheEventLogger</class>
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
<event-ordering-mode>UNORDERED</event-ordering-mode>
<events-to-fire-on>CREATED</events-to-fire-on>
<events-to-fire-on>EXPIRED</events-to-fire-on>
</listener>
</listeners>
<resources>
<heap unit="entries">2</heap>
<offheap unit="MB">10</offheap>
</resources>
</cache>
<cache-template name="default">
<expiry>
<ttl unit="seconds">30</ttl>
</expiry>
<listeners>
<listener>
<class>guru.springframework.ehcache.config.CacheLogger</class>
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
<event-ordering-mode>UNORDERED</event-ordering-mode>
<events-to-fire-on>CREATED</events-to-fire-on>
<events-to-fire-on>EXPIRED</events-to-fire-on>
<events-to-fire-on>EVICTED</events-to-fire-on>
</listener>
</listeners>
<resources>
<heap>1000</heap>
<offheap unit="MB">10</offheap>
<disk persistent="true" unit="MB">20</disk>
</resources>
</cache-template>
</config>
Below is how I am using caching with methods
@Cacheable(value = "id", key = "#restaurantGroupId")
public String getLevel(BigDecimal id, String somRandom) {
return getGroups(somRandom).stream()
.filter(restaurantGroup -> restaurantGroup.getId().equals(id))
.map(restaurantGroup -> restaurantGroup.getLevel().getValue())
.findFirst()
.orElse(null);
}
This is my listener
package com.mcd.restaurantmanager.product.service;
import lombok.extern.slf4j.Slf4j;
import org.ehcache.event.CacheEvent;
import org.ehcache.event.CacheEventListener;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class CustomCacheEventLogger implements CacheEventListener<Object, Object> {
@Override
public void onEvent(CacheEvent<?, ?> event) {
log.info("Cache event = {}, Key = {}, Old value = {}, New value = {}",
event.getType(),
event.getKey(),
event.getOldValue(),
event.getNewValue());
}
}
I am not sure if I missed something, but I am getting below error
A component required a bean of type 'org.springframework.cache.CacheManager' that could not be found.
Please let me know what I am missing, I am using ehcache 3.