SpringBoot利用spring-boot-starter-data-redis整合Redis

技术标签: javaee

Redis是一种高性能的非关系型数据库。redis作用在内存,性能极高。SpringBoot同样可以把Redis整合到项目里。

首先,第一步就是为项目添加Redis依赖。在SpringBoot下有spring-boot-starter-data-redis,使用Redis就相当的简单。

 

第二步添加上Redis配置信息。包括Redis服务器的IP、端口、密码等信息,前提是已经安装好Redis服务,密码等信息必须和服务器一致。 

 

第三步,接下来就要写例子来使用使用Redis,在这个例子中使用的是框架封装Redis的类StringRedisTemplate。为啥用这个呢?,因为例子设计的k-v都是String类型,官方建议符合这种格式的推荐使用 StringRedisTemplate。当k-v是非String类型的object时,推荐使用RedisTemplate。 

第四步测试下结果,按照controller里写的路径请求下得到employee里设置的参数。

 

这样看不出redis有没有写入内存,使用redis下的redis-cli可以访问redis数据。这种方式访问数据的时候展示不是很友好。推荐使用RedisDesktopManager,这个图形界面工具可是比命令行好使。 

 

这个controller里的redisTemplete.opsForValue().set("redis--3","redis cache 1")很麻烦,可以再封装一层,这个用起来也省事,这一长串尾巴看起来就不爽。

 

逐步替换下看起来就爽多了。千万不要忘记注册组件@Component。 

来源:网络


智能推荐

springboot整合redis

redis是目前市面上相对用的最多kv数据库,一般都是用来做缓存作用。那么springboot如何整合redis呢? 1、引入maven依赖,我这里用的是默认版本,具体用哪个版本根据个人需要 2、配置yml文件: 3、就是编写我们的redis工具类了,redis是支持很多种数据存储的opsForValue是String,我们一般都是存String的,还有opsForSet,opsForHash,o...

SpringBoot 整合 Redis 集群

SpringBoot 整合 Redis 集群 文章目录 创建 Redis 集群 打开 Redis 远程访问 添加依赖 配置 Redis 集群连接 测试连接 创建 Redis 集群 详细操作请参考《Redis 集群配置》 打开 Redis 远程访问 详细操作参考《Redis 远程连接》 添加依赖 在 pom 文件中添加 Redis 依赖 配置 Redis 集群连接 在 application.pro...

SpringBoot整合redis

SpringBoot操作redis 参考文档 https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/htmlsingle/#boot-features-redis   1.新建SpringBoot项目 配置pom.xml文件    <!-- https://mvnrepository.co...

Springboot整合redis集群

一、第一步 :搭建redis集群      我的这篇博客中有步骤:https://blog.csdn.net/weixin_42054155/article/details/80999164 二、第二部:创建springboot项目         1、加入maven依赖:  &...

SpringBoot:Shiro 整合 Redis

前言 前段时间做了一个图床的小项目,安全框架使用的是Shiro。为了使用户7x24小时访问,决定把项目由单机升级为集群部署架构。但是安全框架shiro只有单机存储的SessionDao,尽管Shrio有基于Ehcache-rmi的组播/广播实现,然而集群的分布往往是跨网段的,甚至是跨地域的,所以寻求新的方案。 架构 方案 使用 redis 集中存储,实现分布式集群共享用户信息,这里我们采用第三方开...

猜你喜欢

springboot整合redis

1、引入maven配置 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 2、配置application.propertie...

springboot整合redis集群

1、引入maven配置 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.7.RELEASE</version> </...

Redis与springBoot整合

一、说明 直接使用Jedis进行操作,功能相当而言单一,且操作比较繁琐。 将与SpringBoot进行整合,整合后Spring Boot 提供 RedisTemplate 工具类 RedisTemplate增强了Jedis 在redis使用字符串操作最多,专门为字符串处理模板:StringRedisTemplate 二、整合 1)修改pom.xml文件,添加依赖(redis、测试) 2)修改app...

「解放双手」老舅教你VS Code Disco

微信搜索【前端食堂】你的前端食堂,记得按时吃饭。 本文已收录在前端食堂 Github https://github.com/Geekhyt/front-end-canteen,感谢Star。 这是最好的时代,也是最坏的时代。 今年听到过最浪漫的一句话:我们在键盘上留下的余温,也将随时代传递到更远的将来。 感觉让理性的技术人多了份柔光滤镜。 也许你收藏了千篇万篇VS Code快捷键,很可惜却没能记住...

Linux(3):Shell基础

Linux(1):简介与系统安装:https://mp.csdn.net/postedit/88633915 Linux(2):常用命令:https://mp.csdn.net/postedit/88639718 鸟哥的Linux私房菜:https://wizardforcel.gitbooks.io/vbird-linux-basic-4e/content/86.html 目录 9 Shell基...

问答精选

typescript - conditional paramteres

I want to restrict one of the constructor parameter of number type to assign only the values from some range. I know how to archieve similiar effect with setter and conditional statement. I was wonder...

My CSV writer code writes separators between characters and not strings

I have written code which writes to a CSV file, reading from another file. I want to write out particular columns from the input file, so I append them to a list, then separate them by a comma and add...

How to open a Xamarin Forms page from a Xamarin Droid Activity from a onclick listener

I am working on adding an assist feature to my app that when the user holds down on the home button an android activity opens with cards that the user can click on that then opens the xamarin forms pa...

Validation User Mysql Java?

I did this query with this development but I have a problem to receive the API I try to validate Users Login But always the API RESPONSE LOGIN SUCCESS I did this with Java Spring Boot and Mysql Here's...

How convert to array or json php soap __dorequest response?

this is _dorequest response. Response is xml string data. I tried convert with simplexml_load_string() function and later return array but function result is empty. '<env:Envelope xmlns:env="h...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答