Spring Boot使用redis
1.Redis 简介
Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库。
Redis 与其他 key - value 缓存产品有以下三个特点:
1. Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
2. Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
3. Redis支持数据的备份,即master-slave模式的数据备份。
2.新建项目
新建spring boot项目,具体请参考:
3.添加依赖
spring boot和redis的整合需要添加redis相关依赖,具体如下:
<!--添加redis支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
4.添加配置信息
在application.properties中配置如下:
########################################################
###Redis (RedisConfiguration)
########################################################
spring.redis.database=1
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.timeout=5000
5.编写代码
1、创建model
package com.crazyang.domain;
import java.io.Serializable;
/**
* Created with IntelliJ IDEA.
*
* @Author crazyang
* @Desciption:
* @Date 2018-6-13 9:14
*/
public class RedisEntity implements Serializable {
private static final long serialVersionUId = 1L;
private String id;
private String name;
public RedisEntity(){
super();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
2、创建RedisService
package com.crazyang.service;
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;
/**
* Created with IntelliJ IDEA.
*
* @Author crazyang
* @Desciption:
* @Date 2018-6-13 9:15
*/
@Service
public class RedisService {
@Autowired
StringRedisTemplate stringRedisTemplate;
@Resource(name = "stringRedisTemplate")
ValueOperations<String,String> valOpsStr;
@Autowired
RedisTemplate<Object,Object> redisTemplate;
@Resource(name = "redisTemplate")
ValueOperations<Object,Object> valOpsObj;
/**
* 根据指定的key获取缓存
* @param key
* @return
*/
public String getStr(String key){
return valOpsStr.get(key);
}
/**
* 设置缓存
* @param key
* @param val
*/
public void setStr(String key,String val){
valOpsStr.set(key,val);
}
/**
* 删除指定key
* @param key
*/
public void del(String key){
stringRedisTemplate.delete(key);
}
/**
* 根据指定o获取Object
* @param o
* @return
*/
public Object getObj(Object o){
return valOpsObj.get(o);
}
/**
* 设置obj缓存
* @param o1
* @param o2
*/
public void setObj(Object o1,Object o2){
valOpsObj.set(o1,o2);
}
/**
* 删除obj缓存
* @param o
*/
public void delObj(Object o){
redisTemplate.delete(o);
}
}
3、创建RedisController
package com.crazyang.controller;
import com.crazyang.domain.RedisEntity;
import com.crazyang.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created with IntelliJ IDEA.
*
* @Author crazyang
* @Desciption:
* @Date 2018-6-13 9:46
*/
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
private RedisService redisService;
@RequestMapping("/setStr")
public String setStr(String key, String val) {
try {
redisService.setStr(key, val);
return "success";
} catch (Exception e) {
e.printStackTrace();
return "error";
}
}
@RequestMapping("/getStr")
public String getStr(String key) {
return redisService.getStr(key);
}
@RequestMapping("/delStr")
public String delStr(String key){
try {
redisService.del(key);
return "delete success";
}catch (Exception e){
e.printStackTrace();
return "delete false";
}
}
@RequestMapping("/setObj")
public String setObj(String key, RedisEntity o2){
try {
redisService.setObj(key,o2);
return "setObj success";
}catch (Exception e){
e.printStackTrace();
return "setObj false";
}
}
@RequestMapping("/getObj")
public Object getObj(String key){
return redisService.getObj(key);
}
@RequestMapping("/delObj")
public String delObj(String key){
try {
redisService.delObj(key);
return "delObj success";
}catch (Exception e){
e.printStackTrace();
return "delObj false";
}
}
}
6.运行项目
点击运行*Application,进入浏览器后进行测试:
1.StringRedisTemplate
http://127.0.0.1:8080/redis/setStr?key=t1&val=this is t1
http://127.0.0.1:8080/redis/getStr?key=t1
http://127.0.0.1:8080/redis/delStr?key=t1
2.RedisTemplate
http://127.0.0.1:8080/redis/setObj?key=t1&id=1&name=test
http://127.0.0.1:8080/redis/getObj?key=t1
http://127.0.0.1:8080/redis/delObj?key=t1
运行部分截图如下所示:
1、设置字符串
2、提取字符串
3、删除字符串
4、提取字符串