一、图片上传
请求接口:
思路:
1、后端用MultipartFile接收前端传过来的文件信息
2、用uuid将文件重命名,然后将文件以新文件名通过七牛云上传到七牛云服务器
二、导航--文章分类
请求接口:
思路:
1、直接从文章分类表里面查出全部的分类返回给前端即可
三、导航--文章标签
请求接口:
思路:
1、直接从标签表里面查出全部的标签返回给前端页面即可·
四、导航--文章归档
请求接口:
思路:
导航文章归档涉及到两个接口。查询内容那个接口就是首页列表的那个接口;左侧文章归档是文章归档那个接口,接下来讲解的就是这个接口
1、前端页面请求文章归档,我们需要自定义文章表的接口
2、将文章表的创建时间当做突破点,使用函数将创作时间分为年份和月份,然后再以年份和月份分组,用count(*)统计每组的文章数量,然后将年份、月份、每组的文章数量返回给前端页面
五、统一缓存处理
作用:可以加快请求的处理进度,我们把方法的返回结果存入redis中,下次再请求这个方法时直接返回结果加快了请求处理速度
思路:
1、老样子自定义缓存注解
/**
* @Description: 缓存注解
* @Author: 李传城
* @Date: 2022/11/4
* @Time: 9:42
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Cache {
long expire() default 1 * 60 * 1000;
String name() default "";
}
2、使用AOP实现缓存,在切面中绑定切入点和通知,切入点即自定义注解
/**
* @description: 缓存切面
* @author: 李传城
* @date: 2022-11-04 09:25
*/
@Aspect
@Component
@Slf4j
public class CacheAspect {
@Autowired
private RedisTemplate<String, String> redisTemplate;
//1、切入点
@Pointcut("@annotation(com.mszlu.blog.common.cache.Cache)")
public void pt(){}
//2、通知 通知中缓存方法结果
@Around("pt()")
public Object around(ProceedingJoinPoint pjp){
try {
Signature signature = pjp.getSignature();
//类名
String className = pjp.getTarget().getClass().getSimpleName();
//调用的方法名
String methodName = signature.getName();
Class[] parameterTypes = new Class[pjp.getArgs().length];
Object[] args = pjp.getArgs();
//参数
String params = "";
for(int i=0; i<args.length; i++) {
if(args[i] != null) {
params += JSON.toJSONString(args[i]);
parameterTypes[i] = args[i].getClass();
}else {
parameterTypes[i] = null;
}
}
if (StringUtils.isNotEmpty(params)) {
//加密 以防出现key过长以及字符转义获取不到的情况
params = DigestUtils.md5Hex(params);
}
Method method = pjp.getSignature().getDeclaringType().getMethod(methodName, parameterTypes);
//获取Cache注解
Cache annotation = method.getAnnotation(Cache.class);
//缓存过期时间
long expire = annotation.expire();
//缓存名称
String name = annotation.name();
//先从redis获取
String redisKey = name + "::" + className+"::"+methodName+"::"+params;
String redisValue = redisTemplate.opsForValue().get(redisKey);
if (StringUtils.isNotEmpty(redisValue)){
log.info("走了缓存~~~,{},{}",className,methodName);
return JSON.parseObject(redisValue, Result.class);
}
Object proceed = pjp.proceed();
redisTemplate.opsForValue().set(redisKey,JSON.toJSONString(proceed), Duration.ofMillis(expire));
log.info("存入缓存~~~ {},{}",className,methodName);
return proceed;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return Result.fail(-999,"系统错误");
}
}
3、在想使用缓存的方法上,添加自定义注解即可实现缓存