当前位置: 首页 > news >正文

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 {@Beanpublic ConcurrentMapCacheManager cacheManager() {// 创建一个默认的 ConcurrentMapCacheManagerreturn new ConcurrentMapCacheManager("defaultCache"); // 可以指定一个或多个默认缓存名}@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory);// 设置key和value的序列化方式template.setKeySerializer(new StringRedisSerializer()); // 设置key的序列化器为StringRedisSerializertemplate.setValueSerializer(new JdkSerializationRedisSerializer()); // 设置value的序列化器为JdkSerializationRedisSerializertemplate.setHashKeySerializer(new StringRedisSerializer()); // 设置hash key的序列化器为StringRedisSerializertemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); // 设置hash value的序列化器为JdkSerializationRedisSerializertemplate.afterPropertiesSet(); // 初始化RedisTemplatereturn template;}
}

3.修改配置 application.yml

spring:#redisdata:redis:host: 127.0.0.1port: 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")@ResponseBodypublic 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")@ResponseBodypublic 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


http://www.mrgr.cn/news/79143.html

相关文章:

  • 工业—使用Flink处理Kafka中的数据_ProduceRecord1
  • 调试android 指纹遇到的坑
  • 右值引用和完美转发【C++11】
  • flask简易版的后端服务创建接口(python)
  • 【时间序列预测】基于Pytorch实现CNN_LSTM算法
  • 典型的调度算法--短作业优先调度算法
  • 写译热点单词
  • STM32 I2C案例2:硬件实现I2C 代码书写
  • 【Linux---10】本地机器 <=> 服务器 文件互传
  • 工业—使用Flink处理Kafka中的数据_ProduceRecord2
  • 【RDMA】RDMA read和write编程实例(verbs API)
  • React第十一节 组件之间通讯之发布订阅模式(自定义发布订阅器)
  • 微信小程序横滑定位元素案例代码
  • 【go】select 语句case的随机性
  • Python矩阵并行计算;CuPy-CUDA 实现显存加速:;在Python中实现显存加速或卸载;CuPy 和 NumPy 区别
  • compose组件库
  • java调用cmdsh命令
  • 流媒体之linux下离线部署FFmpeg 和 SRS
  • MongoDB集群的介绍与搭建
  • 【测试工具JMeter篇】JMeter性能测试入门级教程(七):JMeter断言