mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-07-24 02:09:35 +08:00
线上课缓存
This commit is contained in:
parent
efb3a69498
commit
ee248b483d
42
src/main/java/xyz/playedu/api/caches/CourseCache.java
Normal file
42
src/main/java/xyz/playedu/api/caches/CourseCache.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package xyz.playedu.api.caches;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import xyz.playedu.api.domain.Course;
|
||||||
|
import xyz.playedu.api.exception.NotFoundException;
|
||||||
|
import xyz.playedu.api.service.CourseService;
|
||||||
|
import xyz.playedu.api.util.RedisUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author 杭州白书科技有限公司
|
||||||
|
* @create 2023/3/20 17:57
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class CourseCache {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CourseService courseService;
|
||||||
|
|
||||||
|
private final static String keyTemplate = "course:%d";
|
||||||
|
|
||||||
|
private final static int expire = 3600;//s
|
||||||
|
|
||||||
|
public Course findOrFail(Integer id) throws NotFoundException {
|
||||||
|
String keyName = key(id);
|
||||||
|
if (RedisUtil.exists(keyName)) {
|
||||||
|
return (Course) RedisUtil.get(keyName);
|
||||||
|
}
|
||||||
|
Course course = courseService.findOrFail(id);
|
||||||
|
put(course);
|
||||||
|
return course;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void put(Course course) {
|
||||||
|
RedisUtil.set(key(course.getId()), course, expire);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String key(Integer courseId) {
|
||||||
|
return String.format(keyTemplate, courseId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -4,6 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import xyz.playedu.api.PlayEduFCtx;
|
import xyz.playedu.api.PlayEduFCtx;
|
||||||
|
import xyz.playedu.api.caches.CourseCache;
|
||||||
import xyz.playedu.api.caches.UserCanSeeCourseCache;
|
import xyz.playedu.api.caches.UserCanSeeCourseCache;
|
||||||
import xyz.playedu.api.domain.*;
|
import xyz.playedu.api.domain.*;
|
||||||
import xyz.playedu.api.exception.NotFoundException;
|
import xyz.playedu.api.exception.NotFoundException;
|
||||||
@ -31,18 +32,21 @@ public class HourController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private CourseHourService hourService;
|
private CourseHourService hourService;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private UserCanSeeCourseCache userCanSeeCourseCache;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ResourceService resourceService;
|
private ResourceService resourceService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserCourseHourRecordService userCourseHourRecordService;
|
private UserCourseHourRecordService userCourseHourRecordService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserCanSeeCourseCache userCanSeeCourseCache;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CourseCache courseCache;
|
||||||
|
|
||||||
@GetMapping("/{id}/play")
|
@GetMapping("/{id}/play")
|
||||||
public JsonResponse play(@PathVariable(name = "courseId") Integer courseId, @PathVariable(name = "id") Integer id) throws NotFoundException, ServiceException {
|
public JsonResponse play(@PathVariable(name = "courseId") Integer courseId, @PathVariable(name = "id") Integer id) throws NotFoundException, ServiceException {
|
||||||
Course course = courseService.findOrFail(courseId);
|
Course course = courseCache.findOrFail(courseId);
|
||||||
userCanSeeCourseCache.check(PlayEduFCtx.getUser(), course, true);
|
userCanSeeCourseCache.check(PlayEduFCtx.getUser(), course, true);
|
||||||
CourseHour hour = hourService.findOrFail(id, courseId);
|
CourseHour hour = hourService.findOrFail(id, courseId);
|
||||||
Resource resource = resourceService.findOrFail(hour.getRid());
|
Resource resource = resourceService.findOrFail(hour.getRid());
|
||||||
@ -63,7 +67,7 @@ public class HourController {
|
|||||||
}
|
}
|
||||||
User user = PlayEduFCtx.getUser();
|
User user = PlayEduFCtx.getUser();
|
||||||
// 线上课检测
|
// 线上课检测
|
||||||
Course course = courseService.findOrFail(courseId);
|
Course course = courseCache.findOrFail(courseId);
|
||||||
// 权限校验
|
// 权限校验
|
||||||
userCanSeeCourseCache.check(user, course, true);
|
userCanSeeCourseCache.check(user, course, true);
|
||||||
// 课时检测
|
// 课时检测
|
||||||
@ -76,7 +80,7 @@ public class HourController {
|
|||||||
|
|
||||||
@PostMapping("/{id}/ping")
|
@PostMapping("/{id}/ping")
|
||||||
public JsonResponse ping(@PathVariable(name = "courseId") Integer courseId, @PathVariable(name = "id") Integer id) throws NotFoundException, ServiceException {
|
public JsonResponse ping(@PathVariable(name = "courseId") Integer courseId, @PathVariable(name = "id") Integer id) throws NotFoundException, ServiceException {
|
||||||
Course course = courseService.findOrFail(courseId);
|
Course course = courseCache.findOrFail(courseId);
|
||||||
userCanSeeCourseCache.check(PlayEduFCtx.getUser(), course, true);
|
userCanSeeCourseCache.check(PlayEduFCtx.getUser(), course, true);
|
||||||
return JsonResponse.success();
|
return JsonResponse.success();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user