프로젝트 구축 시리즈 3: SpringMVC 프레임 워 크 에서 Ehcache 대상, 데이터 캐 시 사용

10560 단어 springMVC
설명: 이 글 은 이전에 쓴 글 입 니 다. 제목 만 바 꾸 었 고 GitHub 소스 코드 가 다운로드 되 지 않 았 습 니 다.
준비 작업
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);

좋은 웹페이지 즐겨찾기