Spring实战之缓存使用condition操作示例

 更新时间:2020年01月14日 09:06:59   作者:cakincqm  
这篇文章主要介绍了Spring实战之缓存使用condition操作,结合实例形式分析了Spring缓存使用condition具体配置、属性、领域模型等相关操作技巧与注意事项,需要的朋友可以参考下

本文实例讲述了Spring实战之缓存使用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" />
   <!-- 配置默认的缓存区 -->
  <defaultCache
    maxElementsInMemory="10000"
    eternal="false"
    timeToIdleSeconds="120"
    timeToLiveSeconds="120"
    maxElementsOnDisk="10000000"
    diskExpiryThreadIntervalSeconds="120"
    memoryStoreEvictionPolicy="LRU"/>
   <!-- 配置名为users的缓存区 -->
  <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 实现类

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("--正在执行findUsersByNameAndAge()查询方法--");
    return new User(name, age);
  }
  public User getAnotherUser(String name, int age)
  {
    System.out.println("--正在执行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);
    // 调用方法时age参数不小于100,因此不会缓存,
    // 所以下面两次方法调用都会真正执行这些方法。
    User u1 = us.getUsersByNameAndAge("孙悟空", 500);
    User u2 = us.getAnotherUser("孙悟空", 500);
    System.out.println(u1 == u2); // 输出false
    // 调用方法时age参数小于100,因此会缓存,
    // 所以下面第二次方法调用时不会真正执行该方法,而是直接使用缓存数据。
    User u3 = us.getUsersByNameAndAge("孙悟空", 50);
    User u4 = us.getAnotherUser("孙悟空", 50);
    System.out.println(u3 == u4); // 输出true
  }
}

六 测试结果

--正在执行findUsersByNameAndAge()查询方法--
--正在执行findAnotherUser()查询方法--
false
--正在执行findUsersByNameAndAge()查询方法--
true

更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

相关文章

  • Java设计模式之共享模式/享元模式(Flyweight模式)介绍

    Java设计模式之共享模式/享元模式(Flyweight模式)介绍

    这篇文章主要介绍了Java设计模式之共享模式/享元模式(Flyweight模式)介绍,本文讲解了为什么使用共享模式/享元模式、如何使用共享模式/享元模式、Flyweight模式在XML等数据源中应用等内容,需要的朋友可以参考下
    2015-03-03
  • emoji表情与unicode编码互转的实现(JS,JAVA,C#)

    emoji表情与unicode编码互转的实现(JS,JAVA,C#)

    这篇文章主要介绍了emoji表情与unicode编码互转的实现(JS,JAVA,C#),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • JAVA多线程之中断机制stop()、interrupted()、isInterrupted()

    JAVA多线程之中断机制stop()、interrupted()、isInterrupted()

    这篇文章主要介绍了JAVA多线程之中断机制stop()、interrupted()、isInterrupted()的相关资料,需要的朋友可以参考下
    2016-05-05
  • spring boot基于Java的容器配置讲解

    spring boot基于Java的容器配置讲解

    这篇文章主要介绍了spring boot基于Java的容器配置讲解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • Java操作文件路径正反斜杠问题解决

    Java操作文件路径正反斜杠问题解决

    最近在实现文件上传时,windows与linux系统出现的问题,两个系统中操作文件使用"\","/"导致IOException,本文主要介绍了Java操作文件路径正反斜杠问题解决,感兴趣的可以了解一下啊
    2024-01-01
  • Java反射使用的详细介绍(最新推荐)

    Java反射使用的详细介绍(最新推荐)

    这篇文章主要介绍了Java反射使用的详细介绍,反射的第一步都是先得到编译后的Class类对象,然后就可以得到Class的全部成分,本文结合实例代码详细讲解,需要的朋友可以参考下
    2023-02-02
  • java线程同步操作实例详解

    java线程同步操作实例详解

    这篇文章主要介绍了java线程同步操作,结合实例形式分析了Java线程同步与锁机制相关原理、操作技巧与注意事项,需要的朋友可以参考下
    2018-09-09
  • Intellij IDEA如何自定义注释模板的实现方法

    Intellij IDEA如何自定义注释模板的实现方法

    这篇文章主要介绍了Intellij IDEA如何自定义注释模板的实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-05-05
  • Spring Cloud Gateway(读取、修改 Request Body)的操作

    Spring Cloud Gateway(读取、修改 Request Body)的操作

    这篇文章主要介绍了Spring Cloud Gateway(读取、修改 Request Body)的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • 聊聊Lombok中的@Builder注解使用教程

    聊聊Lombok中的@Builder注解使用教程

    @Builder注解的作用主要是用来生成对象,并且可以为对象链式赋值。接下来通过本文给大家介绍Lombok中的@Builder注解使用教程,感兴趣的朋友一起看看吧
    2021-11-11

最新评论