zoukankan      html  css  js  c++  java
  • 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的方式编写的代码量可能会更多。

  • 相关阅读:
    Java 并发性和多线程
    Java多线程整理
    线程死锁问题
    随机生成长度为len的密码,且包括大写、小写英文字母和数字
    ConcurrentHashMap原理分析
    并发 并行 同步 异步 多线程的区别
    Android与javaScript的交互
    Android6.0 新特性详解
    Android 6.0 新功能及主要 API 变更
    安装 Python-Client
  • 原文地址:https://www.cnblogs.com/my-program-life/p/12067789.html
Copyright © 2011-2022 走看看