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

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

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

本文實例講述了Spring實戰(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 通過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" />
   <!-- 配置默認的緩存區(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>

三 領域模型

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" , 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);
  }
}

五 測試類

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

六 測試結果

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

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

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

相關文章

  • SpringBoot CommandLineRunner的異步任務機制使用

    SpringBoot CommandLineRunner的異步任務機制使用

    這篇文章主要介紹了SpringBoot CommandLineRunner的異步任務機制使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Spring中利用IOC實現(xiàn)注入的方式

    Spring中利用IOC實現(xiàn)注入的方式

    Spring IOC(控制反轉)實現(xiàn)依賴注入,將對象創(chuàng)建和依賴關系的管理交由Spring容器處理,通過注解或XML配置,實現(xiàn)對象之間的松耦合,提高代碼復用性和可維護性
    2023-04-04
  • Java中的ScheduledThreadPoolExecutor定時任務詳解

    Java中的ScheduledThreadPoolExecutor定時任務詳解

    這篇文章主要介紹了Java中的ScheduledThreadPoolExecutor詳解,??ScheduledThreadPoolExecutor?繼承自?ThreadPoolExecutor,它主要用來在給定的延遲之后運行任務,或者定期執(zhí)行任務,ScheduledThreadPoolExecutor?的功能與?Timer?類似<BR>,需要的朋友可以參考下
    2023-12-12
  • IntelliJ IDEA 安裝 Grep Console插件 自定義控制臺輸出多顏色格式功能

    IntelliJ IDEA 安裝 Grep Console插件 自定義控制臺輸出多顏色格式功能

    由于Intellij idea不支持顯示ascii顏色,grep-console插件能很好的解決這個問題,下面就以開發(fā)JavaEE項目中,結合Log4j配置多顏色日志輸出功能,感興趣的朋友一起看看吧
    2020-05-05
  • Java多線程之中斷線程(Interrupt)的使用詳解

    Java多線程之中斷線程(Interrupt)的使用詳解

    interrupt字面上是中斷的意思,但在Java里Thread.interrupt()方法實際上通過某種方式通知線程,并不會直接中止該線程
    2013-05-05
  • Java 中ConcurrentHashMap的實現(xiàn)

    Java 中ConcurrentHashMap的實現(xiàn)

    本文主要介紹Java 中ConcurrentHashMap的實現(xiàn),這里整理了詳細的資料,及簡單實例代碼,有興趣的小伙伴可以參考下
    2016-09-09
  • Mybatis 自動映射(使用需謹慎)

    Mybatis 自動映射(使用需謹慎)

    這篇文章主要介紹了Mybatis 自動映射(使用需謹慎),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • SpringBoot整合FTP實現(xiàn)文件傳輸?shù)牟襟E

    SpringBoot整合FTP實現(xiàn)文件傳輸?shù)牟襟E

    這篇文章主要給大家介紹了SpringBoot整合FTP實現(xiàn)文件傳輸?shù)牟襟E,文中的流程步驟和代碼示例介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2023-11-11
  • 淺析Java基于Socket的文件傳輸案例

    淺析Java基于Socket的文件傳輸案例

    這篇文章主要針對Java基于Socket的文件傳輸案例進行詳細解析,具有一定的參考價值,感興趣的朋友可以參考一下
    2016-02-02
  • Java常用的八種排序算法及代碼實現(xiàn)+圖解

    Java常用的八種排序算法及代碼實現(xiàn)+圖解

    這篇文章主要介紹了Java常用的八種排序算法及代碼實現(xiàn),在Java的時候,對于排序的應用需要熟練的掌握,這樣才能夠確保Java學習時候能夠有扎實的基礎能力。那Java有哪些排序算法呢?本文小編就來詳細說說Java經(jīng)典的8種排序算法,需要的朋友可以參考一下
    2021-12-12

最新評論