SpringBoot学习之缓存

本文介绍了如何在Spring Boot应用中使用Redis作为缓存,并通过`@Cacheable`注解实现数据缓存。从配置依赖、模型类定义到Service中的方法缓存,再到单元测试验证,一步步教你搭建缓存体系。
摘要由CSDN通过智能技术生成

导入依赖

<dependencies>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

配置application.properties文件

spring.redis.host=192.168.65.128
spring.redis.port=6379
spring.redis.password=hx

在model包下创建User类

package org.hx.springboot_cache_demo42.model;

import java.io.Serializable;

/**
 * @Description: TODO
 * @author: hx
 * @date: 2021年04月12日 20:45
 */
public class User implements Serializable {
    private Integer id;
    private String username;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

在Service包下创建UserService类

package org.hx.springboot_cache_demo42.Service;

import org.hx.springboot_cache_demo42.model.User;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @Description: TODO
 * @author: hx
 * @date: 2021年04月12日 20:46
 */
@Service
public class UserService {
    @Cacheable(cacheNames = "c2")
    //@Cacheable标签:标记在方法上,表示该方法的返回结果需要缓存,
    // 默认情况下,方法的参数将座位缓存的key
    public User getUserById(Integer id){
        System.out.println("getUserById: "+id);
        User user = new User();
        user.setId(id);
        user.setUsername("hx");
        return user;
    }
}

在启动类中添加@EnableCaching标签,开启缓存功能

package org.hx.springboot_cache_demo42;

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

@SpringBootApplication
@EnableCaching //开启缓存功能
public class SpringbootCacheDemo42Application {

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

}

在单元测试类中进行测试

package org.hx.springboot_cache_demo42;

import org.hx.springboot_cache_demo42.Service.UserService;
import org.hx.springboot_cache_demo42.model.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootCacheDemo42ApplicationTests {
    @Autowired
    UserService userService;
    @Test
    void contextLoads() {
        for (int i = 0; i < 3; i++) {
            User u =userService.getUserById(99);
            System.out.println("u = "+u);
        }
    }

}

当redis缓存中没有当前数据时,进行数据添加,如果redis缓存中存在当前数据,则直接读取。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值