• Spring Boot基于API的Redis缓存实现


    本篇随笔基于https://www.cnblogs.com/my-program-life/p/12067656.html 实现

    一、使用Redis API进行业务数据缓存管理

    在service下新建ApiCommentService,删除原有的CommentService

    package com.uos.cache.service;
    
    
    import com.uos.cache.domain.Comment;
    import com.uos.cache.repository.CommentRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Service;
    
    import java.util.Optional;
    import java.util.concurrent.TimeUnit;
    
    @Service
    public class ApiCommentService {
        @Autowired
        private RedisTemplate redisTemplate;
        @Autowired
        private CommentRepository commentRepository;
        public Comment findById(int comment_id){
            Object object =  redisTemplate.opsForValue().get("comment_"+comment_id);
            if (object!=null){
                return (Comment)object;}else {
                Optional<Comment> optional = commentRepository.findById(comment_id);
                if(optional.isPresent()){Comment comment= optional.get();
                    redisTemplate.opsForValue().set("comment_"+comment_id,
                            comment,1, TimeUnit.DAYS);return comment;
                }else {return null;}
            }
        }
        public Comment updateComment(Comment comment){
            commentRepository.updateComment(comment.getAuthor(), comment.getaId());
            redisTemplate.opsForValue().set("comment_"+comment.getId(),comment);
            return comment;
        }
        public void deleteComment(int comment_id){
            commentRepository.deleteById(comment_id);
            redisTemplate.delete("comment_"+comment_id);
        }
    
    }
    ApiCommentService

    二、在controller层下新建ApiCommentController

    删除原有的CommentController

    package com.uos.cache.controller;
    
    import com.uos.cache.domain.Comment;
    import com.uos.cache.service.ApiCommentService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/api")
    public class ApiCommentController {
        @Autowired
        private ApiCommentService apiCommentService;
        @GetMapping("/get/{id}")
        public Comment findById(@PathVariable("id") int comment_id){
            Comment comment = apiCommentService.findById(comment_id);
            return comment;
        }
        @GetMapping("/update/{id}/{author}")
        public Comment updateComment(@PathVariable("id") int comment_id,
                                     @PathVariable("author") String author){
            Comment comment = apiCommentService.findById(comment_id);
            comment.setAuthor(author);
            Comment updateComment = apiCommentService.updateComment(comment);
            return updateComment;
        }
        @GetMapping("/delete/{id}")
        public void deleteComment(@PathVariable("id") int comment_id){
            apiCommentService.deleteComment(comment_id);
        }
    
    }
    ApiCommentController

    三、主程序类

    四、效果测试

    查询测试

    更新测试

     删除测试

          相对使用注解的方式,使用Redis API进行数据缓存管理更加灵活,例如,

    手机验证码进行验证时,可以在缓存中设置验证等待时间。

         相比使用注解的方式进行缓存管理,使用Redis API的方式编写的代码量可能会更多。

  • 相关阅读:
    js全局函数
    几个数拼接生成最大数(java实现)
    数独求解算法(回溯法和唯一解法)java实现
    32位机和64位机下面各类型sizeof的大小
    【经典算法】——KMP,深入讲解next数组的求解
    java 初始化及执行顺序
    CentOS7用yum安装、配置MariaDB 10
    centos7 删除mysql
    Linux下配置两个或多个Tomcat启动
    Linux平台上搭建apache+tomcat负载均衡集群[一台服务器多tomcat集群模式]
  • 原文地址:https://www.cnblogs.com/my-program-life/p/12067789.html
Copyright © 2020-2023  润新知