最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring實戰(zhàn)之清除緩存操作示例

 更新時間:2020年01月14日 09:43:37   作者:cakincqm  
這篇文章主要介紹了Spring實戰(zhàn)之清除緩存操作,結(jié)合實例形式詳細(xì)分析了spring清除緩存操作具體步驟、配置、領(lǐng)域模型及相關(guān)使用技巧,需要的朋友可以參考下

本文實例講述了Spring實戰(zhàn)之清除緩存操作。分享給大家供大家參考,具體如下:

一 配置文件

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:cache="http://www.springframework.org/schema/cache"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/cache
   http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.0.xsd">
   <context:component-scan
      base-package="org.crazyit.app.service" />
   <cache:annotation-driven
      cache-manager="cacheManager" />
   <!-- 配置EhCache的CacheManager 通過configLocation指定ehcache.xml文件的位置 -->
   <bean id="ehCacheManager"
      class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
      p:configLocation="classpath:ehcache.xml" p:shared="false" />
   <!-- 配置基于EhCache的緩存管理器 并將EhCache的CacheManager注入該緩存管理器Bean -->
   <bean id="cacheManager"
      class="org.springframework.cache.ehcache.EhCacheCacheManager"
      p:cacheManager-ref="ehCacheManager">
   </bean>
</beans>

二 屬性文件

<?xml version="1.0" encoding="gbk"?>
<ehcache>
  <diskStore path="java.io.tmpdir" />
   <!-- 配置默認(rèn)的緩存區(qū) -->
  <defaultCache
    maxElementsInMemory="10000"
    eternal="false"
    timeToIdleSeconds="120"
    timeToLiveSeconds="120"
    maxElementsOnDisk="10000000"
    diskExpiryThreadIntervalSeconds="120"
    memoryStoreEvictionPolicy="LRU"/>
   <!-- 配置名為users的緩存區(qū) -->
  <cache name="users"
    maxElementsInMemory="10000"
    eternal="false"
    overflowToDisk="true"
    timeToIdleSeconds="300"
    timeToLiveSeconds="600" />
</ehcache>

三 領(lǐng)域模型

package org.crazyit.app.domain;
public class User
{
   private String name;
   private int age;
   public User()
   {}
   public User(String name, int age)
   {
      super();
      this.name = name;
      this.age = age;
   }
   public String getName()
   {
      return name;
   }
   public void setName(String name)
   {
      this.name = name;
   }
   public int getAge()
   {
      return age;
   }
   public void setAge(int age)
   {
      this.age = age;
   }
}

四 Service

1 接口類

package org.crazyit.app.service;
import org.crazyit.app.domain.User;
public interface UserService
{
   User getUsersByNameAndAge(String name, int age);
   User getAnotherUser(String name, int age);
}

2 實現(xiàn)類

package org.crazyit.app.service.impl;
import org.crazyit.app.service.UserService;
import org.crazyit.app.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.context.annotation.Scope;
@Service("userService")
@Cacheable(value = "users")
public class UserServiceImpl implements UserService
{
  public User getUsersByNameAndAge(String name, int age)
  {
    System.out.println("--正在執(zhí)行findUsersByNameAndAge()查詢方法--");
    return new User(name, age);
  }
  public User getAnotherUser(String name, int age)
  {
    System.out.println("--正在執(zhí)行findAnotherUser()查詢方法--");
    return new User(name, age);
  }
  // 指定根據(jù)name、age參數(shù)清除緩存
  @CacheEvict(value = "users")
  public void evictUser(String name, int age)
  {
    System.out.println("--正在清空"+ name
      + " , " + age + "對應(yīng)的緩存--");
  }
  // 指定清除user緩存區(qū)所有緩存數(shù)據(jù)
  @CacheEvict(value = "users" , allEntries=true)
  public void evictAll()
  {
    System.out.println("--正在清空整個緩存--");
  }
}

五 測試類

package lee;
import org.crazyit.app.service.UserService;
import org.crazyit.app.domain.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest
{
  public static void main(String[] args)
  {
    ApplicationContext ctx =
      new ClassPathXmlApplicationContext("beans.xml");
    UserService us = ctx.getBean("userService" , UserService.class);
    // 調(diào)用us對象的2個帶緩存的方法,系統(tǒng)會緩存兩個方法返回的數(shù)據(jù)
    User u1 = us.getUsersByNameAndAge("孫悟空", 500);
    User u2 = us.getAnotherUser("豬八戒", 400);
    //調(diào)用evictUser()方法清除緩存中指定的數(shù)據(jù)
    us.evictUser("豬八戒", 400);
    // 由于前面根據(jù)"豬八戒", 400緩存的數(shù)據(jù)已經(jīng)被清除了,
    // 因此下面代碼會重新執(zhí)行,方法返回的數(shù)據(jù)將被再次緩存。
    User u3 = us.getAnotherUser("豬八戒", 400); // ①
    System.out.println(u2 == u3); // 輸出false
    // 由于前面前面已經(jīng)緩存了參數(shù)為"孫悟空", 500的數(shù)據(jù),
    // 因此下面代碼不會重新執(zhí)行,直接利用緩存中的數(shù)據(jù)。
    User u4 = us.getAnotherUser("孫悟空", 500);  // ②
    System.out.println(u1 == u4); // 輸出true
    // 清空整個緩存。
    us.evictAll();
    // 由于整個緩存都已經(jīng)被清空,因此下面兩行代碼都會重新執(zhí)行
    User u5 = us.getAnotherUser("孫悟空", 500);
    User u6 = us.getAnotherUser("豬八戒", 400);
    System.out.println(u1 == u5); // 輸出false
    System.out.println(u3 == u6); // 輸出false
  }
}

六 測試結(jié)果

--正在執(zhí)行findUsersByNameAndAge()查詢方法--
--正在執(zhí)行findAnotherUser()查詢方法--
--正在清空豬八戒 , 400對應(yīng)的緩存--
--正在執(zhí)行findAnotherUser()查詢方法--
false
true
--正在清空整個緩存--
--正在執(zhí)行findAnotherUser()查詢方法--
--正在執(zhí)行findAnotherUser()查詢方法--
false
false

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計有所幫助。

相關(guān)文章

  • 詳解java nio中的select和channel

    詳解java nio中的select和channel

    這篇文章主要介紹了java nio中的select和channel
    2019-05-05
  • springboot實現(xiàn)通過路徑從磁盤直接讀取圖片

    springboot實現(xiàn)通過路徑從磁盤直接讀取圖片

    這篇文章主要介紹了springboot實現(xiàn)通過路徑從磁盤直接讀取圖片,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java如何基于wsimport調(diào)用wcf接口

    Java如何基于wsimport調(diào)用wcf接口

    這篇文章主要介紹了Java如何基于wsimport調(diào)用wcf接口,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • Mybatis 高級用法和tk.mybatis使用示例詳解

    Mybatis 高級用法和tk.mybatis使用示例詳解

    tkmybatis 是對底層 sql 進(jìn)行了抽象封裝,不需要考慮 sql 怎么寫,只需要按照邏輯思維,遵循 tkmybatis 的語法即可實現(xiàn)數(shù)據(jù)庫操作,這篇文章主要介紹了Mybatis 高級用法和tk.mybatis使用,需要的朋友可以參考下
    2024-05-05
  • Java的WeakHashMap源碼解析及使用場景詳解

    Java的WeakHashMap源碼解析及使用場景詳解

    這篇文章主要介紹了Java的WeakHashMap源碼解析及使用場景詳解,Map本身生命周期很長,需要長期貯留內(nèi)存中,但Map中的Entry可以刪除,使用時可以從其它地方再次取得,需要的朋友可以參考下
    2023-09-09
  • java實現(xiàn)學(xué)生信息錄入界面

    java實現(xiàn)學(xué)生信息錄入界面

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)學(xué)生信息錄入界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • mybatis plus自動生成器解析(及遇到的坑)

    mybatis plus自動生成器解析(及遇到的坑)

    這篇文章主要介紹了mybatis-plus自動生成器及遇到的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 一文弄懂fastjson

    一文弄懂fastjson

    fastjson?是一個java語言編寫的高性能且功能完善的JSON庫,本文主要介紹了fastjson的使用,具有一定的參考價值,感興趣的可以了解一下
    2023-05-05
  • 使用java實現(xiàn)http多線程斷點下載文件(二)

    使用java實現(xiàn)http多線程斷點下載文件(二)

    下載工具我想沒有幾個人不會用的吧,前段時間比較無聊,花了點時間用java寫了個簡單的http多線程下載程序,我實現(xiàn)的這個http下載工具功能很簡單,就是一個多線程以及一個斷點恢復(fù),當(dāng)然下載是必不可少的,需要的朋友可以參考下
    2012-12-12
  • 如何構(gòu)建可重復(fù)讀取inputStream的request

    如何構(gòu)建可重復(fù)讀取inputStream的request

    這篇文章主要介紹了如何構(gòu)建可重復(fù)讀取inputStream的request,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評論

固镇县| 盐城市| 富平县| 方正县| 九江市| 山东省| 贺州市| 全州县| 弋阳县| 明溪县| 修文县| 谷城县| 抚松县| 杂多县| 玛多县| 永顺县| 青阳县| 科技| 剑河县| 奉节县| 上杭县| 景东| 红安县| 太仆寺旗| 民和| 潼南县| 山西省| 天祝| 博爱县| 广河县| 沅江市| 晋城| 邵阳市| 榆中县| 巩义市| 那曲县| 绥宁县| 永靖县| 娄烦县| 靖江市| 日照市|