java spring 使用注解來實現緩存


這里舉例使用spring3.1.4 + ehcache 

注解的方式使用cache 是在spring3.1加入的

使用方法:

1.ehcache依賴+spring依賴

<!-- ehcache依賴-->
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.7.2</version>
</dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.1.4.RELEASE</version>
        </dependency>
<!-- 這里有注解類需要的jar -->
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.1.4.RELEASE</version> </dependency>

      <dependency>
          <groupId>cglib</groupId>
          <artifactId>cglib</artifactId>
          <version>2.2.2</version>
      </dependency>

 

2.spring+ehcache的簡單配置

Ehcache的基本配置,具體參數意思看:http://www.ehcache.org/documentation/configuration/configuration

name

cache的唯一標識

maxElementsInMemory

最大創建條數

eternal

緩存是否是永久的,默認false

timeToLiveSeconds

緩存的存活時間

timeToIdleSeconds

多長時間不訪問就清楚該緩存

overflowToDisk

內存不足是否寫入磁盤,默認False

maxElementsOnDisk

持久化到硬盤最大條數

memoryStoreEvictionPolicy

緩存滿了后,清除緩存的規則,

自帶三種:FIFO(先進先出),LFU(最少使用),LRU(最近最少使用)

diskSpoolBufferSizeMB

磁盤緩存的緩存區大小,默認30M

diskExpiryThreadIntervalSeconds

磁盤失效線程時間間隔

2.1 首先建立一個ehcache.xml的配置文件

<ehcache>
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />

    <cache
            name="onecache"
            maxElementsInMemory="10000"
            eternal="false"
            overflowToDisk="false"
            timeToIdleSeconds="10"
            timeToLiveSeconds="10"/>
</ehcache>

 

 

2.2在spring的apllication.xml 加入注入的cache

    <cache:annotation-driven cache-manager="cacheManager"/>

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
          p:cacheManager-ref="ehcache"/>
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
          p:configLocation="classpath:ehcache.xml"
          p:shared="true"/>

這里還需要在配置文件頭部引入

 xmlns:cache="http://www.springframework.org/schema/cache"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="
      ...
        http://www.springframework.org/schema/cache   http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
        "

在mvc-servlet.xml 里加入

  <mvc:annotation-driven/>  <!-- 挺重要的,不加注解不會被掃描-->

3.注解使用

3.1 用於對象 class

@Cacheable(value = "onecache")
class A1{}

這種情況類中方法中返回值都會被緩存,

3.2用於方法method

 @Cacheable(value = "onecache", key = "#name",condition = "#age < 25")
  public xx findEmployeeBySurname(String firstName, String name, int age) {
    return xxx
  }

這個就會將age小於25的值,按name為key緩存

3.3 清除

@CacheEvict(value="onecache",key="#name + 'haha'")
public void delete(String name) {
System.out.println("delete one name");
}

還可使用下面的清除全部

@CacheEvict(value="oneCache",allEntries=true)

4.代碼調用

  @Autowired
    private CacheManager cacheManager;

    //獲取當前時間
    public String getABCCache() {        
cacheManager.getCache("ccc").put("hello", new Date().toString()); return (String) cacheManager.getCache("ccc").get("hello").get(); }

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM