前言
Redis是一个缓存、消息代理和功能丰富的键值存储。StringBoot提供了基本的自动配置。本文记录一下springboot与redis的简单整合实例
官方文档:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/
前期准备
首先我们要有一个Redis服务,由于我没有Linux环境,为了方便搭建项目,直接在Windows下安装,参考这篇博客:Windows下安装Redis服务
安装步骤:一直点下一步(偷懒,步骤9、10设置密码我没有设置)
下载、安装、启动好Redis服务后我们设置一个key并获取一下
代码编写
maven引包
org.springframework.boot
spring-boot-starter-data-redis
配置文件
我们先看一下都有哪些Redis相关配置,发现好多都有默认值,而且刚好符合我们现在的测试环境,于是乎我的配置文件是这样滴....
server.port=1113
spring.application.name=redis-server
接口测试代码
@RestController
public class Controller{
@Autowired
private StringRedisTemplate template;
@RequestMapping("/redis/get/{key}")
private String get(@PathVariable("key") String key){
return template.opsForValue().get(key);
}
@RequestMapping("/redis/set/{key}/{value}")
private Boolean set(@PathVariable("key") String key,@PathVariable("value") String value){
boolean flag = true;
try {
template.opsForValue().set(key,value);
} catch (Exception e) {
e.printStackTrace();
flag = false;
}
return flag;
}
}
测试效果
我们直接启动springboot的main函数,浏览器访问测试接口
扩展工具类
关于StringRedisTemplate类的操作自行查阅资料,我在网上找了一个工具类,我没有测试过,但可以参考自行测试!
@SuppressWarnings("ALL")
@Component
public class RedisUtil {
private static StringRedisTemplate template;
public RedisUtil(StringRedisTemplate template) {
RedisUtil.template = template;
}
private void expire(String key, long time) {
try {
if (time > 0) {
template.expire(key, time, TimeUnit.SECONDS);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public long getExpire(String key) {
return template.getExpire(key, TimeUnit.SECONDS);
}
public boolean hasKey(String key) {
try {
return template.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
template.delete(key[0]);
} else {
template.delete(CollectionUtils.arrayToList(key));
}
}
}
//============================String=============================
public Object get(String key) {
return key == null ? null : template.opsForValue().get(key);
}
public boolean set(String key, Object value) {
try {
template.opsForValue().set(key, String.valueOf(value));
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
template.opsForValue().set(key, String.valueOf(value), time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return template.opsForValue().increment(key, delta);
}
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return template.opsForValue().increment(key, -delta);
}
//================================Map=================================
public Object hget(String key, String item) {
return template.opsForHash().get(key, item);
}
public Map
后记
这只是一个单机版的Redis服务,而且还是Windows上面的,后续有空再搭建Redis集群
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。