Following was the config and controller class being used by us for implementation of 2.x ehcache:
In the above code, the eviction policy is LFU as defined in application.properties. I have replaced the above config with the below code where i am unable to understand how to implement the LFU Eviction policy:
import net.sf.ehcache.management.CacheStatistics;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tv.videoready.searchconnector.config.CacheConfig;
import tv.videoready.searchconnector.util.GenericResponse;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
/**
* The type Cache controller.
*/
@RestController
@RequestMapping("admin/cache")
public class CacheController {
private static final Logger log= LogManager.getLogger(CacheController.class);
/**
* The Cache config.
*/
@Autowired
CacheConfig cacheConfig;
/**
* Gets stats.
*
* @return the stats
*/
@GetMapping("stats")
public GenericResponse<Map<String, Map<String, String>>> getStats(@RequestHeader(value="true-client-ip",required = false) String trueClientIp,
@RequestHeader(value="tpr-id",required = false) String tprId) {
log.info("[AKAMAI_GRN] true-client-id :: {} and tpr-id :: {}",trueClientIp,tprId);
EhCacheCacheManager cacheManager = cacheConfig.cacheManager();
try {
Map<String, Map<String, String>> data = new HashMap<>();
for (String cache : cacheManager.getCacheNames()) {
Map<String, String> stat = new LinkedHashMap<>();
CacheStatistics stats = new CacheStatistics(
Objects.requireNonNull(cacheManager.getCacheManager()).getEhcache(cache));
stat.put("cacheName", cache);
stat.put("status", cacheManager.getCacheManager().getEhcache(cache).isDisabled()
? "disabled"
: "enabled");
stat.put("evictionPolicy",
stats.getEhcache().getCacheConfiguration().getMemoryStoreEvictionPolicy().toString());
stat.put("timeToLiveInSeconds",
Long.toString(stats.getEhcache().getCacheConfiguration().getTimeToLiveSeconds()));
stat.put("maxCacheSize",
Long.toString(stats.getEhcache().getCacheConfiguration().getMaxEntriesLocalHeap()));
stat.put("currentObjectCountInCache",
Long.toString(stats.getEhcache().getKeysWithExpiryCheck().size()));
stat.put("hits", Long.toString(stats.getCacheHits()));
stat.put("misses", Long.toString(stats.getCacheMisses()));
stat.put("hitPercentage", stats.getCacheHitPercentage() * 100 + " %");
stat.put("missPercentage", stats.getCacheMissPercentage() * 100 + " %");
data.put(cache, stat);
}
return new GenericResponse<>((data),
HttpStatus.OK.value(),
"Cache Stats fetched successfully.");
} catch (Exception e) {
e.printStackTrace();
return new GenericResponse<>(
null,
HttpStatus.INTERNAL_SERVER_ERROR.value(),
"Exception Occurred while Calculating Stats: " + e.getMessage()
);
}
}
/**
* Clear all caches generic response.
*
* @return the generic response
*/
@GetMapping("clear")
public GenericResponse<Map<String, Map<String, String>>> clearAllCaches(@RequestHeader(value="true-client-ip",required = false) String trueClientIp,
@RequestHeader(value="tpr-id",required = false) String tprId) {
log.info("[AKAMAI_GRN] true-client-id :: {} and tpr-id :: {}",trueClientIp,tprId);
EhCacheCacheManager cacheManager = cacheConfig.cacheManager();
try {
Map<String, Map<String, String>> data = new HashMap<>();
cacheManager.getCacheNames()
.forEach(cacheName -> {
data.put(cacheName, Collections.singletonMap("status", "fail"));
try {
Objects.requireNonNull(cacheManager.getCache(cacheName)).clear();
} catch (Exception ignored) {
}
data.put(cacheName, Collections.singletonMap("status", "success"));
});
return new GenericResponse<>((data),
HttpStatus.OK.value(),
"Caches Cleared successfully.");
} catch (Exception e) {
e.printStackTrace();
return new GenericResponse<>(
null,
HttpStatus.INTERNAL_SERVER_ERROR.value(),
"Exception Occurred while Clearing Caches: " + e.getMessage()
);
}
}
/**
* Disable all caches generic response.
*
* @return the generic response
*/
@GetMapping("disable")
public GenericResponse<Map<String, Map<String, String>>> disableAllCaches(@RequestHeader(value="true-client-ip",required = false) String trueClientIp,
@RequestHeader(value="tpr-id",required = false) String tprId) {
log.info("[AKAMAI_GRN] true-client-id :: {} and tpr-id :: {}",trueClientIp,tprId);
EhCacheCacheManager cacheManager = cacheConfig.cacheManager();
try {
Map<String, Map<String, String>> data = new HashMap<>();
cacheManager.getCacheNames()
.forEach(cacheName -> {
data.put(cacheName, Collections.singletonMap("status", "enabled"));
try {
Objects.requireNonNull(cacheManager.getCacheManager()).getEhcache(cacheName)
.setDisabled(true);
} catch (Exception ignored) {
}
data.put(cacheName, Collections.singletonMap("status", "disabled"));
});
return new GenericResponse<>((data),
HttpStatus.OK.value(),
"Caches Cleared successfully.");
} catch (Exception e) {
e.printStackTrace();
return new GenericResponse<>(
null,
HttpStatus.INTERNAL_SERVER_ERROR.value(),
"Exception Occurred while Clearing Caches: " + e.getMessage()
);
}
}
/**
* Enable all caches generic response.
*
* @return the generic response
*/
@GetMapping("enable")
public GenericResponse<Map<String, Map<String, String>>> enableAllCaches(@RequestHeader(value="true-client-ip",required = false) String trueClientIp,
@RequestHeader(value="tpr-id",required = false) String tprId) {
log.info("[AKAMAI_GRN] true-client-id :: {} and tpr-id :: {}",trueClientIp,tprId);
EhCacheCacheManager cacheManager = cacheConfig.cacheManager();
try {
Map<String, Map<String, String>> data = new HashMap<>();
cacheManager.getCacheNames()
.forEach(cacheName -> {
data.put(cacheName, Collections.singletonMap("status", "disabled"));
try {
Objects.requireNonNull(cacheManager.getCacheManager()).getEhcache(cacheName)
.setDisabled(false);
} catch (Exception ignored) {
}
data.put(cacheName, Collections.singletonMap("status", "enabled"));
});
return new GenericResponse<>((data),
HttpStatus.OK.value(),
"Caches Cleared successfully.");
} catch (Exception e) {
e.printStackTrace();
return new GenericResponse<>(
null,
HttpStatus.INTERNAL_SERVER_ERROR.value(),
"Exception Occurred while Clearing Caches: " + e.getMessage()
);
}
}
}
In the above code, there are a lot of issues during migration as the getCacheNames(), EhCacheCacheManager, and various other elements are not available in 3.10.8. Can anyone help with the above please?