使用Spring boot整合redis,其本质还是使用了Spring的Spring Data Redis去处理的实现的。使用之前要保证redis安装完成并启动其服务端,然后在框架中需要添加spring boot中的redis和redis连接池的相应依赖,然后编写相应的配置文件,写一个简单的demo,可以直接调用spring提供的redisTemplate来实现简单的crud操作,注意对象保存是需要实现序列化,不能直接存储,可以在对象类中实现Serializable类。
一、添加依赖
打开一个spring boot项目,这里有两个很要依赖,一个会spring boot中的redis依赖,另一个是redis连接池需要的依赖
<!-- redis依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二、编写配置文件
这里主要就是配置redis的基本配置和连接池属性的配置
spring:
#redis
redis:
host: 127.0.0.1
port: 6379
database: 0
timeout: 5000
#连接池
jedis:
pool:
max-idle: 20
min-idle: 2
max-wait: 3000
max-active: 50
三、编写一个小demo
添加一个对象到redis中,key为String,value为对象。
spring提供的redisTemplate,已经封装好了一些东西,可以直接调用使用实现简单的redis的CRUD操作。一般使用时都会讲这个类做成做个工具类,方便service层的使用。
package com.example.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.logging.Logger;
@Component
public class RedisUtils {
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
Logger log = Logger.getLogger("RedisUtils.class");
/**
* 读取缓存
*/
public Object get(final Object key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 写入缓存
*/
public boolean set(final Object key, Object value) {
boolean result = false;
try {
redisTemplate.opsForValue().set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 更新缓存
*/
public boolean getAndSet(final Object key, Object value) {
boolean result = false;
try {
redisTemplate.opsForValue().getAndSet(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 删除缓存
*/
public boolean delete(final Object key) {
boolean result = false;
try {
redisTemplate.delete(key);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
编写对象类,并序列化(因为要存储到redis至内容中,对象必须序列化才能存储)。实现序列化在存储的时候会序列化,在取出的时候会反序列化!
package com.example.model;
import lombok.Data;
import java.io.Serializable;
/*
* 对象存储到redis中需要先序列化
* */
@Data
public class User1 implements Serializable {
private int id;
private String name;
private int age;
}
service层,直接写实现,接口就不写了
package com.example.serviceimpl;
import com.example.model.User1;
import com.example.util.RedisUtils;
import com.example.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class RedisServiceimpl implements RedisService {
@Autowired
private RedisUtils redisUtils;
public void setUser(){
User1 u = new User1();
u.setAge(1);
u.setId(1);
u.setName("tom");
redisUtils.set("user001",u);
}
public Object getByKey(){
return redisUtils.get("user001");
}
}
controller层调用
package com.example.controller;
import com.example.model.User;
import com.example.model.User1;
import com.example.service.RedisService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
import java.util.logging.Logger;
@Controller
public class HelloController {
Logger log = Logger.getLogger("HelloController.class");
@Autowired
UserService userService;
@Autowired
RedisService redisService;
@GetMapping(value = "/hello")
public String hello(){
redisService.setUser();
User1 u = (User1) redisService.getByKey();
log.info(String.valueOf(u));
return "hello";
}
}
运行结果
这只是最简单的一个demo,对于里面的序列化的操作以及redis支持的五种数据类型的操作,都在学习过程中。