Springboot学习笔记(七)-集成Redis
添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<target>1.8</target>
<source>1.8</source>
</configuration>
</plugin>
</plugins>
</build>
RedisApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication
@EnableScheduling
@EnableWebMvc
public class RedisApplication {
public static void main(String[] args) {
SpringApplication.run(RedisApplication.class, args);
}
}
RedisService
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;
@Service
public class RedisService {
public static final String USER_INFO = "userInfo";
private final StringRedisTemplate redisTemplate;
@Autowired
public RedisService(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public String getToken() {
return redisTemplate.opsForValue().get(USER_INFO);
}
public void setToken(String token) {
redisTemplate.opsForValue().set(USER_INFO, token, 5, TimeUnit.SECONDS);
}
}
RedisController
import com.yan.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisController {
private final RedisService redisService;
@Autowired
public RedisController(RedisService redisService) {
this.redisService = redisService;
}
@PostMapping(value = "/setToken")
public String setToken(String token) {
try {
redisService.setToken(token);
return "success";
} catch (Exception e) {
e.printStackTrace();
return "error";
}
}
@GetMapping(value = "/getToken")
public String getToken() {
return redisService.getToken();
}
}
test
import com.yan.controller.RedisController;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.concurrent.TimeUnit;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
@Autowired
WebApplicationContext context;
@Autowired
private RedisController service;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(service).build();
}
@Test
public void s() throws Exception {
String contentAsString = mockMvc.perform(post("/setToken").param("token", "yan")).andReturn().getResponse().getContentAsString();
Assert.assertEquals("success", contentAsString);
}
@Test
public void s1() throws Exception {
Assert.assertEquals("yan", service.getToken());
TimeUnit.SECONDS.sleep(2);
Assert.assertEquals("yan", service.getToken());
TimeUnit.SECONDS.sleep(2);
Assert.assertEquals("yan", service.getToken());
TimeUnit.SECONDS.sleep(2);
Assert.assertEquals(null, service.getToken());
}
}
Springboot学习笔记(七)-集成Redis的更多相关文章
- springboot学习笔记-3 整合redis&mongodb
一.整合redis 1.1 建立实体类 @Entity @Table(name="user") public class User implements Serializable ...
- SpringBoot学习笔记:Redis缓存
SpringBoot学习笔记:Redis缓存 关于Redis Redis是一个使用ANSI C语言编写的免费开源.支持网络.可基于内存亦可以持久化的日志型.键值数据库.其支持多种存储类型,包括Stri ...
- SpringBoot学习笔记(2):引入Spring Security
SpringBoot学习笔记(2):用Spring Security来保护你的应用 快速开始 本指南将引导您完成使用受Spring Security保护的资源创建简单Web应用程序的过程. 参考资料: ...
- SpringBoot学习笔记(7):Druid使用心得
SpringBoot学习笔记(7):Druid使用心得 快速开始 添加依赖 <dependency> <groupId>com.alibaba</groupId> ...
- SpringBoot学习笔记:Swagger实现文档管理
SpringBoot学习笔记:Swagger实现文档管理 Swagger Swagger是一个规范且完整的框架,用于生成.描述.调用和可视化RESTful风格的Web服务.Swagger的目标是对RE ...
- go微服务框架kratos学习笔记七(kratos warden 负载均衡 balancer)
目录 go微服务框架kratos学习笔记七(kratos warden 负载均衡 balancer) demo demo server demo client 池 dao service p2c ro ...
- (转)Qt Model/View 学习笔记 (七)——Delegate类
Qt Model/View 学习笔记 (七) Delegate 类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...
- Windows phone 8 学习笔记(9) 集成
原文:Windows phone 8 学习笔记(9) 集成 本节整理了之前并没有提到的Windows phone 8 系统相关集成支持,包括选择器.锁定屏幕的.联系人的访问等.选择器列举了若干内置应用 ...
- SpringBoot学习笔记
SpringBoot个人感觉比SpringMVC还要好用的一个框架,很多注解配置可以非常灵活的在代码中运用起来: springBoot学习笔记: .一.aop: 新建一个类HttpAspect,类上添 ...
- Learning ROS for Robotics Programming Second Edition学习笔记(七) indigo PCL xtion pro live
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...
随机推荐
- TP v5中Url Compat模式
compatible 对于配置pathinfo的支持,在Nginx作服务器.无数种系统要同时运行的环境,实在是一项很累赘的事情,而又不想很low的多个参数(像m.c.a)构造路由参数,我需要那种不必强 ...
- keras系列︱人脸表情分类与识别:opencv人脸检测+Keras情绪分类(四)
引自:http://blog.csdn.net/sinat_26917383/article/details/72885715 人脸识别热门,表情识别更加.但是表情识别很难,因为人脸的微表情很多,本节 ...
- docker:Error running DeviceCreate (createSnapDevice) dm_task_run failed
1) service docker stop 2) thin_check /home/docker/devicemapper/devicemapper/metadata 3) thin_check - ...
- linux 基础笔记(一)
[1.1]系统的选择 Centos: CentOS(Community Enterprise Operating System)是Linux发行版之一,它是来自于Red Hat Enterprise ...
- hbase 学习(十三)集群间备份原理
集群建备份,它是master/slaves结构式的备份,由master推送,这样更容易跟踪现在备份到哪里了,况且region server是都有自己的WAL 和HLog日志,它就像mysql的主从备份 ...
- redis获取当前时间精确到微秒
在redis取得当前时的方法为执行time命令 127.0.0.1:6382> time1) "1495780564"2) "894089" 第一行为以 ...
- Ubuntu下Eclipse中文乱码解决
把Windows下的工程导入到了Linux下Eclipse中,由于以前的工程代码,都是GBK编码的(Windows下的Eclipse 默认会去读取系统的编码,所以Widnwos下的Eclipse的编码 ...
- STM32的TAMPER-RTC管脚作为Tamper的使用[转]
问题一: 当 TAMPER引脚上的信号从 0变成1或者从 1变成 0(取决于备份控制寄存器BKP_CR的 TPAL位),会产生一个侵入检测事件.侵入检测事件将所有数据备份寄存器内容清除. 然而为了 ...
- 关于网页游戏断线重连的思路和demo求助
http://bbs.9ria.com/thread-146997-1-1.html —————————————————————————————————————————————————— 1:俺有什么 ...
- js学习(三)-使用大括号({ })语法创建无类型对象
//----------------------js代码------------------- var user = { name:'tom', say:function(){ console.log ...