首页 > 其他分享 >springboot reids缓存

springboot reids缓存

时间:2024-11-04 09:19:27浏览次数:5  
标签:缓存 springboot spring reids cache redis springframework import org

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 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.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.shrimpking</groupId>
    <artifactId>demo3</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo3</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-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.6.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

server.port=8089

#lettuce
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=2
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.shutdown-timeout=200ms

spring.cache.redis.cache-null-values=true
spring.cache.redis.time-to-live=600000
spring.cache.redis.use-key-prefix=true
spring.cache.type=redis
spring.cache.cache-names=articleCache
#关闭redis缓存
#spring.cache.type=none

article

package com.shrimpking.model;

import lombok.Data;

import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2024/1/27 17:09
 */
@Data
public class Article implements Serializable
{
    private long articleId;

    private String title;

    private String category;

    public Article(long articleId,String title,String category){
        this.articleId = articleId;
        this.title = title;
        this.category = category;
    }
}

redisconfig

package com.shrimpking.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;

import java.time.Duration;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2024/1/27 18:11
 */
@Configuration
@EnableCaching
@PropertySource("classpath:application.properties")
public class RedisConfig
{
    @Autowired
    private Environment environment;

    @Bean
    public LettuceConnectionFactory lettuceConnectionFactory(){
        RedisStandaloneConfiguration configuration
                = new RedisStandaloneConfiguration();
        configuration.setHostName(environment.getProperty("spring.redis.host"));
        configuration.setPort(Integer.parseInt(Objects.requireNonNull(environment.getProperty("spring.redis.port"))));
        configuration.setPassword(RedisPassword.of(environment.getProperty("spring.redis.password")));
        return new LettuceConnectionFactory(configuration);
    }

    @Bean
    public RedisCacheConfiguration redisCacheConfiguration(){
        RedisCacheConfiguration configuration
                = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(600))
                .disableCachingNullValues();
        return configuration;
    }

    @Bean
    public RedisCacheManager redisCacheManager(){
        RedisCacheManager manager = RedisCacheManager.builder(lettuceConnectionFactory())
                .cacheDefaults(redisCacheConfiguration())
                .transactionAware()
                .build();
        return manager;
    }
}

controller

package com.shrimpking.controller;

import com.shrimpking.model.Article;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2024/1/27 17:11
 */
@RestController
public class TestController
{



    @GetMapping("/getList")
    @Cacheable(value = "articleCache")
    public List<Article> getList(){
        System.out.println("get list");
        List<Article> articleList = new ArrayList<>();
        articleList.add(new Article(103,"title3","content3"));
        articleList.add(new Article(104,"title4","content4"));
        return articleList;
    }
}

标签:缓存,springboot,spring,reids,cache,redis,springframework,import,org
From: https://blog.csdn.net/modern358/article/details/135888775

相关文章