码云地址:https://gitee.com/hjc2/springboot.git
需要添加的依赖如下:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
配置文件如下:
server.port=8081
# 连接池最大数量(使用负值表示没有限制)
spring.redis.jedis.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=30000
spring.redis.port=6379
#redis地址
spring.redis.host=localhost
新建一个 RedisConfig 配置redis,代码如下,其中解决了redis存进去之后是16进制的问题,redis客户端下载地址:
https://gitee.com/qishibo/AnotherRedisDesktopManager/releases
package com.dream.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.jedis.pool.max-active}")
private int maxActive;
@Value("${spring.redis.pool.max-wait}")
private int maxWait;
@Value("${spring.redis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.port}")
private int port;
/**
* redis模板,存储关键字是字符串,值是Jdk序列化
**/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
// // key 和 value 的序列化
// RedisSerializer<String> redisSerializer = new StringRedisSerializer();
// redisTemplate.setKeySerializer(redisSerializer);
// redisTemplate.setHashKeySerializer(redisSerializer);
//
// //JdkSerializationRedisSerializer序列化方式;
// JdkSerializationRedisSerializer jdkRedisSerializer=new JdkSerializationRedisSerializer();
// redisTemplate.setValueSerializer(jdkRedisSerializer);
// redisTemplate.setHashValueSerializer(jdkRedisSerializer);
// redisTemplate.afterPropertiesSet();
// // 开启事务
// redisTemplate.setEnableTransactionSupport(true);
// 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// 设置value的序列化规则和 key的序列化规则
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
redisTemplate.setEnableDefaultSerializer(true);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
封装了一下常用的redis的使用方法,代码如下,注释很清楚:
package com.dream.util;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
/**
* 功能描述: <br>
* 〈设置redis的值〉
*
* @Param: [key, object]
* @Return: boolean
* @Author: huajiancheng
* @Date: 2020/9/12
*/
public boolean set(String key, Object object) {
try {
redisTemplate.opsForValue().set(key, JSON.toJSONString(object));
return true;
} catch (Exception e) {
System.out.println("redis设置值发生异常--->" + e);
}
return false;
}
/**
* 功能描述: <br>
* 〈设置redis的值〉
*
* @Param: [key, object,seconds]
* @Return: boolean
* @Author: huajiancheng
* @Date: 2020/9/12
*/
public boolean set(String key, Object object, int seconds) {
try {
redisTemplate.opsForValue().set(key, JSON.toJSONString(object));
expire(key, seconds);
return true;
} catch (Exception e) {
System.out.println("redis设置值发生异常--->" + e);
}
return false;
}
/**
* 功能描述: <br>
* 〈设置redis超时时间〉
*
* @Param: [key, seconds]
* @Return: boolean
* @Author: huajiancheng
* @Date: 2020/9/12
*/
public boolean expire(String key, int seconds) {
try {
return redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
} catch (Exception e) {
System.out.println("redis设置超时时间发生异常--->" + e);
}
return false;
}
/**
* 功能描述: <br>
* 〈判断key存不存在〉
*
* @Param: [key]
* @Return: boolean
* @Author: huajiancheng
* @Date: 2020/9/12
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 功能描述: <br>
* 〈删除缓存〉
*
* @Param: [key]
* @Return: boolean
* @Author: huajiancheng
* @Date: 2020/9/12
*/
public boolean remove(String key) {
try {
return redisTemplate.delete(key);
} catch (Exception e) {
System.out.println("删除缓存时发生异常---->" + e);
}
return false;
}
/**
* 功能描述: <br>
* 〈获取key的value〉
*
* @Param: [key]
* @Return: java.lang.String
* @Author: huajiancheng
* @Date: 2020/9/12
*/
public Object get(String key) {
try {
return redisTemplate.opsForValue().get(key);
} catch (Exception e) {
System.out.println("获取redis值发生异常--->" + e);
}
return null;
}
/**
* 根据key 获取过期时间
*
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 功能描述: <br>
* 〈设置DB分区〉
*
* @Param: [index]
* @Return: void
* @Author: huajiancheng
* @Date: 2020/9/12
*/
public void setDataBase(int index) {
LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();
if (connectionFactory != null && index != connectionFactory.getDatabase()) {
connectionFactory.setDatabase(index);
this.redisTemplate.setConnectionFactory(connectionFactory);
connectionFactory.resetConnection();
}
}
/**
* 获取hashKey对应的所有键值
*
* @param key 键
* @return 对应的多个键值
*/
public Map<Object, Object> hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* HashSet
*
* @param key 键
* @param map 对应多个键值
* @return true 成功 false 失败
*/
public boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
客户端的存储结果:
欢迎关注我的微信公众号,更多内容将在微信公众号中发布: