SpringBoot分布式缓存

207 阅读1分钟

SpringBoot分布式缓存实现步骤

安装Redis

省略

启动redis服务

省略

打开RedisDesktopManager 如下图:

创建SpringBoot项目

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>springcloud</groupId>
    <artifactId>a_05_rediscache</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
    </parent>
   
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

    </dependencies>

</project>

yml配置

spring:
  cache:
    type: redis
  redis:
    database: 0
    host: localhost
    port: 6379
    password:
    jedis:
      pool:
        max-active: 8

启动程序

package com;

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

@SpringBootApplication
@EnableCaching
public class CacheApp {
    public static void main(String[] args) {
        SpringApplication.run(CacheApp.class);
    }
}

service 缓存注解的使用

package  com.service;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
//@CacheConfig(cacheNames = "cachename1")
public class UserService {

    @Cacheable("map")
    public Map getMap(){
        HashMap<Object, Object> map = new HashMap<>();
        map.put("id",1);
        map.put("username","张三");
        map.put("password","123456");
        System.out.println("------------------");
        return map;
    }


    @CacheEvict("map")
    public void deleteMap(){
        System.out.println("delete");
    }




    @Cacheable("#id")
    public Map  getMap1(int id){
        HashMap<Object, Object> map = new HashMap<>();
        map.put("id",1);
        map.put("username","张三");
        map.put("password","123456");
        System.out.println("------------------");
        return map;
    }

    @CacheEvict("#id")
    public void deleteMap1(int id){
        System.out.println("delete");
    }



    @Cacheable("#id")
    public List<Map> getMaps(int id) {
        HashMap<Object, Object> map1 = new HashMap<>();
        map1.put("id",1);
        map1.put("username","张三");
        map1.put("password","123456");


        HashMap<Object, Object> map2 = new HashMap<>();
        map2.put("id",1);
        map2.put("username","张三");
        map2.put("password","123456");

        List<Map> list = new ArrayList<>();
        list.add(map1);
        list.add(map2);

        System.out.println("list added");
        return  list;
    }

    @CacheEvict("#id")
    public void deleteMaps(int id){
        System.out.println("delete");
    }





    /************************************************************/
    @Cacheable("#id")
    public List<Map> getMaps1(int id) {
        HashMap<Object, Object> map1 = new HashMap<>();
        map1.put("id",1);
        map1.put("username","张三");
        map1.put("password","123456");


        HashMap<Object, Object> map2 = new HashMap<>();
        map2.put("id",1);
        map2.put("username","张三");
        map2.put("password","123456");

        List<Map> list = new ArrayList<>();
        list.add(map1);
        list.add(map2);

        System.out.println("list added");
        return  list;
    }

    @CacheEvict("#id")
    public void deleteMaps1(int id){
        System.out.println("delete");
    }
}

controller 编写

package com.controller;

import com.service.UserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Resource
     UserService userService;



    @RequestMapping("/getMap")
    public Map getMap(){
        return  userService.getMap();
    }


    @RequestMapping("/deleteMap")
    public void deleteMap(){
         userService.deleteMap();
    }




    @RequestMapping("/getMap1")
    public Map  getMap1(int id){

        return  userService.getMap1(id);
    }

    @RequestMapping("/deleteMap1")
    public void deleteMap1(int id){
        userService.deleteMap1(id);
    }



    @RequestMapping("/getMaps")
    public List<Map> getMaps(int id) {

        return  userService.getMaps(id);
    }

    @RequestMapping("/deleteMaps")
    public void deleteMaps(int id){
       userService.deleteMaps(id);
    }

    @RequestMapping("/getMaps1")
    public List<Map> getMaps1(int id) {

        return  userService.getMaps1(id);
    }

    @RequestMapping("/deleteMaps1")
    public void deleteMaps1(int id){
       userService.deleteMaps1(id);
    }
}

运行CacheApp启动服务

浏览器输入: http://localhost:8080/getMap 访问 刷新多次观看 控制台输出效果,如果发现只能输出一次,就说明缓存发挥作用 浏览器输入: http://localhost:8080/deleteMap 清空缓存 再一次 浏览器输入: http://localhost:8080/getMap 访问 看看效果

以下几个方法测试省略。get就是加入缓存,delete就是删除缓存