spring缓存机制-@CachePut的用法(四)

2023-12-14 18:38

本文主要是介绍spring缓存机制-@CachePut的用法(四),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

spring是一个轻量级开源框架,以IoC(Inverse of Control:控制反转)和AOP(Aspect Oriented Programming:面向切面编程)为内核,
兼具功能强大的的原生展现层spring mvc,原生持久层spring jdbc和原生业务层等技术,并且以海纳百川的胸怀整合了开源世界里众多
著名的第三方框架和类库,已经逐渐成为世界上使用最多的JavaEE企业级应用开源框架.
在使用spring缓存技术过程中,有时候我们既希望方法被调用,同时又希望结果被缓存.例如记录日志,调用接口等,
在这种情况下需要用到@CachePut 注释.
1.通过maven构建项目,直接看pom.xml文件(略:参考相关博客)
2.创建service,dao和model,并将spring缓存加在service层,直接看代码UserService,UserServiceImpl,UserDao,UserDaoImpl和User.(略:参考相关博客)

  在UserServiceImpl代码中通过@CachePut既保证方法被调用,同时又保证结果被缓存.

<span style="font-size:12px;">package com.ilucky.spring.cache.service.impl;import java.util.Map;import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;import com.ilucky.spring.cache.dao.UserDao;
import com.ilucky.spring.cache.model.User;
import com.ilucky.spring.cache.service.UserService;/*** @author IluckySi* @date 20140613*/
public class UserServiceImpl implements UserService {private UserDao userDao;/*** JVM加载spring配置文件时, 通过set方法注入本类的依赖.* @param userDao*/public void setUserDao(UserDao userDao) {this.userDao = userDao;}/*** 对数据进行增删改时清空缓存, 查询时使用缓存, 其中value为缓存区,* allEntries表示清空缓存中所有的数据, 使用#"user.id + '方法名'"做为key.*/@Override@CacheEvict(value = "data", key = "#id + 'add'")  public void add(User user) {System.out.println("UserService: method- add(User user)" );userDao.add(user);}@Override@CacheEvict(value = "data", key = "#id + 'delete'")  public void delete(String id) {System.out.println("UserService: method-delete(String id)" );userDao.delete(id);}@Override@CacheEvict(value = "data", key = "#user.id+ 'update'")  public void update(User user) {System.out.println("UserService: method-update(User user)" );userDao.update(user);}@Override@Cacheable(value = "data", condition = "#id.length() > 5")   public User find(String id) {System.out.println("UserService: method-find(String id)" );return userDao.find(id);}@Override//@CachePut(value = "data")  //@Cacheable(value = "data")  @Cacheable(value = "data")  public Map<String, User> getAll() {System.out.println("UserService: method-getAll()" );return userDao.getAll();}
}
</span>
3.创建spring配置文件,配置spring自己的缓存管理器和bean之间的依赖关系,直接看代码spring-cahce.xml.(略:参考相关博客)

4.最后通过测试类测试spring缓存机制是否生效,直接看代码MainTest.

<span style="font-size:12px;">package com.ilucky.spring.cache;import java.util.Map;
import java.util.Map.Entry;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.ilucky.spring.cache.model.User;
import com.ilucky.spring.cache.service.UserService;/*** @author IluckySi* @date 20140614* 测试spring缓存机制是否生效.*/
public class MainTest {public static void main(String[] args) {//加载ClassPath路径下的spring配置文件, 并获取service bean.ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-cache.xml");  UserService userService = context.getBean("userService", UserService.class); System.out.println("service bean: " + userService);//添加三个用户, 然后查询并输出.userService.add(new User("1", "Ilucky1", "pwd1"));userService.add(new User("2", "Ilucky2", "pwd2"));userService.add(new User("3", "Ilucky3", "pwd3"));Map<String, User> map = userService.getAll();for(Entry<String, User> entry : map.entrySet()) {System.out.println(entry.getValue());}//验证@Cacheable和CachePut的区别, CachePut既保证方法被调用,同时保证结果被缓存.Map<String, User> map2 = userService.getAll();for(Entry<String, User> entry : map2.entrySet()) {System.out.println(entry.getValue());}}
}
/**
spring缓存分为两种:方法和数据,如果方法被清除了,需要重新从数据库获取数据放入缓存,另外只有方法加入了缓存才可以获取缓存中的数据.
@Cacheable(value = "data")  
public Map<String, User> getAll() {
输出结果:
service bean: com.ilucky.spring.cache.service.impl.UserServiceImpl@17ec9f7
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method-getAll()
UserDao method- getAll()
记录日志--调用接口等操作!
3-Ilucky3-pwd3
2-Ilucky2-pwd2
1-Ilucky1-pwd1
3-Ilucky3-pwd3
2-Ilucky2-pwd2
1-Ilucky1-pwd1@CachePut(value = "data")  
service bean: com.ilucky.spring.cache.service.impl.UserServiceImpl@fd918a
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method- add(User user)
UserDao method- add(User user)
UserService: method-getAll()
UserDao method- getAll()
记录日志--调用接口等操作!
3-Ilucky3-pwd3
2-Ilucky2-pwd2
1-Ilucky1-pwd1
UserService: method-getAll()
UserDao method- getAll()
记录日志--调用接口等操作!
3-Ilucky3-pwd3
2-Ilucky2-pwd2
1-Ilucky1-pwd1
*/
</span>
  在测试代码中通过ClassPathXmlApplicationContext类加载spring配置文件,因为此maven项目是jar项目,并且在maven中约定资源
  放在src/main/resources目录,所以spring配置文件需要放在src/main/resources目录下.
注意:spring缓存分为两种:方法和数据,如果方法被清除了,需要重新从数据库获取数据放入缓存,另外只有方法加入了缓存才可以获取缓存中的数据.




这篇关于spring缓存机制-@CachePut的用法(四)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/493604

相关文章

Spring实现Bean的初始化和销毁的方式

《Spring实现Bean的初始化和销毁的方式》:本文主要介绍Spring实现Bean的初始化和销毁的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Bean的初始化二、Bean的销毁总结在前面的章节当中介绍完毕了ApplicationContext,也就

Java的"伪泛型"变"真泛型"后对性能的影响

《Java的伪泛型变真泛型后对性能的影响》泛型擦除本质上就是擦除与泛型相关的一切信息,例如参数化类型、类型变量等,Javac还将在需要时进行类型检查及强制类型转换,甚至在必要时会合成桥方法,这篇文章主... 目录1、真假泛型2、性能影响泛型存在于Java源代码中,在编译为字节码文件之前都会进行泛型擦除(ty

Java中的getBytes()方法使用详解

《Java中的getBytes()方法使用详解》:本文主要介绍Java中getBytes()方法使用的相关资料,getBytes()方法有多个重载形式,可以根据需要指定字符集来进行转换,文中通过代... 目录前言一、常见重载形式二、示例代码三、getBytes(Charset charset)和getByt

Java使用Stream流的Lambda语法进行List转Map的操作方式

《Java使用Stream流的Lambda语法进行List转Map的操作方式》:本文主要介绍Java使用Stream流的Lambda语法进行List转Map的操作方式,具有很好的参考价值,希望对大... 目录背景Stream流的Lambda语法应用实例1、定义要操作的UserDto2、ListChina编程转成M

Spring框架中@Lazy延迟加载原理和使用详解

《Spring框架中@Lazy延迟加载原理和使用详解》:本文主要介绍Spring框架中@Lazy延迟加载原理和使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、@Lazy延迟加载原理1.延迟加载原理1.1 @Lazy三种配置方法1.2 @Component

使用easy connect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题

《使用easyconnect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题》:本文主要介绍使用easyconnect之后,maven无法... 目录使用easGWowCy connect之后,maven无法使用,原来需要配置-DJava.net.pr

idea报错java: 非法字符: ‘\ufeff‘的解决步骤以及说明

《idea报错java:非法字符:‘ufeff‘的解决步骤以及说明》:本文主要介绍idea报错java:非法字符:ufeff的解决步骤以及说明,文章详细解释了为什么在Java中会出现uf... 目录BOM是什么?1. BOM的作用2. 为什么会出现 \ufeff 错误?3. 如何解决 \ufeff 问题?最

使用Java编写一个字符脱敏工具类

《使用Java编写一个字符脱敏工具类》这篇文章主要为大家详细介绍了如何使用Java编写一个字符脱敏工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、字符脱敏工具类2、测试工具类3、测试结果1、字符脱敏工具类import lombok.extern.slf4j.Slf4j

Java实现按字节长度截取字符串

《Java实现按字节长度截取字符串》在Java中,由于字符串可能包含多字节字符,直接按字节长度截取可能会导致乱码或截取不准确的问题,下面我们就来看看几种按字节长度截取字符串的方法吧... 目录方法一:使用String的getBytes方法方法二:指定字符编码处理方法三:更精确的字符编码处理使用示例注意事项方

嵌入式Linux驱动中的异步通知机制详解

《嵌入式Linux驱动中的异步通知机制详解》:本文主要介绍嵌入式Linux驱动中的异步通知机制,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、异步通知的核心概念1. 什么是异步通知2. 异步通知的关键组件二、异步通知的实现原理三、代码示例分析1. 设备结构