Spring Cache简单实现
发布日期:2022-02-17 04:52:24 浏览次数:16 分类:技术文章

本文共 3224 字,大约阅读时间需要 10 分钟。

Spring Cache简单实现

业务场景

假定一个业务场景:在项目中,用户查询是一个非常频繁的操作,从性能优化的角度,自然会想到对用户的查询方法做缓存,以避免频繁的数据库访问操作,提高页面的响应速度。

通常的做法是以用户的userId作为键值key,以返回的用户对象作为value值进行存储,而以相同的userId查询用户时,程序将直接从缓存中获取结果并返回,否则更新缓存。

代码清单

这里使用的是Spring默认的缓存管理器

服务类

package com.example.cache.springcache;import com.example.cache.customize.User;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;/** * @author: 博客「成猿手册」 * @description: com.example.cache.springcache * @date: 2020/6/7 */@Service(value = "userServiceBean")public class UserService {
//使用名为users的缓存 @Cacheable(cacheNames = "users") public User getUserById(String userId) {
//方法内部不考虑缓存逻辑直接实现业务 System.out.println("query user by userId=" + userId); return getFromDB(userId); } //模拟数据库查询 private User getFromDB(String userId) {
System.out.println("querying id from DB..." + userId); return new User(userId); }}

getUserById()方法被标注了一个注解,即 @Cacheable(cacheNames = "users")当调用这个方法时,会先从users缓存中查询匹配的缓存对象:

  1. 如果存在,则直接返回;

  2. 如果不存在,则执行方法体内的逻辑(查询数据库),并将返回值放进缓存中。对应缓存的key为userId的值,value就是userId所对应的User对象。

相关配置

applicationContext.xml

cache.xml

实体类

Java对象和序列化是息息相关的,一般情况下,需要被缓存的实体类需要实现Serializable,只有实现了Serializable接口的类,JVM才可以将其对象进行序列化,对于Redis,EhCache等缓存套件来说,被缓存的对象应该是可序列化的,否则在网络传输,硬盘存储时,都会抛出序列化异常。

package com.example.cache.customize;import java.io.Serializable;/** * @author: 博客「成猿手册」 * @description: com.example.cache * @date: 2020/6/7 */public class User  implements Serializable {
private String userId; private String userName; private Integer age; //todo:此处省略get和set方法}

进行测试

这里写一个UserMain以测试上面我们所实现的代码效果:

package com.example.cache.springcache;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.ApplicationContext;/** * @author: 博客「成猿手册」 * @description: com.example.cache.customize * @date: 2020/6/7 */public class UserMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) context.getBean("userServiceBean"); //第一次查询,缓存中没有,从数据库查询 System.out.println("first query..."); System.out.println("result object: " + userService.getUserById("test001")); //第二次查询,缓存中存在,从缓存查询 System.out.println("second query..."); System.out.println("result object: " + userService.getUserById("test001")); }}

在控制台打印的相关信息如下:

first query...query user by userId=test001querying id from DB...test001Disconnected from the target VM, address: '127.0.0.1:12371', transport: 'socket'result object: com.example.cache.customize.User@6fb365edsecond query...result object: com.example.cache.customize.User@6fb365ed

可以看出querying id from DB的信息只在我们第一次查询时出现。到此上面配置的基于注解的缓存起作用了,而回顾代码会发现,在的代码中并没有任何属于缓存逻辑的代码,只是一个注解@Cacheable(cacheNames = "users")就实现了基本缓存方案,使得代码变得非常简洁。事实上,使用Spring Cache非常简单,只需要:

  • 缓存定义:确定需要缓存的方法和缓存策略。

  • 缓存配置:配置缓存。

转载地址:https://blog.csdn.net/qq_41429510/article/details/107041671 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Bootstrap-Modal模态框插件
下一篇:Spring Cache缓存注解

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年03月26日 14时33分30秒