org.ligoj.bootstrap.resource.system.cache.CacheResource Maven / Gradle / Ivy
/*
* Licensed under MIT (https://github.com/ligoj/ligoj/blob/master/LICENSE)
*/
package org.ligoj.bootstrap.resource.system.cache;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import jakarta.persistence.EntityNotFoundException;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Service;
import com.hazelcast.cache.HazelcastCacheManager;
import com.hazelcast.cache.impl.CacheProxy;
import com.hazelcast.cluster.Member;
import com.hazelcast.core.HazelcastInstanceNotActiveException;
import com.hazelcast.internal.cluster.ClusterService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
/**
* Cache resource.
*/
@Path("/system/cache")
@Service
@Transactional
@Slf4j
@Produces(MediaType.APPLICATION_JSON)
public class CacheResource implements ApplicationListener {
@Autowired
protected CacheManager cacheManager;
@Setter
@Value("${hazelcast.statistics.enable:false}")
private boolean statisticsEnabled = false;
/**
* Return the installed caches.
*
* @return the installed caches
*/
@GET
public List getCaches() {
return cacheManager.getCacheNames().stream().map(this::getCache).toList();
}
/**
* Return the information of given cache.
*
* @param name the cache's name to display.
*
* @return the cache's configuration.
*/
@GET
@Path("{name:[\\w\\-]+}")
public CacheStatistics getCache(@PathParam("name") final String name) {
final var result = new CacheStatistics();
final var cache = getCacheNative(name);
final var node = newCacheNode(cache.getNodeEngine().getLocalMember());
node.setCluster(newCacheCluster(cache.getNodeEngine().getClusterService()));
result.setId(name);
result.setNode(node);
setStatistics(result, cache.getLocalCacheStatistics());
return result;
}
@SuppressWarnings("unchecked")
private CacheProxy