shiro+mybatis+ehcache详细配置 缓存降低数据库压力

1.首先:pom.xml需要的jar

<dependency>
  <groupId>org.mybatisgroupId>
  <artifactId>mybatis-ehcacheartifactId>
  <version>1.0.0version>
 dependency>
2. 添加ehcache.xml的配置文件

xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

         xsi:noNamespaceSchemaLocation="ehcache.xsd"  

         updateCheck="true" monitoring="autodetect"  

         dynamicConfig="true">  

    <diskStore path="java.io.tmpdir"/>

  

  

  <defaultCache eternal="false" maxElementsInMemory="1000"

       overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"

       timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />

 

    <cache name="cache"  

           maxEntriesLocalHeap="10000"  

           maxEntriesLocalDisk="1000"  

           eternal="true"  

           diskSpoolBufferSizeMB="20"  

           timeToIdleSeconds="0"  

           timeToLiveSeconds="0"  

           memoryStoreEvictionPolicy="LFU"  

           transactionalMode="off">  

    cache>

   

    <cache name="passwordRetryCache"

           maxEntriesLocalHeap="2000"

           eternal="false"

           timeToIdleSeconds="600"

           timeToLiveSeconds="0"

           overflowToDisk="false"

           statistics="true">

    cache>

    <cache name="shiro-activeSessionCache"

           maxEntriesLocalHeap="2000"

           eternal="false"

           timeToIdleSeconds="3600"

           timeToLiveSeconds="0"

           overflowToDisk="false"

           statistics="true">

    cache> 

<cache name="shiro-kickout-session"

           maxEntriesLocalHeap="2000"

           eternal="false"

           timeToIdleSeconds="3600"

           timeToLiveSeconds="0"

           overflowToDisk="false"

           statistics="true">

    cache>

ehcache>

3. 在spring.xml中加入encache的配置


<bean id="cacheManager" class="com.jd.shiro.spring.SpringCacheManagerWrapper" >

<property name="cacheManager" ref="springCacheManager"/>

bean>

<bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">

        <property name="cacheManager" ref="ehcacheManager"/>

    bean>

   

    <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

        <property name="configLocation" value="classpath:ehcache.xml"/>

        <property name="shared" value="true">property>

    bean>

4.在mybatis mapper.xml 文件里加入配置


<cache type="org.mybatis.caches.ehcache.LoggingEhcache" />


第一种 是带日志的 第二种不带日志

假如某个业务是不要缓存的,可以在当前业务下加上useCache="false"

你可能感兴趣的:(JAVA)