<?xml version="1.0" encoding="utf-8" ?>
<config xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance>"
xmlns="<http://www.ehcache.org/v3>"
xsi:schemaLocation="<http://www.ehcache.org/v3> <http://www.ehcache.org/schema/ehcache-core-3.0.xsd>">
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<scope>runtime</scope>
<classifier>jakarta</classifier>
</dependency>
Example EhCache Usage:
@Bean
CacheManager cacheManager(){
CachingProvider cachingProvider = Caching.getCachingProvider();
javax.cache.CacheManager jCacheManager = cachingProvider.getCacheManager();
return new JCacheCacheManager(jCacheManager);
}
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;
@Service
@AllArgsConstructor
public class CacheService {
private final CacheManager cacheManager;
public void putInCache(String cacheName, String key, Object value) {
Cache cache = cacheManager.getCache(cacheName);
if (cache != null) {
cache.put(key, value);
}
}
public <T> T getFromCache(String cacheName, String key, Class<T> type) {
Cache cache = cacheManager.getCache(cacheName);
if (cache != null) {
Cache.ValueWrapper wrapper = cache.get(key);
if (wrapper != null) {
return type.cast(wrapper.get());
}
}
return null;
}
public void evictFromCache(String cacheName, String key) {
Cache cache = cacheManager.getCache(cacheName);
if (cache != null) {
cache.evict(key);
}
}
public void clearCache(String cacheName) {
Cache cache = cacheManager.getCache(cacheName);
if (cache != null) {
cache.clear();
}
}
}
Other Way
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;
@Service
public class CacheService {
private final CacheManager cacheManager;
public CacheService(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
// Add entry to cache
public <K, V> void addToCache(String cacheName, K key, V value) {
Cache cache = cacheManager.getCache(cacheName);
if (cache != null) {
cache.put(key, value);
}
}
// Get entry from cache
public <K, V> V getFromCache(String cacheName, K key, Class<V> type) {
Cache cache = cacheManager.getCache(cacheName);
if (cache != null) {
Cache.ValueWrapper wrapper = cache.get(key);
if (wrapper != null) {
return type.cast(wrapper.get());
}
}
return null;
}
// Get entry with default value if not found
public <K, V> V getFromCacheOrDefault(String cacheName, K key, Class<V> type, V defaultValue) {
V value = getFromCache(cacheName, key, type);
return value != null ? value : defaultValue;
}
// Remove single entry
public <K> void evictFromCache(String cacheName, K key) {
Cache cache = cacheManager.getCache(cacheName);
if (cache != null) {
cache.evict(key);
}
}
// Clear entire cache
public void clearCache(String cacheName) {
Cache cache = cacheManager.getCache(cacheName);
if (cache != null) {
cache.clear();
}
}
// Check if key exists in cache
public <K> boolean existsInCache(String cacheName, K key) {
Cache cache = cacheManager.getCache(cacheName);
if (cache != null) {
return cache.get(key) != null;
}
return false;
}
// Get or compute if absent
public <K, V> V getOrCompute(String cacheName, K key, Class<V> type, Supplier<V> valueSupplier) {
V value = getFromCache(cacheName, key, type);
if (value == null) {
value = valueSupplier.get();
if (value != null) {
addToCache(cacheName, key, value);
}
}
return value;
}
}