org.zodiac.cache.http.HttpCacheService Maven / Gradle / Ivy
The newest version!
package org.zodiac.cache.http;
import javax.annotation.Nonnull;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.zodiac.cache.config.HttpCacheableInfo;
import org.zodiac.sdk.toolkit.util.AssertUtil;
public class HttpCacheService implements InitializingBean {
private final HttpCacheableInfo httpCacheInfo;
private final CacheManager cacheManager;
private Cache cache;
public HttpCacheService(@Nonnull HttpCacheableInfo httpCacheInfo, CacheManager cacheManager) {
this.httpCacheInfo = httpCacheInfo;
this.cacheManager = cacheManager;
}
@Override
public void afterPropertiesSet() throws Exception {
if (!httpCacheInfo.isEnabled()) {
return;
}
AssertUtil.notNull(cacheManager, "cacheManager must not be null!");
String cacheName = httpCacheInfo.getCacheName();
this.cache = cacheManager.getCache(cacheName);
AssertUtil.notNull(this.cache, String.format("HttCache cacheName: %s is not config.", cacheName));
}
public boolean get(String key) {
Boolean result = cache.get(key, Boolean.class);
return Boolean.TRUE.equals(result);
}
public HttpCacheService set(String key) {
cache.put(key, Boolean.TRUE);
return this;
}
public HttpCacheService remove(String key) {
cache.evict(key);
return this;
}
public HttpCacheService clear() {
cache.clear();
return this;
}
}