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

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

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

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

一 配置文件

<?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 通過(guò)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" , key="#name")
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);
  }
}

五 測(cè)試類

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對(duì)象的方法時(shí)會(huì)執(zhí)行該方法,并緩存方法的結(jié)果
    User u1 = us.getUsersByNameAndAge("孫悟空", 500);
    // 指定使用name作為緩存key,因此主要兩次調(diào)用方法的name參數(shù)相同
    // 緩存機(jī)制就會(huì)生效
    User u2 = us.getAnotherUser("孫悟空", 400);
    System.out.println(u1 == u2); // 輸出true
  }
}

六 測(cè)試結(jié)果

--正在執(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緩存操作技巧匯總

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

相關(guān)文章

  • java 讀取zip文件的兩種方式示例詳解

    java 讀取zip文件的兩種方式示例詳解

    ZIP(壓縮文件)是一種常見的文件格式,在Java中可以使用java.util.zip包提供的API來(lái)讀取和處理ZIP文件,本文將介紹如何使用Java讀取ZIP文件,并提供代碼示例,感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • 基于mybatis中數(shù)組傳遞注意事項(xiàng)

    基于mybatis中數(shù)組傳遞注意事項(xiàng)

    這篇文章主要介紹了mybatis中數(shù)組傳遞注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringBoot+MyBatis簡(jiǎn)單數(shù)據(jù)訪問(wèn)應(yīng)用的實(shí)例代碼

    SpringBoot+MyBatis簡(jiǎn)單數(shù)據(jù)訪問(wèn)應(yīng)用的實(shí)例代碼

    這篇文章主要介紹了SpringBoot+MyBatis簡(jiǎn)單數(shù)據(jù)訪問(wèn)應(yīng)用的實(shí)例代碼,需要的朋友可以參考下
    2017-05-05
  • Java?電話號(hào)碼的組合示例詳解

    Java?電話號(hào)碼的組合示例詳解

    這篇文章主要介紹了Java?電話號(hào)碼的組合,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • Java-JFrame窗體美化方式

    Java-JFrame窗體美化方式

    這篇文章主要介紹了Java-JFrame窗體美化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • Java的分片上傳功能的實(shí)現(xiàn)

    Java的分片上傳功能的實(shí)現(xiàn)

    本文主要介紹了Java的分片上傳功能的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • SpringCloud Feign傳遞HttpServletRequest對(duì)象流程

    SpringCloud Feign傳遞HttpServletRequest對(duì)象流程

    HttpServletRequest接口的對(duì)象代表客戶端的請(qǐng)求,當(dāng)客戶端通過(guò)HTTP協(xié)議訪問(wèn)Tomcat服務(wù)器時(shí),HTTP請(qǐng)求中的所有信息都封裝在HttpServletRequest接口的對(duì)象中,這篇文章介紹了Feign傳遞HttpServletRequest對(duì)象的流程,感興趣的同學(xué)可以參考下文
    2023-05-05
  • 在Java中關(guān)閉SQL執(zhí)行日志來(lái)優(yōu)化服務(wù)器性能

    在Java中關(guān)閉SQL執(zhí)行日志來(lái)優(yōu)化服務(wù)器性能

    Java應(yīng)用程序中,數(shù)據(jù)庫(kù)操作是一個(gè)常見的任務(wù),如果不適當(dāng)?shù)靥幚鞸QL執(zhí)行日志,可能會(huì)導(dǎo)致不必要的性能損失,SQL執(zhí)行日志通常由數(shù)據(jù)庫(kù)連接池、ORM框架(如Hibernate、MyBatis)、或者應(yīng)用服務(wù)器的內(nèi)置日志機(jī)制生成,本文將探討如何在Java中關(guān)閉SQL執(zhí)行日志,提升應(yīng)用性能和效率
    2024-11-11
  • 如何獲取springboot打成jar后的classpath

    如何獲取springboot打成jar后的classpath

    這篇文章主要介紹了如何獲取springboot打成jar后的classpath問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過(guò)程詳解

    SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過(guò)程詳解

    這篇文章主要介紹了SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12

最新評(píng)論

长乐市| 阿拉善左旗| 滦南县| 如东县| 武安市| 晋城| 肥西县| 米脂县| 邵武市| 蕲春县| 常德市| 潮州市| 杨浦区| 勃利县| 汶上县| 乐安县| 抚州市| 东港市| 莎车县| 新竹市| 乐都县| 绿春县| 大冶市| 灌云县| 曲靖市| 乐平市| 抚宁县| 岳阳县| 武乡县| 金川县| 宁都县| 曲水县| 垦利县| 长顺县| 兴化市| 荣昌县| 桦南县| 韶关市| 河曲县| 孝感市| 江油市|