springboot使用spring Data redis操作redis缓存
1.引入相应的包采用gradle来管理包用maven也是类似的方法
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'redis.clients:jedis:4.0.1'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
2.properties文件配置
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=0
3.编解码器用来序列化和反序列化对象(如果不需要存储对象可以省略)
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
public class RedisObjectSerializer implements RedisSerializer<Object> {
private Converter<Object, byte[]> serializer = new SerializingConverter();
private Converter<byte[], Object> deserializer = new DeserializingConverter();
static final byte[] EMPTY_ARRAY = new byte[0];
// 解压
public Object deserialize(byte[] bytes) {
if (isEmpty(bytes)) {
return null;
}
try {
return deserializer.convert(bytes);
} catch (Exception ex) {
throw new SerializationException("Cannot deserialize", ex);
}
}
// 压缩
public byte[] serialize(Object object) {
if (object == null) {
return EMPTY_ARRAY;
}
try {
return serializer.convert(object);
} catch (Exception ex) {
return EMPTY_ARRAY;
}
}
private boolean isEmpty(byte[] data) {
return (data == null || data.length == 0);
}
}
4.编写配置类,装载并注入redisTemplate实例,代码如下
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class redisConfig {
@Bean
RedisTemplate createRedisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String, ?> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new RedisObjectSerializer());
return template;
}
}
5.使用RedisTemplate代码如下
import com.zqw.learnredies.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class testController {
private RedisTemplate redisTemplate;
@Autowired
public void setRedisTemplate(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
@GetMapping("/test")
public Map<String,Object> test(){
Map<String,Object> res = new HashMap<>();
redisTemplate.opsForValue().set("url", "http://www.zqwxsf.top");
res.put("usl",redisTemplate.opsForValue().get("url"));
User user = new User("C++", 40);
redisTemplate.opsForValue().set(user.name, user);
res.put(user.name,redisTemplate.opsForValue().get(user.name));
user = new User("Java", 30);
redisTemplate.opsForValue().set(user.name, user);
res.put(user.name,redisTemplate.opsForValue().get(user.name));
user = new User("python", 33);
redisTemplate.opsForValue().set(user.name, user);
res.put(user.name,redisTemplate.opsForValue().get(user.name));
return res;
}
}
user类如下
import java.io.Serializable;
public class User implements Serializable {
public String name;
public int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
运行结果如下