org.zodiac.autoconfigure.cache.http.HttpCacheableServletAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.cache.http;
import java.util.Set;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.zodiac.autoconfigure.cache.condition.ConditionalOnHttpCacheEnabled;
import org.zodiac.commons.util.ArrayUtil;
import org.zodiac.commons.util.Colls;
@SpringBootConfiguration
@ConditionalOnHttpCacheEnabled
@ConditionalOnWebApplication(type = Type.SERVLET)
@AutoConfigureAfter(value = {HttpCacheableAutoConfiguration.class})
@ConditionalOnClass(value = {org.zodiac.cache.http.HttpCacheable.class})
@ConditionalOnBean(value = {org.springframework.cache.CacheManager.class})
public class HttpCacheableServletAutoConfiguration implements WebMvcConfigurer {
private final WebMvcProperties webMvcProperties;
private final HttpCacheableProperties httpCacheProperties;
private final org.zodiac.cache.http.HttpCacheService httpCacheService;
public HttpCacheableServletAutoConfiguration(WebMvcProperties webMvcProperties, HttpCacheableProperties httpCacheProperties,
org.zodiac.cache.http.HttpCacheService httpCacheService) {
this.webMvcProperties = webMvcProperties;
this.httpCacheProperties = httpCacheProperties;
this.httpCacheService = httpCacheService;
}
@Override
public void addInterceptors(@NonNull InterceptorRegistry registry) {
if (!httpCacheProperties.isEnabled()) {
/*没有开启缓存*/
return;
}
Set excludePatterns = Colls.set(httpCacheProperties.getExcludePatterns());
String staticPathPattern = webMvcProperties.getStaticPathPattern();
/* 如果静态 目录 不为“/**” */
if (!HttpCacheableProperties.DEFAULT_STATIC_PATH_PATTERN.equals(staticPathPattern.trim())) {
excludePatterns.add(staticPathPattern);
}
org.zodiac.cache.http.servlet.HttpCacheableInterceptor httpCacheableInterceptor = new org.zodiac.cache.http.servlet.HttpCacheableInterceptor(httpCacheProperties, httpCacheService);
registry.addInterceptor(httpCacheableInterceptor)
.addPathPatterns(httpCacheProperties.getIncludePatterns().toArray(ArrayUtil.EMPTY_STRING_ARRAY))
.excludePathPatterns(excludePatterns.toArray(ArrayUtil.EMPTY_STRING_ARRAY));
}
}