欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring實(shí)戰(zhàn)之方法級(jí)別緩存用法示例

 更新時(shí)間:2020年01月14日 09:37:26   作者:cakincqm  
這篇文章主要介紹了Spring實(shí)戰(zhàn)之方法級(jí)別緩存用法,結(jié)合實(shí)例形式分析了spring方法級(jí)別緩存配置、屬性文件、領(lǐng)域模型及相關(guān)使用技巧,需要的朋友可以參考下

本文實(shí)例講述了Spring實(shí)戰(zhàn)之方法級(jí)別緩存用法。分享給大家供大家參考,具體如下:

一 配置文件

<?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"/>
   <!-- 配置名為users1的緩存區(qū) -->
  <cache name="users1"
    maxElementsInMemory="10000"
    eternal="false"
    overflowToDisk="true"
    timeToIdleSeconds="300"
    timeToLiveSeconds="600" />
  <cache name="users2"
    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")
public class UserServiceImpl implements UserService
{
  @Cacheable(value = "users1")
  public User getUsersByNameAndAge(String name, int age)
  {
    System.out.println("--正在執(zhí)行findUsersByNameAndAge()查詢方法--");
    return new User(name, age);
  }
  @Cacheable(value = "users2")
  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);
    // 由于getAnotherUser()方法使用另一個(gè)緩存區(qū),
    // 因此無法使用getUsersByNameAndAge()方法緩存區(qū)的數(shù)據(jù)。
    User u2 = us.getAnotherUser("孫悟空", 500);
    System.out.println(u1 == u2); // 輸出false
    // getAnotherUser("孫悟空", 500)已經(jīng)執(zhí)行過一次,故下面代碼使用緩存
    User u3 = us.getAnotherUser("孫悟空", 500);
    System.out.println(u2 == u3); // 輸出true
  }
}

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

--正在執(zhí)行findUsersByNameAndAge()查詢方法--
--正在執(zhí)行findAnotherUser()查詢方法--
false
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 Netty實(shí)現(xiàn)聊天室+私聊功能的示例代碼

    JAVA Netty實(shí)現(xiàn)聊天室+私聊功能的示例代碼

    這篇文章主要介紹了JAVA Netty實(shí)現(xiàn)聊天室+私聊功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java實(shí)現(xiàn)簡(jiǎn)單棋盤存檔和讀取功能

    Java實(shí)現(xiàn)簡(jiǎn)單棋盤存檔和讀取功能

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單棋盤存檔和讀取功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • 深入理解Java序列化與反序列化

    深入理解Java序列化與反序列化

    今天教大家深入理解Java的序列化與反序列化,文中介紹的非常詳細(xì),有很多代碼示例,對(duì)正在學(xué)習(xí)Java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • Java中抽象類和接口的用法詳解

    Java中抽象類和接口的用法詳解

    這篇文章主要為大家詳細(xì)介紹了Java中抽象類和接口的使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-08-08
  • springboot?@Validated的概念及示例實(shí)戰(zhàn)

    springboot?@Validated的概念及示例實(shí)戰(zhàn)

    這篇文章主要介紹了springboot?@Validated的概念以及實(shí)戰(zhàn),使用?@Validated?注解,Spring?Boot?應(yīng)用可以有效地實(shí)現(xiàn)輸入驗(yàn)證,提高數(shù)據(jù)的準(zhǔn)確性和應(yīng)用的安全性,本文結(jié)合實(shí)例給大家講解的非常詳細(xì),需要的朋友可以參考下
    2024-04-04
  • java實(shí)現(xiàn)本地緩存的示例代碼

    java實(shí)現(xiàn)本地緩存的示例代碼

    在高性能服務(wù)架構(gòu)設(shè)計(jì)中,緩存是不可或缺的環(huán)節(jié),因此這篇文章主要為大家詳細(xì)介紹了java中如何實(shí)現(xiàn)本地緩存,感興趣的小伙伴可以了解一下
    2024-01-01
  • Java棧的運(yùn)用之中綴表達(dá)式求值詳解

    Java棧的運(yùn)用之中綴表達(dá)式求值詳解

    本文來介紹一題中綴表達(dá)式求值的問題,就是給定一個(gè)中綴計(jì)算式,編寫程序?qū)⑦@個(gè)式子運(yùn)算結(jié)果給計(jì)算出來,其實(shí)和后綴表達(dá)式的思路差不多,都是棧的運(yùn)用問題,感興趣的可以了解一下
    2022-11-11
  • MyBatis一對(duì)一映射初識(shí)教程

    MyBatis一對(duì)一映射初識(shí)教程

    MyBatis是一個(gè)支持普通SQL查詢,存儲(chǔ)過程和高級(jí)映射的優(yōu)秀持久層框架。在我們生活中一對(duì)一的例子很多見,下面通過本文給大家?guī)砹薽ybatis一對(duì)一映射初識(shí)教程,感興趣的朋友一起看下吧
    2016-08-08
  • 最新評(píng)論