亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

本文實例講述了Spring實戰(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 實現(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é)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

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

相關(guān)文章

  • 解決Mybatis查詢方法selectById()主鍵不一致問題

    解決Mybatis查詢方法selectById()主鍵不一致問題

    這篇文章主要介紹了解決Mybatis查詢方法selectById()主鍵不一致問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Java Map如何根據(jù)key取value以及不指定key取出所有的value

    Java Map如何根據(jù)key取value以及不指定key取出所有的value

    這篇文章主要介紹了Java Map如何根據(jù)key取value以及不指定key取出所有的value,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 利用Jmeter發(fā)送Java請求的實戰(zhàn)記錄

    利用Jmeter發(fā)送Java請求的實戰(zhàn)記錄

    JMeter是Apache組織的開放源代碼項目,它是功能和性能測試的工具,100%的用java實現(xiàn),下面這篇文章主要給大家介紹了關(guān)于如何利用Jmeter發(fā)送Java請求的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • Java生成條形碼code128(親測有效)

    Java生成條形碼code128(親測有效)

    這篇文章主要介紹了Java生成條形碼code128,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • 使用resty Quartz執(zhí)行定時任務(wù)的配置方法

    使用resty Quartz執(zhí)行定時任務(wù)的配置方法

    這篇文章主要為大家介紹了使用resty?Quartz來執(zhí)行定時任務(wù)的配置方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • HttpServletResponse亂碼問題_動力節(jié)點Java學(xué)院整理

    HttpServletResponse亂碼問題_動力節(jié)點Java學(xué)院整理

    這篇文章主要介紹了HttpServletResponse亂碼問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Java銀行取錢線程安全問題實例分析

    Java銀行取錢線程安全問題實例分析

    這篇文章主要介紹了Java銀行取錢線程安全問題,結(jié)合具體實例形式分析了java使用線程操作模擬銀行取錢的相關(guān)安全問題,需要的朋友可以參考下
    2019-09-09
  • Java中使用Closeable接口自動關(guān)閉資源詳解

    Java中使用Closeable接口自動關(guān)閉資源詳解

    這篇文章主要介紹了Java中使用Closeable接口自動關(guān)閉資源詳解,Closeable接口繼承于AutoCloseable,主要的作用就是自動的關(guān)閉資源,其中close()方法是關(guān)閉流并且釋放與其相關(guān)的任何方法,如果流已被關(guān)閉,那么調(diào)用此方法沒有效果,需要的朋友可以參考下
    2023-12-12
  • 剖析Java中阻塞隊列的實現(xiàn)原理及應(yīng)用場景

    剖析Java中阻塞隊列的實現(xiàn)原理及應(yīng)用場景

    這篇文章主要介紹了剖析Java中阻塞隊列的實現(xiàn)原理及應(yīng)用場景,這里也對阻塞和非阻塞隊列的不同之處進(jìn)行了對比,需要的朋友可以參考下
    2015-12-12
  • Java使用DateFormatter格式化日期時間的方法示例

    Java使用DateFormatter格式化日期時間的方法示例

    這篇文章主要介紹了Java使用DateFormatter格式化日期時間的方法,結(jié)合具體實例分析了java使用DateFormatter格式化日期時間的相關(guān)操作技巧,需要的朋友可以參考下
    2017-04-04

最新評論