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

Spring實(shí)戰(zhàn)之緩存使用condition操作示例

 更新時間:2020年01月14日 09:06:59   作者:cakincqm  
這篇文章主要介紹了Spring實(shí)戰(zhàn)之緩存使用condition操作,結(jié)合實(shí)例形式分析了Spring緩存使用condition具體配置、屬性、領(lǐng)域模型等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下

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

一 配置文件

<?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 實(shí)現(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.context.annotation.Scope;
@Service("userService")
@Cacheable(value = "users" , condition="#age<100")
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);
  }
}

五 測試類

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)用方法時age參數(shù)不小于100,因此不會緩存,
    // 所以下面兩次方法調(diào)用都會真正執(zhí)行這些方法。
    User u1 = us.getUsersByNameAndAge("孫悟空", 500);
    User u2 = us.getAnotherUser("孫悟空", 500);
    System.out.println(u1 == u2); // 輸出false
    // 調(diào)用方法時age參數(shù)小于100,因此會緩存,
    // 所以下面第二次方法調(diào)用時不會真正執(zhí)行該方法,而是直接使用緩存數(shù)據(jù)。
    User u3 = us.getUsersByNameAndAge("孫悟空", 50);
    User u4 = us.getAnotherUser("孫悟空", 50);
    System.out.println(u3 == u4); // 輸出true
  }
}

六 測試結(jié)果

--正在執(zhí)行findUsersByNameAndAge()查詢方法--
--正在執(zhí)行findAnotherUser()查詢方法--
false
--正在執(zhí)行findUsersByNameAndAge()查詢方法--
true

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

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

相關(guān)文章

  • Java設(shè)計(jì)模式之共享模式/享元模式(Flyweight模式)介紹

    Java設(shè)計(jì)模式之共享模式/享元模式(Flyweight模式)介紹

    這篇文章主要介紹了Java設(shè)計(jì)模式之共享模式/享元模式(Flyweight模式)介紹,本文講解了為什么使用共享模式/享元模式、如何使用共享模式/享元模式、Flyweight模式在XML等數(shù)據(jù)源中應(yīng)用等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • emoji表情與unicode編碼互轉(zhuǎn)的實(shí)現(xiàn)(JS,JAVA,C#)

    emoji表情與unicode編碼互轉(zhuǎn)的實(shí)現(xiàn)(JS,JAVA,C#)

    這篇文章主要介紹了emoji表情與unicode編碼互轉(zhuǎn)的實(shí)現(xiàn)(JS,JAVA,C#),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • JAVA多線程之中斷機(jī)制stop()、interrupted()、isInterrupted()

    JAVA多線程之中斷機(jī)制stop()、interrupted()、isInterrupted()

    這篇文章主要介紹了JAVA多線程之中斷機(jī)制stop()、interrupted()、isInterrupted()的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • spring boot基于Java的容器配置講解

    spring boot基于Java的容器配置講解

    這篇文章主要介紹了spring boot基于Java的容器配置講解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Java操作文件路徑正反斜杠問題解決

    Java操作文件路徑正反斜杠問題解決

    最近在實(shí)現(xiàn)文件上傳時,windows與linux系統(tǒng)出現(xiàn)的問題,兩個系統(tǒng)中操作文件使用"\","/"導(dǎo)致IOException,本文主要介紹了Java操作文件路徑正反斜杠問題解決,感興趣的可以了解一下啊
    2024-01-01
  • Java反射使用的詳細(xì)介紹(最新推薦)

    Java反射使用的詳細(xì)介紹(最新推薦)

    這篇文章主要介紹了Java反射使用的詳細(xì)介紹,反射的第一步都是先得到編譯后的Class類對象,然后就可以得到Class的全部成分,本文結(jié)合實(shí)例代碼詳細(xì)講解,需要的朋友可以參考下
    2023-02-02
  • java線程同步操作實(shí)例詳解

    java線程同步操作實(shí)例詳解

    這篇文章主要介紹了java線程同步操作,結(jié)合實(shí)例形式分析了Java線程同步與鎖機(jī)制相關(guān)原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-09-09
  • Intellij IDEA如何自定義注釋模板的實(shí)現(xiàn)方法

    Intellij IDEA如何自定義注釋模板的實(shí)現(xiàn)方法

    這篇文章主要介紹了Intellij IDEA如何自定義注釋模板的實(shí)現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • Spring Cloud Gateway(讀取、修改 Request Body)的操作

    Spring Cloud Gateway(讀取、修改 Request Body)的操作

    這篇文章主要介紹了Spring Cloud Gateway(讀取、修改 Request Body)的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • 聊聊Lombok中的@Builder注解使用教程

    聊聊Lombok中的@Builder注解使用教程

    @Builder注解的作用主要是用來生成對象,并且可以為對象鏈?zhǔn)劫x值。接下來通過本文給大家介紹Lombok中的@Builder注解使用教程,感興趣的朋友一起看看吧
    2021-11-11

最新評論

达拉特旗| 马龙县| 察哈| 寿阳县| 富平县| 汝阳县| 行唐县| 高安市| 陈巴尔虎旗| 四子王旗| 凤翔县| 双桥区| 平陆县| 平定县| 杭州市| 金乡县| 延安市| 中山市| 常德市| 麻城市| 丹阳市| 嘉义县| 玛曲县| 开江县| 图片| 平果县| 普兰店市| 鄢陵县| 尤溪县| 黄浦区| 会理县| 孝昌县| 黑水县| 芷江| 汝城县| 隆子县| 张家界市| 靖宇县| 崇仁县| 兰州市| 高安市|