-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#284 add spring cache to citype attribute repository for ci service q…
…uery
- Loading branch information
Showing
5 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
cmdb-core/src/main/java/com/webank/cmdb/cache/CacheHandlerInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
cmdb-core/src/main/java/com/webank/cmdb/cache/RequestScopedCacheManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters