spring Boot 熟悉后,集成一个外部扩展是一件很容易的事,集成Redis也很简单,看下面步骤配置:
一、添加pom依赖
org.springframework.boot
spring-boot-starter-redis
二、创建 RedisClient.java
注意该类存放的package
package org.springframework.data.redis.connection.jedis;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.exceptions.JedisException;
public class RedisClient {
private static Logger logger = LoggerFactory.getLogger(RedisClient.class);
private JedisConnectionFactory factory;
public RedisClient(JedisConnectionFactory factory) {
super();
this.factory = factory;
}
public void putObject(final String key, final Object value, final int cacheSeconds) {
if (StringUtils.isNotBlank(key)) {
redisTemplete(key, new RedisExecute
三、创建Redis配置类
RedisConfig.Java
package com.shanhy.example.redis;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.RedisClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(JedisConnectionFactory factory) {
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new RedisObjectSerializer());
template.afterPropertiesSet();
return template;
}
@Bean
public RedisClient redisClient(JedisConnectionFactory factory){
return new RedisClient(factory);
}
}
RedisObjectSerializer.java
package com.shanhy.example.redis; 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{ private Converter serializer = new SerializingConverter(); private Converter deserializer = new DeserializingConverter(); static final byte[] EMPTY_ARRAY = new byte[0]; @Override public Object deserialize(byte[] bytes) { if (isEmpty(bytes)) { return null; } try { return deserializer.convert(bytes); } catch (Exception ex) { throw new SerializationException("Cannot deserialize", ex); } } @Override 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); } }
四、创建测试方法
下面代码随便放一个Controller里
@Autowired private RedisTemplateredisTemplate; @RequestMapping("/redisTest") public String redisTest() { try { redisTemplate.opsForValue().set("test-key", "redis测试内容", 2, TimeUnit.SECONDS);// 缓存有效期2秒 logger.info("从Redis中读取数据:" + redisTemplate.opsForValue().get("test-key").toString()); TimeUnit.SECONDS.sleep(3); logger.info("等待3秒后尝试读取过期的数据:" + redisTemplate.opsForValue().get("test-key")); } catch (InterruptedException e) { e.printStackTrace(); } return "OK"; }
五、配置文件配置Redis
application.yml
spring: # Redis配置 redis: host: 192.168.1.101 port: 6379 password: # 连接超时时间(毫秒) timeout: 10000 pool: max-idle: 20 min-idle: 5 max-active: 20 max-wait: 2
这样就完成了Redis的配置,可以正常使用 redisTemplate 了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



