Springboot3整合Redis

书接上篇《Redis 安装篇(阿里云服务器)_阿里云安装redis-CSDN博客》,安装好Redis后,就需要在springboot项目中使用Redis了。

一、SpringBoot整合Redis

1.添加坐标

<!--redis-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.增加配置 RedisConfig

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
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.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@EnableCaching
@Configuration
public class RedisConfig {
    @Bean
    public ConcurrentMapCacheManager cacheManager() {
        // 创建一个默认的 ConcurrentMapCacheManager
        return new ConcurrentMapCacheManager("defaultCache"); // 可以指定一个或多个默认缓存名
    }
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        // 设置key和value的序列化方式
        template.setKeySerializer(new StringRedisSerializer()); // 设置key的序列化器为StringRedisSerializer
        template.setValueSerializer(new JdkSerializationRedisSerializer()); // 设置value的序列化器为JdkSerializationRedisSerializer
        template.setHashKeySerializer(new StringRedisSerializer()); // 设置hash key的序列化器为StringRedisSerializer
        template.setHashValueSerializer(new JdkSerializationRedisSerializer()); // 设置hash value的序列化器为JdkSerializationRedisSerializer
        template.afterPropertiesSet(); // 初始化RedisTemplate

        return template;
    }
}

3.修改配置 application.yml

spring:
  #redis
  data:
    redis:
      host: 127.0.0.1
      port: 6379   #端口
      database: 0  # 使用的数据库编号
      password: "******" #Redis密码
      lettuce: #Lettuce客户端配置
        pool: # 连接池配置
          max-active: 8  # 最大活跃连接数
          max-wait: -1  # 最大等待时间(-1表示无限等待)
          max-idle: 8  # 最大空闲连接数
          min-idle: 0  # 最小空闲连接数

注意:host,port,password 请根据实际情况进行修改。建议使用客户端先测试能否正常链接。

客户端可以使用Another Redis Desktop Manager | 更快、更好、更稳定的Redis桌面(GUI)管理客户端,兼容Windows、Mac、Linux,性能出众,轻松加载海量键值

 下载地址:AnotherRedisDesktopManager 发行版 - Gitee.com

4.编写java代码进行调用

@Controller
@RequestMapping("/")
@RequiredArgsConstructor
public class HomeController {
    
    private final StringRedisTemplate stringRedisTemplate;

    @Operation(summary = "redis_set", description = "")
    @GetMapping("/redis_set")
    @ResponseBody
    public String redis_set(HttpServletRequest request,
                            @RequestParam("key") String key,
                            @RequestParam("value") String value,
                            @RequestParam("min") Integer min) {

        stringRedisTemplate.opsForValue().set(key, value, min, TimeUnit.MINUTES);
        return "设置成功,请访问 <a href='/redis_get?key=" + key + "'>此处</a> 进行测试";
    }

    @Operation(summary = "redis_get", description = "")
    @GetMapping("/redis_get")
    @ResponseBody
    public String redis_get(@RequestParam("key") String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }
}

5.运行效果

二、Redis的使用 

Redis存储的是key-value结构的数据,其中key是字符串类型,value有5种常见的数据类型:

1.字符串-string

2.哈希表-hash

3.列表-list

4.集合-set

5.有序集合-sorted set/zset

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值