本文基于前面的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的使用的更多相关文章

  1. SpringBoot中Redis的set、map、list、value、实体类等基本操作介绍

    今天给大家介绍一下SpringBoot中Redis的set.map.list.value等基本操作的具体使用方法 上一节中给大家介绍了如何在SpringBoot中搭建Redis缓存数据库,这一节就针对 ...

  2. springboot中redis取缓存类型转换异常

    异常如下: [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested ...

  3. SpringBoot中redis的使用介绍

    REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. Redis是一个开源的使用ANSI C语言编写.遵守B ...

  4. SpringBoot中Redis的使用

    转载:http://www.ityouknow.com/springboot/2016/03/06/spring-boot-redis.html Spring Boot 对常用的数据库支持外,对 No ...

  5. springboot中redis的缓存穿透问题

    什么是缓存穿透问题?? 我们使用redis是为了减少数据库的压力,让尽量多的请求去承压能力比较大的redis,而不是数据库.但是高并发条件下,可能会在redis还没有缓存的时候,大量的请求同时进入,导 ...

  6. springBoot 中redis 注解缓存的使用

    1,首先在启动类上加上 @EnableCaching 这个注解 在查询类的controller,或service ,dao 中方法上加 @Cacheable 更新或修改方法上加 @CachePut 注 ...

  7. springboot中redis做缓存时的配置

    import com.google.common.collect.ImmutableMap;import org.slf4j.Logger;import org.slf4j.LoggerFactory ...

  8. springboot中Redis的Lettuce客户端和jedis客户端

    1.引入客户端依赖 <!--jedis客户端依赖--> <dependency> <groupId>redis.clients</groupId> &l ...

  9. Spring-Boot 中 Redis 的简单使用以及简单模糊匹配删除

    https://yulaiz.com/spring-boot-redis-simple/

随机推荐

  1. Java 四大作用域总结

    一.ServletContext 1.生命周期:当Web应用被加载进容器时创建代表整个web应用的ServletContext对象,当服务器关闭或Web应用被移除时,ServletContext对象跟 ...

  2. spark SQL学习(load和save操作)

    load操作:主要用于加载数据,创建出DataFrame save操作:主要用于将DataFrame中的数据保存到文件中 代码示例(默认为parquet数据源类型) package wujiadong ...

  3. js 执行跨域刷新页面

    主要代码: 注意这段代码 是子页面中添加的也就是弹出的那个页面刷新父页面 <script type="text/javascript"> function shuaxi ...

  4. Oracle索引以及索引碎片

    索引,可以增加查询速度,若没有索引,每次查询都必须是全表查询.例如,搜索某个记录时(如name="gdpuzxs")时,需要全表扫描一下,因为不知道有多少个name="g ...

  5. apache kafka配置中request.required.acks含义

    Kafka producer的ack有3中机制,初始化producer时的producerconfig可以通过配置request.required.acks不同的值来实现. 0:这意味着生产者prod ...

  6. mapreduce&GFS&bigtable learning

    之前在学习udf的时候接触到了mapreduce,感觉很酷,于是学习了一下,看了mapreduce和GFS的论文,但是没有总结,只是看了一遍 准备利用在学校的时间,学习一下bigtable,然后再认真 ...

  7. MyEclipse安装aptana插件, 8.5之前版本 和 之后版本, MyEclipse10安装aptana

    MyEclipse8.5安装aptana插件说明: 1.在myeclipse的安装目录下,有个common文件夹,建一个myplugIns,此目录下建aptana_update_024747目录 2. ...

  8. ASCII_01

    1.来自“http://baike.baidu.com/link?url=WgFPtGe-rT6x6X0r_OiHGVZAV87Fu4_P5fvr7FsGyrm8QqTGuvVUfg4Jx7Rn-Le ...

  9. 使用mybatis如果类属性名和数据库中的属性名不一样取值就会为null

    使用mybatis时如果类属性名和数据库中的属性名不一样取值就会为null 这是不能再去改变javabean中的属性,只能改变sql语句.语句如下所示: <select id="sel ...

  10. mac用ssh连接linux云服务器中文乱码或无法显示解决

    问题1:服务器是ubuntu16.04,用mac自带的ssh连接后无法正常输入中文? 解:这种情况一般是终端和服务器的字符集不匹配,MacOSX下默认的是utf8字符集. 打开编辑 .bashrc 文 ...