SpringBoot与SpringCache概念用法大全
作者:#Hideonbush 发布时间:2022-05-24 01:35:44
标签:SpringBoot,SpringCache
1.SpringCache的概念
首先我们知道jpa,jdbc这些东西都是一些规范,比如jdbc,要要连接到数据库,都是需要用到数据库连接,预处理,结果集这三个对象,无论是连接到mysql还是oracle都是需要用到这个三个对象的,这是一种规范,而SpringCache是一种作为缓存的规范,具体实现有redis,EhCahe等
2.SpringCache用法(redis版)
2.1 .SpringCache基本用法
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yl</groupId>
<artifactId>cache_redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cache_redis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<artifactId>spring-boot-starter-data-redis</artifactId>
<artifactId>spring-boot-starter-web</artifactId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.application.properties
# redis的配置
spring.redis.host=192.168.244.135
spring.redis.port=6379
spring.redis.password=root123
3.实体类
package com.yl.cache_redis.domain;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
4.service
package com.yl.cache_redis;
import com.yl.cache_redis.domain.User;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Cacheable(cacheNames = "u1") //这个注解作用就是将方法的返回值存到缓存中
public User getUserById(Integer id) {
System.out.println("getUserById:" + id);
User user = new User();
user.setId(id);
user.setUsername("root");
user.setPassword("root");
return user;
}
}
5.主程序,加上开启缓存的注解
package com.yl.cache_redis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching //开启缓存功能
public class CacheRedisApplication {
public static void main(String[] args) {
SpringApplication.run(CacheRedisApplication.class, args);
}
}
6.测试
6.1)userservice没加@Cacheable注解时
6.2)userservice加@Cacheable注解后,发现sevice中的方法只调用了一次
6.3)在redis中也可以看到缓存中有数据,key为定义好的cacheNames+::+方法的参数
2.2 .SpringCache自定义缓存key
1.SpringCache默认使用cacheNames和方法中的参数结合组成key的,那么如果有多个参数呢?它又是如何组成key的呢?我们可以指定key吗?
2.如何自定义key呢?
1)自定义key
package com.yl.cache_redis;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Arrays;
@Component
public class MyKeyGenerator implements KeyGenerator {
@Override
public Object generate(Object target, Method method, Object... params) {
return target.toString() + ":" + method.getName() + ":" + Arrays.toString(params);
}
}
2)测试
2.3 .SpringCache更新缓存
1.使用@CachePut注解来更新,注意:@CachePut中的key要和@Cacheable中的key一样,否则更新不了!
2.4 .SpringCache清空缓存
1.使用@CacheEvict注解,主要key和要@Cacheable中的key一致
2.测试
2.5 .SpringCache其他用法
1.@Caching注解,可以组合多个注解
2.@CacheConfig注解
3.SpringCache用法(EhCache版)
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yl</groupId>
<artifactId>ehcache</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ehcache</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<artifactId>spring-boot-starter-web</artifactId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.6</version>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.实体类
package com.yl.ehcache.model;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
3.service
package com.yl.ehcache.service;
import com.yl.ehcache.model.User;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Cacheable(cacheNames = "user")
public User getUserById(Integer id) {
System.out.println("getUserById()...");
User user = new User();
user.setId(id);
user.setUsername("root");
user.setPassword("root");
return user;
}
@CacheEvict(cacheNames = "user")
public void delete(Integer id) {
System.out.println("delete");
}
4.主程序
package com.yl.ehcache;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class EhcacheApplication {
public static void main(String[] args) {
SpringApplication.run(EhcacheApplication.class, args);
}
}
5.ehcache.xml
<ehcache>
<diskStore path="java.io.tmpdir/shiro-spring-sample"/>
<defaultCache
maxElementsInMemory = "1000"
eternal = "false"
timeToIdleSeconds = "120"
timeToLiveSeconds = "120"
overflowToDisk = "false"
diskPersistent = "false"
diskExpiryThreadIntervalSeconds = "120"/>
<cache name = "user"
maxElementsInMemory = "1000"
eternal = "false"
overflowToDisk = "true"
diskPersistent = "true"
diskExpiryThreadIntervalSeconds = "600"/>
</ehcache>
6.测试
来源:https://blog.csdn.net/weixin_41359273/article/details/122823344
0
投稿
猜你喜欢
- 本文实例为大家分享了java实现登录窗口的具体代码,供大家参考,具体内容如下登录窗口主类package ccnu.paint;import
- LinkedListLinkedList是一种可以在任何位置进行高效地插入和删除操作的有序序列。它的最基本存储结构是一个节点:每个节点将存储
- 工厂模式Spring中bean的创建,默认是框架利用反射new出来的bean实例。有时候也会有一些复杂的情况。假设有一个飞机,属性如下,现在
- 所谓回调,就是客户程序C调用服务程序S中的某个方法A,然后S又在某个时候反过来调用C中的某个方法B,对于C来说,这个B便叫做回调方法。下面看
- 本文实例讲述了Java Swing实现窗体添加背景图片的2种方法。分享给大家供大家参考,具体如下:在美化程序时,常常需要在窗体上添加背景图片
- 目录1、成员2、辅助功能3、字段4、方法4.1参数4.2方法主体和局部变量4.3静态和实例方法4.4虚方法、重写方法和抽象方法4.5方法重载
- Java 爬虫工具Jsoup详解Jsoup是一款 Java 的 HTML 解析器,可直接解析某个 URL 地址、HTML 文本内
- 准备工作:import java.text.SimpleDateFormat;import java.util.Calendar;impor
- activity A和BA 获取数据的activity B返回数据的activity点击A上的按钮,在A的textview上显示
- Android自定义短信验证码组件,供大家参考,具体内容如下效果图1.布局实现因为要禁用光标,所以我用TextView代替了EditText
- 利用Java,在控制台操作下,编写的五子棋,作为复习二维数组,面向对象等基础知识。w表示白棋,b表示黑棋import java.util.S
- 前言在Android屏幕的空间中,大部分的区域我们都是可以随意绘制,只有一部分区域是显示的固定内容:状态栏标题栏(ActionBar)页面内
- 前言:什么是多数据源?最常见的单一应用中最多涉及到一个数据库,即是一个数据源(Datasource)。那么顾名思义,多数据源就是在一个单一应
- 在本篇介绍的Winform界面样式改变及存储操作中,是指基于DevExpress进行界面样式的变化。一般情况下,默认我们会为客户提供多种De
- 首先为大家介绍Andorid5.0原生下拉刷新简单实现。先上效果图;相对于上一个19.1.0版本中的横条效果好看了很多。使用起来也很简单。
- 十进制转二进制正整数转二进制除2取余,逆序排列,高位补零。示例:十进制255 = 二进制1111 1111计算过程:255/2=127===
- 自定义注解实现接口幂等性方式近期需要对接口进行幂等性的改造,特此记录下。背景在微服务架构中,幂等是一致性方面的一个重要概念。一个幂等操作的特
- 1.利用 “+”(加号)运算符:string str = “Hello”+ “World”; console.WriteLine(str);
- 引入假定我们有两个项目:Cup 和 Water,其中 Cup 表示主项目,而 Water 表示子模块项目。其中 Cup 的远程仓库地址为 g
- 一、获取程序集版本 程序代码 label版本.Text = System.Reflection.Assembly.GetExecutingA