Redis整合SpringBoot的RedisTemplate实现类(实例详解)

本文深入讲解如何使用SpringBoot中的RedisTemplate实现类整合Redis,并提供详实的实例分析。

SpringBoot整合RedisTemplate实现高效缓存操作:实例详解

概述

在当前互联网项目中,缓存技术已经成为提升系统性能的重要手段,Redis作为一款高性能的键值对存储系统,被广泛应用于缓存、分布式锁、消息队列等场景,Spring Boot作为一款流行的Java开发框架,其提供了丰富的自动配置和模板工具,简化了与Redis的整合过程,本文将通过一个实例来详细讲解如何在Spring Boot项目中使用RedisTemplate实现Redis缓存操作。

Redis整合SpringBoot的RedisTemplate实现类(实例详解)

环境准备

1、开发工具:IntelliJ IDEA

2、项目构建工具:Maven

3、Spring Boot版本:2.2.5.RELEASE

4、Redis版本:5.0.8

整合步骤

1、添加依赖

在pom.xml文件中添加Spring Boot Starter Data Redis依赖:

Redis整合SpringBoot的RedisTemplate实现类(实例详解)

<dependencies>
    <!-- Spring Boot Starter Data Redis -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <!-- 其他依赖 -->
</dependencies>

2、配置application.properties

在application.properties文件中配置Redis服务器信息:

Redis服务器地址
spring.redis.host=127.0.0.1
Redis服务器端口
spring.redis.port=6379
Redis密码(如果有的话)
spring.redis.password=
Redis数据库索引(默认为0)
spring.redis.database=0
连接池最大连接数(默认为8)
spring.redis.jedis.pool.max-active=8
连接池最大阻塞等待时间(默认为-1ms)
spring.redis.jedis.pool.max-wait=-1ms
连接池中的最大空闲连接(默认为8)
spring.redis.jedis.pool.max-idle=8
连接池中的最小空闲连接(默认为0)
spring.redis.jedis.pool.min-idle=0

3、创建RedisTemplate配置类

在项目中创建一个配置类,用于配置RedisTemplate:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        // 设置key的序列化策略
        template.setKeySerializer(new StringRedisSerializer());
        // 设置value的序列化策略
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        // 设置hash key和value的序列化策略
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        // 必须执行这个函数,初始化RedisTemplate
        template.afterPropertiesSet();
        return template;
    }
}

4、使用RedisTemplate进行缓存操作

在Service层,我们可以通过注入RedisTemplate来执行各种缓存操作:

Redis整合SpringBoot的RedisTemplate实现类(实例详解)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class CacheService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }
    public void delete(String key) {
        redisTemplate.delete(key);
    }
}

5、编写Controller进行测试

创建一个Controller,调用Service层的缓存操作方法进行测试:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CacheController {
    @Autowired
    private CacheService cacheService;
    @GetMapping("/set")
    public String set(String key, String value) {
        cacheService.set(key, value);
        return "success";
    }
    @GetMapping("/get")
    public String get(String key) {
        Object value = cacheService.get(key);
        return value != null ? value.toString() : "null";
    }
    @GetMapping("/delete")
    public String delete(String key) {
        cacheService.delete(key);
        return "success";
    }
}

通过以上步骤,我们已经成功地将Spring Boot与RedisTemplate进行整合,并实现了基本的缓存操作,RedisTemplate提供了丰富的API,包括字符串操作、列表操作、集合操作、有序集合操作等,可以满足各种场景下的缓存需求,在实际项目中,我们可以根据业务需求,灵活地使用RedisTemplate进行缓存设计和优化。

原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/323185.html

(0)
K-seoK-seoSEO优化员
上一篇 2024年2月18日 21:59
下一篇 2024年2月18日 22:17

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入