保存Redis

第一步:启动类中加入注解 @EnableCaching

package com.payease;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
//@MapperScan(basePackages = "com.payease.dataobject.mapper")
@EnableCaching
public class SellApplication {

    public static void main(String[] args) {
        SpringApplication.run(SellApplication.class, args);
    }
}

 第二步:在对应的controller中的方法中加入 @Cacheable()

springboot项目:Redis缓存使用

第三步:对该方法的返回值 ResultVO 类中实现Serializable 

注:需要安装插件 1.快捷键   command +  , 

         2.点击Plugins 搜索 serializable

springboot项目:Redis缓存使用

     3.安装 插件 重启idea

springboot项目:Redis缓存使用

 springboot项目:Redis缓存使用

  4.为插件设置快捷键

     a.左键点击屏幕左上角: IntelliJ IDEA

     b.点击选项菜单:Preferences 打开设置对话框

     c.在左侧的导航框中点击: KeyMap

    springboot项目:Redis缓存使用

    d.在红框内右键选择 springboot项目:Redis缓存使用

    e.设置快捷键 shift + control + i 点击OK springboot项目:Redis缓存使用

    springboot项目:Redis缓存使用

    

package com.payease.VO;

import lombok.Data;

import java.io.Serializable;

/**
 * http请求返回最外层对象
 * Created by liuxiaoming on 2017/11/11.
 */
@Data
//@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultVO<T> implements Serializable{

    private static final long serialVersionUID = 816318696842351963L;

/** 错误码 */
    private Integer code;

    /** 提示信息 */
    private String msg;

    /** 具体内容 */
    private T data;
}

 

第四步:启动项目 在浏览器中输入:127.0.0.1:8080/sell/buyer/product/list

第五步:打开rdm  查看 此时调用controller的方法中的返回值resultVO已经被序列化保存到Redis服务器中

springboot项目:Redis缓存使用

 更新Redis(对数据库数据进行更新操作后):

两种方式:

  第一种: @CachePut()更新缓存  需要将返回值序列化

  第二种: @CacheEvict() 清除缓存

springboot项目:Redis缓存使用

 注:rdm 手动清除缓存

  1.打开控制台

  springboot项目:Redis缓存使用

  2.输入命令 flushdb

  springboot项目:Redis缓存使用

注:Redis中的 @CachePut()的调用

 第一种:

springboot项目:Redis缓存使用

第二种:

springboot项目:Redis缓存使用

扩展:

springboot项目:Redis缓存使用

在浏览器中输入请求:http://127.0.0.1:8080/sell/buyer/product/list?sellerId=1234

 

 总结:

springboot项目:Redis缓存使用

 

分类:

技术点:

相关文章: