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

Spring實(shí)戰(zhàn)之類級別緩存實(shí)現(xiàn)與使用方法

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

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

一 配置文件

<?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")
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)用us對象的方法時(shí)會(huì)執(zhí)行該方法,并緩存方法的結(jié)果
    User u1 = us.getUsersByNameAndAge("孫悟空", 500);
    // 第二次調(diào)用us對象的方法時(shí)直接利用緩存的數(shù)據(jù),并不真正執(zhí)行該方法
    User u2 = us.getAnotherUser("孫悟空", 500);
    System.out.println(u1 == u2); // 輸出true
  }
}

六 測試結(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緩存操作技巧匯總

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

相關(guān)文章

  • springboot配置加密的正確姿勢分享

    springboot配置加密的正確姿勢分享

    在Spring boot開發(fā)中,需要在application.yml文件里配置數(shù)據(jù)庫的連接信息,或者在啟動(dòng)時(shí)傳入數(shù)據(jù)庫密碼,如果不加密,傳明文,數(shù)據(jù)庫就直接暴露了,下面這篇文章主要給大家介紹了關(guān)于springboot配置加密的正確姿勢,需要的朋友可以參考下
    2022-11-11
  • Mybatis的動(dòng)態(tài)拼接條件方式

    Mybatis的動(dòng)態(tài)拼接條件方式

    這篇文章主要介紹了Mybatis的動(dòng)態(tài)拼接條件方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • idea mybatis配置log4j打印sql語句的示例

    idea mybatis配置log4j打印sql語句的示例

    本篇文章主要介紹了idea mybatis配置log4j打印sql語句的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • Java開發(fā)中解決Js的跨域問題過程解析

    Java開發(fā)中解決Js的跨域問題過程解析

    這篇文章主要介紹了Java開發(fā)中解決Js的跨域問題過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java BufferWriter寫文件寫不進(jìn)去或缺失數(shù)據(jù)的解決

    Java BufferWriter寫文件寫不進(jìn)去或缺失數(shù)據(jù)的解決

    這篇文章主要介紹了Java BufferWriter寫文件寫不進(jìn)去或缺失數(shù)據(jù)的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java Spring @Autowired的這些騷操作,你都知道嗎

    Java Spring @Autowired的這些騷操作,你都知道嗎

    這篇文章主要介紹了徹底搞明白Spring中的自動(dòng)裝配和Autowired注解的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2021-09-09
  • springboot封裝JsonUtil,CookieUtil工具類代碼實(shí)例

    springboot封裝JsonUtil,CookieUtil工具類代碼實(shí)例

    這篇文章主要介紹了springboot封裝JsonUtil,CookieUtil工具類過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • 消息隊(duì)列 RabbitMQ 與 Spring 整合使用的實(shí)例代碼

    消息隊(duì)列 RabbitMQ 與 Spring 整合使用的實(shí)例代碼

    本篇文章主要介紹了消息隊(duì)列 RabbitMQ 與 Spring 整合使用的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Java中的TreeSet集合解析

    Java中的TreeSet集合解析

    這篇文章主要介紹了Java中的TreeSet集合解析,  TreeSet是一個(gè)有序的集合,基于TreeMap實(shí)現(xiàn),支持兩種排序方式:自然排序和定制排序,
    TreeSet是非同步的,線程不安全的,需要的朋友可以參考下
    2023-09-09
  • Spring中Bean的加載與SpringBoot的初始化流程詳解

    Spring中Bean的加載與SpringBoot的初始化流程詳解

    這篇文章主要介紹了Spring中Bean的加載與SpringBoot的初始化流程詳解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評論