提交 3d303ad4 编写于 作者: E Evan

use redis to cache data

上级 01317381
......@@ -18,6 +18,7 @@ before_install:
sevices:
- mysql
- redis-server --port 6379
script:
- ./mvnw clean install -B
\ No newline at end of file
......@@ -76,7 +76,7 @@
## 数据库
1.MySQL
1.MySQL
2.Redis
# 部署方法
......@@ -95,7 +95,7 @@
数据库完整脚本 `wj.sql` 放在后端项目的 `src\main\resources` 目录下,也可根据需要自行在 MySQL 中执行数据库脚本。
Redis 端口为 6379(默认端口),密码为空。
运行项目前请启动 Redis 服务,端口为 6379(默认端口),密码为空。
3.数据库配置在后端项目的 `src\main\resources` 目录下的`application.properties` 文件中,mysql 版本为 8.0.15 。
......
package com.gm.wj.service;
import com.gm.wj.config.RedisConfig;
import com.gm.wj.dao.BookDAO;
import com.gm.wj.entity.Book;
import com.gm.wj.entity.Category;
import com.gm.wj.redis.RedisService;
import com.gm.wj.util.CastUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.processing.Processor;
import java.util.List;
import java.util.Timer;
/**
* @author Evan
......@@ -20,22 +20,44 @@ import java.util.List;
@Service
public class BookService {
@Autowired
BookDAO bookDAO;
private BookDAO bookDAO;
@Autowired
CategoryService categoryService;
private CategoryService categoryService;
@Autowired
private RedisService redisService;
// @Cacheable(value = RedisConfig.REDIS_KEY_DATABASE)
public List<Book> list() {
Sort sort = new Sort(Sort.Direction.DESC, "id");
return bookDAO.findAll(sort);
String key = "booklist";
List<Book> books = CastUtils.castList(redisService.get(key), Book.class);
if (books == null) {
Sort sort = new Sort(Sort.Direction.DESC, "id");
books = bookDAO.findAll(sort);
redisService.set(key, books);
}
return books;
}
public void addOrUpdate(Book book) {
redisService.delete("booklist");
bookDAO.save(book);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
redisService.delete("booklist");
}
public void deleteById(int id) {
redisService.delete("booklist");
bookDAO.deleteById(id);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
redisService.delete("booklist");
}
public List<Book> listByCategory(int cid) {
......
package com.gm.wj.util;
import java.util.ArrayList;
import java.util.List;
/**
* @author Evan
* @date 2020/5/31 19:04
*/
public class CastUtils {
public static <T> List<T> castList(Object obj, Class<T> clazz)
{
List<T> result = new ArrayList<T>();
if(obj instanceof List<?>)
{
for (Object o : (List<?>) obj)
{
result.add(clazz.cast(o));
}
return result;
}
return null;
}
}
......@@ -13,8 +13,8 @@ spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=none
# 打印 sql 语句
#spring.jpa.properties.hibernate.show_sql=true
#spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
## Hikari 连接池配置
## 最小空闲连接数量
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册