프로젝트 구축 시리즈 3: SpringMVC 프레임 워 크 에서 Ehcache 대상, 데이터 캐 시 사용
10560 단어 springMVC
준비 작업
SpringMVC 환경 을 성공 적 으로 구축 했다 면 Ehcache 의 준비 작업 에 들 어 갈 수 있 을 것 이다.1, jar 가방 다운로드 Ehcache 대상, 데이터 캐 시: http://ehcache.org/downloads/destination?name=ehcache-2.9.0-distribution.tar.gz&bucket=tcdistributions&file=ehcache-2.9.0-distribution.tar.gz 2. jar 가방 을 공사 에 추가 해 야 합 니 다. ehcache - core - 2.9.0. jar 3, src 디 렉 터 리 에 다음 파일 을 추가 해 야 합 니 다. ehcache.xml 프로필 ehcache.xsd XML 구조 정의 파일 이 파일 들 은 ehcache - 2.9.0 폴 더 에서 찾 을 수 있 습 니 다.
2. Ehcache 설정 소개
<diskStore> , ,path=”java.io.tmpdir” 。
Java.io.tmpdir 。
<defaultCache> 。
<cache> 。
name 。
maxElementsInMemory cache 。
eternal: eternal=true,timeToLive timeToIdle , , , 。
, eternal false。
timeToIdleSeconds: , , , ,
0 。
timeToLiveSeconds: , ,
, 0 。 overflowToDisk: , 。
memoryStoreEvictionPolicy: 。
3. 대상, 데이터 캐 시 사례
ehcache. xml 설정:
<?xml version="1.0" encoding="gbk"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir" />
<defaultCache
maxElementsInMemory="10000"
//
eternal="false"
// , , ,
timeToIdleSeconds="120"
// ( )
timeToLiveSeconds="120"
// , ,
overflowToDisk="false"
// ,
/>
<cache name="role_cache" maxElementsInMemory="200" eternal="true"
overflowToDisk="true" />
</ehcache>
applicationContext. xml 설정:
프로젝트 시작 시 캐 시 초기 화
<bean id="roleMenuCacheInit" class="cn.jxufe.web.cache.RoleMenuCacheInit" init-method="init" lazy-init="false" >
RoleMenuCache.java
public class RoleMenuCache {
private final static String ROLE_NAME="role_cache";
private static Cache menuCache=CacheManager.getInstance().getCache(ROLE_NAME);
// cache
public synchronized static void putRoleMenuCaches(long roleId, Collection<SystemNavMenu>menu){
Element element=new Element(roleId, menu);
menuCache.put(element);
}
//
@SuppressWarnings("unchecked")
public synchronized static Collection<SystemNavMenu> getRoleMenuCaches(long roleId){
Element element = null;
try {
element =menuCache.get(roleId);
} catch (CacheException cacheException) {
throw new DataRetrievalFailureException("MenuCache failure: " + cacheException.getMessage(), cacheException);
}
if (element == null) {
return null;
} else {
return (Collection<SystemNavMenu>) element.getValue();
}
}
public synchronized static void removeAllMenuCache(){
menuCache.removeAll();
menuCache.clearStatistics();
menuCache.flush();
}
public synchronized static void removeCache(int role){
menuCache.remove(role);
}
}
RoleMenuCacheInit.java
public class RoleMenuCacheInit {
protected final Logger logger = LoggerFactory.getLogger(getClass());
private IndexService indexService;
public void init(){
logger.info(" ");
Collection<Role> roles=indexService.getAllRoles();
for(Role r:roles){
RoleMenuCache.putRoleMenuCaches(r.getId(),indexService.getRoleMenus(r.getId()));
}
logger.info(" ");
}
public void setIndexService(IndexService indexService) {
this.indexService = indexService;
}
}
4. 사용 방법
//
List<SystemNavMenu> lcMenu=indexService.getRoleMenus(defRoleId);
//
Collection<SystemNavMenu> Menu=RoleMenuCache.getRoleMenuCaches(defRoleId);
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[Spring MVC] [1] 5. 스프링 MVC - 구조 이해핸들러 조회: 핸들러 매핑을 통해 요청 URL에 매핑된 핸들러(컨트롤러)를 조회 핸들러 어댑터 조회: 핸들러를 실행할 수 있는 핸들러 어댑터를 조회 핸들러 어댑터 실행: 핸들러 어댑터를 실행 핸들러 매핑 org.sp...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.