SpringBoot非官方教程 | 第九篇: springboot整合Redis
转载请标明出处:
原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot9-redis/
本文出自方志朋的博客
这篇文章主要介绍springboot整合redis,至于没有接触过redis的同学可以看下这篇文章:5分钟带你入门Redis。
引入依赖:
在pom文件中添加redis依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置数据源
spring.redis.host=localhost
spring.redis.port=6379
#spring.redis.password=
spring.redis.database=1
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=500
spring.redis.pool.min-idle=0
spring.redis.timeout=0
如果你的redis有密码,配置下即可。经过上述两步的操作,你可以访问redis数据了。
数据访问层dao
通过redisTemplate来访问redis.
@Repository
public class RedisDao {
@Autowired
private StringRedisTemplate template;
public void setKey(String key,String value){
ValueOperations<String, String> ops = template.opsForValue();
ops.set(key,value,1, TimeUnit.MINUTES);//1分钟过期
}
public String getValue(String key){
ValueOperations<String, String> ops = this.template.opsForValue();
return ops.get(key);
}
}
单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRedisApplicationTests {
public static Logger logger= LoggerFactory.getLogger(SpringbootRedisApplicationTests.class);
@Test
public void contextLoads() {
}
@Autowired
RedisDao redisDao;
@Test
public void testRedis(){
redisDao.setKey("name","forezp");
redisDao.setKey("age","11");
logger.info(redisDao.getValue("name"));
logger.info(redisDao.getValue("age"));
}
}
启动单元测试,你发现控制台打印了:
forezp
11
单元测试通过;
源码下载:https://github.com/forezp/SpringBootLearning
参考资料
扫码关注公众号有惊喜
(转载本站文章请注明作者和出处 方志朋的博客)
SpringBoot非官方教程 | 第九篇: springboot整合Redis的更多相关文章
- (转) SpringBoot非官方教程 | 第一篇:构建第一个SpringBoot工程
简介 spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程.它采用的是建立生产就绪的应用程序观点,优先于配置的惯例 ...
- SpringBoot非官方教程 | 第一篇:构建第一个SpringBoot工程
转载请标明出处: https://www.fangzhipeng.com/springboot/2017/07/11/springboot1 本文出自方志朋的博客 简介 spring boot 它的设 ...
- (转) SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解
springboot采纳了建立生产就绪spring应用程序的观点. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.在一般情况下,我们不需要做太多的配置就能够让spring boot正 ...
- SpringBoot进阶教程(二十九)整合Redis 发布订阅
SUBSCRIBE, UNSUBSCRIBE 和 PUBLISH 实现了 发布/订阅消息范例,发送者 (publishers) 不用编程就可以向特定的接受者发送消息 (subscribers). Ra ...
- SpringBoot进阶教程(二十五)整合Redis之@Cacheable、@CachePut、@CacheEvict的应用
在上一篇文章(<SpringBoot(二十四)整合Redis>)中,已经实现了Spring Boot对Redis的整合,既然已经讲到Cache了,今天就介绍介绍缓存注解.各家互联网产品现在 ...
- SpringBoot进阶教程(二十四)整合Redis
缓存现在几乎是所有中大型网站都在用的必杀技,合理的利用缓存不仅能够提升网站访问速度,还能大大降低数据库的压力.Redis提供了键过期功能,也提供了灵活的键淘汰策略,所以,现在Redis用在缓存的场合非 ...
- SpringBoot进阶教程(二十八)整合Redis事物
Redis默认情况下,事务支持被禁用,必须通过设置setEnableTransactionSupport(true)为使用中的每个redistplate显式启用.这样做会强制将当前重新连接绑定到触发m ...
- SpringBoot进阶教程(二十六)整合Redis之共享Session
集群现在越来越常见,当我们项目搭建了集群,就会产生session共享问题.因为session是保存在服务器上面的.那么解决这一问题,大致有三个方案,1.通过nginx的负载均衡其中一种ip绑定来实现( ...
- SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot2-config-file/ 本文出自方志朋的博客 ...
随机推荐
- jgrid 选择器 C#控件
一.RadioButtonList html代码: <asp:RadioButtonList ID="rlPlan" runat="server" Rep ...
- Java Web SpringMVC AJAX,实现页面懒加载数据
因为做的微信端的网页,所以在显示后台数据的时候,要么分页,要么全部加载数据,开始分页对于用户来说,其实体验不是很好,毕竟要去不断的点击下一页,但是如果我把全部数据读取出来的话,但用户可能也就看前面几条 ...
- Nginx反向代理、负载均衡功能
环境: [root@db02 ~]# uname -a Linux db02 -.el6.x86_64 # SMP Tue Mar :: UTC x86_64 x86_64 x86_64 GNU/Li ...
- vue分页
1.依赖文件 <link href="/css/index.css" rel="stylesheet" type="text/css" ...
- Observer(观察者)设计模式[转]
Observer设计模式中主要包括如下两类对象: Subject:监视对象,它往往包含着其他对象所感兴趣的内容.在本范例中,热水器就是一个监视对象,它包含的其他对象所感兴趣的内容,就是tempratu ...
- 02.for循环
语法: for(表达式1;表达式2;表达式3) { 循环体; } 练习1: namespace _02.for循环的练习01 { class Program { static void Main(st ...
- Python 动态加载 Extension Manager Classes
看着看着发现了一个库:stevedore(http://stevedore.readthedocs.org/en/latest/managers.html),但是感觉文档做得不行啊,都没个tutori ...
- C++基础--static的用法
首先,看看变量的存储: int global ; int main() { int stackStore ; int heapStore* = (int *)malloc(sizeof(int)); ...
- Linux CentOS如何汉化系统
su root切换为root用户 写入环境变量 echo "export LANG="zh_CN.UTF8"">>/etc/profile sour ...
- Linux漏洞分析入门笔记-Off-By-One(栈)
ubuntu-16.04.5(X86) IDA7.0 0x00.漏洞描述 1.什么是off by one?又称1字节溢出. 源字符串长度等于目标缓冲区长度时,将源字符串复制到目标缓冲区可能会导致off ...