Commit 5391003c authored by michael.simon's avatar michael.simon
Browse files

introduce serviceCache

parent a83f0b6c
package edu.kit.scc.webreg.util;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import org.slf4j.Logger;
import edu.kit.scc.webreg.entity.ServiceEntity;
import edu.kit.scc.webreg.service.ServiceService;
public class ServiceCache {
private Logger logger;
private ServiceService serviceService;
private LoadingCache<Long, ServiceEntity> cache;
public void init() {"Initializing serviceCache");
cache = CacheBuilder.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
public ServiceEntity getServiceFromId(Long key) {
try {
return cache.get(key);
} catch (ExecutionException e) {"Execution Exception on cache", e);
return null;
private CacheLoader<Long, ServiceEntity> cacheLoader = new CacheLoader<Long, ServiceEntity>() {
public ServiceEntity load(Long key) {
ServiceEntity service = serviceService.findByIdWithServiceProps(key);
return service;
private RemovalListener<Long, ServiceEntity> removalListener = new RemovalListener<Long, ServiceEntity>() {
public void onRemoval(RemovalNotification<Long, ServiceEntity> removal) {
if (removal.getValue() != null && logger.isTraceEnabled())
logger.trace("Removing entry {} -> {} from serviceCache ({})",
removal.getKey(), removal.getValue().getName(), removal.getCause());
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment