springboot+vue全栈开发实战pdf下载_spring+vue全栈开发实
战-第七。。。
1、redis单机缓存
Redis 单机缓存使⽤步骤如下:
创建项⽬,添加缓存依赖?
创建 Spring Boot 项⽬,添加 spring-boot-starter-cache 和 Redis 依赖,代码如下:
<!--redis单机缓存-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!--redis单机缓存end-->
缓存配置?
Red is 单机缓存只需要开发者在 application.properties 中进⾏ Redis 配置及缓存配置即可,代码:
#缓存配置
spring.cache.cache-names=c1,c2
dis.time-to-live=1800s
#Redis配置
开启缓存?
接下来在项 ⽬⼊⼝类中开启缓存,代码如下:
package controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class RediscacheApplication {
public static void main(String[] args) {
SpringApplication.run(RediscacheApplication.class, args);
}
}
创建 BookDao ?
创建 Book 实体类和 BookService
package Dao;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.*;
import t.annotation.Bean;
import org.dis.cache.RedisCacheManager; import org.springframework.stereotype.Service;
pto.KeyGenerator;
import Bean.Book;
@Service
@CacheConfig(cacheNames = "book_cache")
public class BookDao {
@Autowired
//    MyKeyGenerator myKeyGenerator;
//    @Cacheable(keyGenerator = "myKeyGenerator")
public Book getBookById(Integer id) {
System.out.println("getBookById");
Book book = new Book();
book.setId(id);
book.setName("三国演义");
book.setAuthor("罗贯中");
return book;
}
@CachePut(key = "#book.id")
public Book updateBookById(Book book) {
System.out.println("updateBookById");
book.setName("三国演义2");
return book;
}
@CacheEvict(key = "#id")
public void deleteBookById(Integer id) {
System.out.println("deleteBookById");
}
}
package Bean;
import java.io.Serializable;
public class Book implements Serializable {
private Integer id;
private String name;
private String author;
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + ''' +
", author='" + author + ''' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
cacheablepublic void setAuthor(String author) {
this.author = author;
}
}
创建测试类?
创建测试类, 对 Service 中的⽅法进⾏测试,代码如下:
package test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.st.context.SpringBootTest;
import st.context.junit4.SpringRunner;
import Dao.BookDao;
import Bean.Book;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CacheApplicationTests {
@Autowired
BookDao bookDao;
@Test
public void contextLoads() {
bookDao.deleteBookById(1);
Book b3 = BookById(1);
System.out.println("b3:"+b3);
Book b = new Book();
b.setName("三国演义");
b.setAuthor("罗贯中");
b.setId(1);
bookDao.updateBookById(b);
Book b4 = BookById(1);
System.out.println("b4:"+b4);
}
}
⼀开始执⾏了两个查询,但是查询⽅法只打印了⼀次,因为第⼆次使⽤了缓存。接下来执⾏ 了删除⽅法,删除⽅法执⾏完之后再次执⾏查询, 查询⽅法⼜被执⾏了,因为在删除⽅法中缓存⼰ 经被删除了 。再接下来执⾏更新⽅法,更新⽅法中不仅更新数据,也更新了缓存,所以在最后的查 询⽅法中, 查询⽅法⽇志没打印,说明该⽅法没执⾏,⽽是使⽤了缓存中的数据,⽽缓存中的数据 已经被更新了 。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。