程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2024-11(1)

springboot-cache缓存和J2cache二级缓存框架(带点漫画)

发布于2021-05-30 19:52     阅读(1860)     评论(0)     点赞(15)     收藏(2)


引spring-boot-starter-cache漫画

针对热数据,一般我们会把他放到缓冲里,减轻数据库的压力,针对集群下的缓冲,一般我们会把缓冲放到redis里

在这里插入图片描述在这里插入图片描述

spring-boot-starter-cache项目整合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

在这里插入图片描述
redis里面已经有数据了
在这里插入图片描述
下次走helloCache接口时,从redis里拿取

demo地址

https://gitee.com/null_751_0808/spring-boot-demo
分支:origin/spring-boot2-cache-manager

在这里插入图片描述

引J2Cache漫画

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

J2Cache整合springboot-demo

项目结构

在这里插入图片描述

j2cache配置文件

j2cache.properties
这里不放出来了,主要是配置一级缓存是什么,二级缓存是什么,redis广播通知节点的方式,想看的可以去官网看,或者下载此demo看
我的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

在这里插入图片描述
在这里插入图片描述
从源码这里可以看到j2cache缓存
AbstractCacheInvoker doGet方法
在这里插入图片描述

demo地址

https://gitee.com/null_751_0808/spring-boot-demo/tree/spring-boot2-j2cache/
分支:origin/spring-boot2-j2cache

在这里插入图片描述

原理图

redis订阅与发布
在这里插入图片描述
在这里插入图片描述

二级缓存框架的连接

J2Cache
hotkey
redis6.0客户端缓存

参考连接

SpringBoot中Cache缓存的使用
@Cacheable缓存解决双冒号::问题

原文链接:https://blog.csdn.net/qq_42745404/article/details/117376963



所属网站分类: 技术文章 > 博客

作者:Nxnndn

链接:http://www.phpheidong.com/blog/article/86949/2b280adc5ddf93dd0d4b/

来源:php黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

15 0
收藏该文
已收藏

评论内容:(最多支持255个字符)