③SpringBoot中Redis的使用
本文基于前面的springBoot系列文章进行学习,主要介绍redis的使用。
SpringBoot对常用的数据库支持外,对NoSQL 数据库也进行了封装自动化。
redis介绍
Redis是目前业界使用最广泛的内存数据存储。相比memcached,Redis支持更丰富的数据结构,例如hashes, lists, sets等,同时支持数据持久化。除此之外,Redis还提供一些类数据库的特性,比如事务,HA,主从库。可以说Redis兼具了缓存系统和数据库的一些特性,因此有着丰富的应用场景。本文介绍Redis在Spring Boot中两个典型的应用场景。
如何使用
1、引入 spring-boot-starter-redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、添加配置文件
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=
# 连接超时时间(毫秒)
spring.redis.timeout=300
3、Redis工具类
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository; @Repository
public class RedisDao {
@Autowired
private StringRedisTemplate template; /**
* redis存
* @param key
* @param value
*/
public void setKey(String key,String value){
ValueOperations<String, String> ops = template.opsForValue();
ops.set(key,value,,TimeUnit.MINUTES);//1分钟过期
} /**
* redis取
* @param key
* @return
*/
public String getValue(String key){
ValueOperations<String, String> ops = this.template.opsForValue();
return ops.get(key);
}
}
4.测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisDaoTest {
@Test
public void contextLoads() { } @Autowired
private RedisDao redisDao; @Test
public void testRedis(){
redisDao.setKey("name","forezp");
redisDao.setKey("age","");
System.out.println("存到redis中的name为"+redisDao.getValue("name"));
System.out.println("存到redis中的age为"+redisDao.getValue("age"));
}
}

③SpringBoot中Redis的使用的更多相关文章
- SpringBoot中Redis的set、map、list、value、实体类等基本操作介绍
今天给大家介绍一下SpringBoot中Redis的set.map.list.value等基本操作的具体使用方法 上一节中给大家介绍了如何在SpringBoot中搭建Redis缓存数据库,这一节就针对 ...
- springboot中redis取缓存类型转换异常
异常如下: [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested ...
- SpringBoot中redis的使用介绍
REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. Redis是一个开源的使用ANSI C语言编写.遵守B ...
- SpringBoot中Redis的使用
转载:http://www.ityouknow.com/springboot/2016/03/06/spring-boot-redis.html Spring Boot 对常用的数据库支持外,对 No ...
- springboot中redis的缓存穿透问题
什么是缓存穿透问题?? 我们使用redis是为了减少数据库的压力,让尽量多的请求去承压能力比较大的redis,而不是数据库.但是高并发条件下,可能会在redis还没有缓存的时候,大量的请求同时进入,导 ...
- springBoot 中redis 注解缓存的使用
1,首先在启动类上加上 @EnableCaching 这个注解 在查询类的controller,或service ,dao 中方法上加 @Cacheable 更新或修改方法上加 @CachePut 注 ...
- springboot中redis做缓存时的配置
import com.google.common.collect.ImmutableMap;import org.slf4j.Logger;import org.slf4j.LoggerFactory ...
- springboot中Redis的Lettuce客户端和jedis客户端
1.引入客户端依赖 <!--jedis客户端依赖--> <dependency> <groupId>redis.clients</groupId> &l ...
- Spring-Boot 中 Redis 的简单使用以及简单模糊匹配删除
https://yulaiz.com/spring-boot-redis-simple/
随机推荐
- 自然语言处理中的语言模型预训练方法(ELMo、GPT和BERT)
自然语言处理中的语言模型预训练方法(ELMo.GPT和BERT) 最近,在自然语言处理(NLP)领域中,使用语言模型预训练方法在多项NLP任务上都获得了不错的提升,广泛受到了各界的关注.就此,我将最近 ...
- Multiply Strings,字符串相乘
问题描述:给定两个字符串,返回他们的乘积. public class MultiplyStrings { public String multiply(String num1, String num2 ...
- angularjs实现星星评分
angularjs实现星星评分 自定义指令 app.directive('myStars', function () { return { require : '?ngModel', // ?ngMo ...
- Java中如何指定跳出多重嵌套循环
今天做项目优化涉及到一个跳出指定多重嵌套循环的问题,觉得还是记录一下那么在Java中如何跳出当前的多重嵌套循环? 方法一:可以在需要的循环语句前定义一个标号,然后在里层循环体的代码中使用带有标号的br ...
- activity启动模式之singleTop
activity启动模式之singleTop 一.简介 二.设置方法 在AndroidManifest.xml中将要设置为singleTop启动模式的页面进行配置 <activity andro ...
- 编译android源码中的icu4c
在external/icu4c/studata/readme.txt,里面有修改icu4c中资源的编译方法 # 具体步骤(可复制下面命令,直接运行): # 1)新增或者修改external/icu4c ...
- CocoaPods使用事项
本人整理转载自 :Code4App 原创文章.转载请注明出处:http://code4app.com/article/cocoapods-install-usage 1CocoaPods是什么? 当你 ...
- LeetCode OJ:Count Primes(质数计数)
Count the number of prime numbers less than a non-negative number, n. 计算小于n的质数的个数,当然就要用到大名鼎鼎的筛法了,代码如 ...
- C++轮子队-软件需求规格说明书
团队Github项目仓库 软件规格需求说明书 引言 编写目的 软件规格需求说明书书了“2048俄罗斯方块”1.0版本的软件功能性需求和非功能性需求. 文档约定 描述编写文档时所采用的标准或排版约定,包 ...
- react : code splitting
1.webpack config output: { ... chunkFilename: 'js/[name].min.js' ... } optimization: { splitChunks: ...