Skip to content

Commit

Permalink
#284 add spring cache to citype attribute repository for ci service q…
Browse files Browse the repository at this point in the history
…uery
  • Loading branch information
junchensz committed Oct 17, 2019
1 parent 0b1278b commit 7b40177
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.webank.cmdb.cache;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class CacheHandlerInterceptor extends HandlerInterceptorAdapter{

@Autowired
private RequestScopedCacheManager requestScopedCacheManager;

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

requestScopedCacheManager.clearCaches();
return true;
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
requestScopedCacheManager.clearCaches();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.webank.cmdb.cache;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.stereotype.Component;

@Component
public class RequestScopedCacheManager implements CacheManager {

private static final ThreadLocal<Map<String, Cache>> threadLocalCache = new ThreadLocal<Map<String, Cache>>() {
@Override
protected Map<String, Cache> initialValue() {
return new ConcurrentHashMap<String, Cache>();
}
};

@Override
public Cache getCache(String name) {
final Map<String, Cache> cacheMap = threadLocalCache.get();
Cache cache = cacheMap.get(name);
if (cache == null) {
cache = createCache(name);
cacheMap.put(name, cache);
}
return cache;
}

private Cache createCache(String name) {
return new ConcurrentMapCache(name);
}

@Override
public Collection<String> getCacheNames() {
return threadLocalCache.get().keySet();
}

public void clearCaches() {
threadLocalCache.remove();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@Configuration
@EnableCaching
@ComponentScan({ "com.webank.cmdb.service", "com.webank.cmdb.mvc", "com.webank.cmdb.util" })
@ComponentScan({ "com.webank.cmdb.service", "com.webank.cmdb.mvc", "com.webank.cmdb.util","com.webank.cmdb.cache" })
@Import({ DatabaseConfig.class })
@EnableConfigurationProperties({ ApplicationProperties.class ,DatasourceProperties.class, UIProperties.class, SecurityProperties.class})
public class SpringAppConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
Expand All @@ -11,6 +13,8 @@

import com.webank.cmdb.domain.AdmBasekeyCode;

@CacheConfig(cacheManager = "requestScopedCacheManager", cacheNames = "admCiTypeAttr")
@Cacheable
public interface AdmBasekeyCodeRepository extends JpaRepository<AdmBasekeyCode, Integer> {
@Query(value = "select max(seq_no) from adm_basekey_code where id_adm_basekey_cat = :catId", nativeQuery = true)
Integer getMaxSeqNoByCatId(@Param("catId") int catId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import java.util.List;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.webank.cmdb.domain.AdmCiTypeAttr;

@CacheConfig(cacheManager = "requestScopedCacheManager", cacheNames = "admCiTypeAttr")
@Cacheable
public interface AdmCiTypeAttrRepository extends JpaRepository<AdmCiTypeAttr, Integer> {
List<AdmCiTypeAttr> findAllByCiTypeId(Integer ciTypeId);

Expand Down

0 comments on commit 7b40177

Please sign in to comment.