天天看点

springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接

文章目录

  • 引spring-boot-starter-cache漫画
  • spring-boot-starter-cache项目整合demo
    • 项目结构
    • pom.xml
    • RedisConfig.java 配置好对应缓存对应的配置
    • HelloRespDTO.java
    • HelloService.java
    • HelloController.java
    • DemoApplication.java 启动类
    • 效果展示
    • demo地址
  • 引J2Cache漫画
  • J2Cache整合springboot-demo
    • 项目结构
    • j2cache配置文件
    • HelloService.java
    • HelloRespDTO.java
    • HelloController.java
    • DemoApplication.java
    • 效果展示
    • demo地址
    • 原理图
    • 二级缓存框架的连接
  • 参考连接

引spring-boot-starter-cache漫画

针对热数据,一般我们会把他放到缓冲里,减轻数据库的压力,针对集群下的缓冲,一般我们会把缓冲放到redis里
springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接
springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接

spring-boot-starter-cache项目整合demo

项目结构

springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
    </dependencies>
</project>
           

RedisConfig.java 配置好对应缓存对应的配置

package com.example.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

@EnableCaching
@Configuration
public class RedisConfig {
    @Autowired
    RedisConnectionFactory redisConnectionFactory;
    @Bean
    public CacheManager cacheManager() {
        // 设置缓存有效期1小时
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofHours(1))
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer((new StringRedisSerializer())))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer((new GenericJackson2JsonRedisSerializer())))
                .computePrefixWith((name)->name+":");
        return RedisCacheManager
                .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
                .cacheDefaults(redisCacheConfiguration).build();
    }
}
           

HelloRespDTO.java

package com.example.demo.dto;

import java.io.Serializable;

public class HelloRespDTO implements Serializable {

    private String name;

    private String address;

    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
           

HelloService.java

package com.example.demo.service;

import com.example.demo.dto.HelloRespDTO;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class HelloService {

    @Cacheable(cacheNames = "employee",key = "'detail'+#id")
    public HelloRespDTO helloCache(String id){
        HelloRespDTO hello = new HelloRespDTO();
        hello.setName("ben");
        hello.setAddress("广州市白云区");
        hello.setAge(18);
        return hello;
    }

    @CacheEvict(value = "employee",key = "'detail'+#id")
    public HelloRespDTO helloClear(String id){
        HelloRespDTO hello = new HelloRespDTO();
        hello.setName("ben");
        hello.setAddress("广州市白云区");
        hello.setAge(18);
        return hello;
    }
}
           

HelloController.java

package com.example.demo.controller;

import com.example.demo.dto.HelloRespDTO;
import com.example.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public HelloRespDTO hello(){
        String id="1";
        HelloRespDTO helloRespDTO = helloService.helloCache(id);
        return helloRespDTO;
    }

    @GetMapping("/hello2")
    public HelloRespDTO helloClear(){
        String id="1";
        HelloRespDTO helloRespDTO = helloService.helloClear(id);
        return helloRespDTO;
    }
}
           

DemoApplication.java 启动类

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

效果展示

访问网址:

http://localhost:8889/hello/hello
           
springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接

redis里面已经有数据了

springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接

下次走helloCache接口时,从redis里拿取

demo地址

https://gitee.com/null_751_0808/spring-boot-demo
分支:origin/spring-boot2-cache-manager
           
springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接

引J2Cache漫画

springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接
springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接
springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接

J2Cache整合springboot-demo

项目结构

springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接

j2cache配置文件

j2cache.properties

这里不放出来了,主要是配置一级缓存是什么,二级缓存是什么,redis广播通知节点的方式,想看的可以去官网看,或者下载此demo看

我的demo配置

springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接

一级缓存用了caffeine,二级缓存用了lettuce(连接redis的)

application.yml 配置对应j2cache

server:
  port: 8889
  servlet:
    context-path: /hello
spring:
  application:
    name: hello
  redis:
    host: 127.0.0.1
    port: 6379
    password:
    database: 2
  cache:
    type: none
j2cache:
  config-location: /j2cache.properties
  redis-client: lettuce
  open-spring-cache: true
           

caffeine.properties

#########################################
# Caffeine configuration
# [name] = size, xxxx[s|m|h|d]
#########################################

default = 1000, 30m 
           

HelloService.java

package com.example.demo.service;

import com.example.demo.dto.HelloRespDTO;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class HelloService {

    //加缓存
    @Cacheable(value="employee",key = "'detail'+#id")
    public HelloRespDTO helloCache(String id){
        HelloRespDTO hello = new HelloRespDTO();
        hello.setName("ben");
        hello.setAddress("广州市白云区");
        hello.setAge(18);
        return hello;
    }

    //清缓存
    @CacheEvict(value="employee",key = "'detail'+#id")
    public HelloRespDTO helloClear(String id){
        HelloRespDTO hello = new HelloRespDTO();
        hello.setName("ben");
        hello.setAddress("广州市白云区");
        hello.setAge(18);
        return hello;
    }
}

           

HelloRespDTO.java

package com.example.demo.dto;


import java.io.Serializable;

public class HelloRespDTO implements Serializable {

    private String name;

    private String address;

    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

           

HelloController.java

package com.example.demo.controller;

import com.example.demo.dto.HelloRespDTO;
import com.example.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public HelloRespDTO hello(){
        String id="1";
        HelloRespDTO helloRespDTO = helloService.helloCache(id);
        return helloRespDTO;
    }

    @GetMapping("/hello2")
    public HelloRespDTO helloClear(){
        String id="1";
        HelloRespDTO helloRespDTO = helloService.helloClear(id);
        return helloRespDTO;
    }

}

           

DemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

效果展示

访问

http://localhost:8889/hello/hello
           
springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接
springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接

从源码这里可以看到j2cache缓存

AbstractCacheInvoker doGet方法

springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接

demo地址

https://gitee.com/null_751_0808/spring-boot-demo/tree/spring-boot2-j2cache/
分支:origin/spring-boot2-j2cache
           
springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接

原理图

redis订阅与发布

springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接
springboot-cache缓存和J2cache二级缓存框架(带点漫画)引spring-boot-starter-cache漫画spring-boot-starter-cache项目整合demo引J2Cache漫画J2Cache整合springboot-demo参考连接

二级缓存框架的连接

J2Cache

hotkey

redis6.0客户端缓存

参考连接

SpringBoot中Cache缓存的使用

@Cacheable缓存解决双冒号::问题

继续阅读