配置:RedisConfig
package com.wewetea.weadmin.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.EnableCaching;
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
@EnableCaching
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 連接工廠
template.setConnectionFactory(factory);
// 使用Jackson2JsonRedisSerializer
Jackson2JsonRedisSerializer<Object> jacksonSeial = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化輸入的類(lèi)型
om.activateDefaultTyping(om.getPolymorphicTypeValidator(),ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.serialize(om);
// 值采用json序列化
template.setValueSerializer(jacksonSeial);
// 使用StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
// 設(shè)置hash序列化模式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();
return template;
}
}
這樣序列化以后,才會(huì)使用JSON明文顯示,否則是二進(jìn)制碼顯示。

image.png
配置:application-dev.yml
--- ### redis源配置
spring:
data:
redis:
host: localhost
port: 6379
password:
database: 0
connect-timeout: 30000
timeout: 60000
lettuce:
pool:
enabled: true
max-idle: 16
min-idle: 8
max-active: 32
max-wait: 60000
time-between-eviction-runs: 900000
常用工具類(lèi):
package com.wewetea.weadmin.common.utils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
private final RedisTemplate<String,Object> redisTemplate;
public RedisUtil(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
/**
* 給一個(gè)指定的 key 值附加過(guò)期時(shí)間
*
*/
public boolean expire(String key, long time) {
return redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
/**
* 根據(jù)key 獲取過(guò)期時(shí)間
*
*/
public long getTime(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 根據(jù)key 獲取過(guò)期時(shí)間
*
*/
public boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* 移除指定key 的過(guò)期時(shí)間
*
*/
public boolean persist(String key) {
return Boolean.TRUE.equals(redisTemplate.boundValueOps(key).persist());
}
//- - - - - - - - - - - - - - - - - - - - - String類(lèi)型 - - - - - - - - - - - - - - - - - - - -
/**
* 根據(jù)key獲取值
*
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 將值放入緩存
*
*/
public void set(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 將值放入緩存并設(shè)置時(shí)間
*
*/
public void set(String key, String value, long time) {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
redisTemplate.opsForValue().set(key, value);
}
}
/**
* 批量添加 key (重復(fù)的鍵會(huì)覆蓋)
*/
public void batchSet(Map<String, String> keyAndValue) {
redisTemplate.opsForValue().multiSet(keyAndValue);
}
/**
* 批量添加 key-value 只有在鍵不存在時(shí),才添加
* map 中只要有一個(gè)key存在,則全部不添加
*/
public void batchSetIfAbsent(Map<String, String> keyAndValue) {
redisTemplate.opsForValue().multiSetIfAbsent(keyAndValue);
}
/**
* 對(duì)一個(gè) key-value 的值進(jìn)行加減操作,
* 如果該 key 不存在 將創(chuàng)建一個(gè)key 并賦值該 number
* 如果 key 存在,但 value 不是長(zhǎng)整型 ,將報(bào)錯(cuò)
*/
public Long increment(String key, long number) {
return redisTemplate.opsForValue().increment(key, number);
}
/**
* 對(duì)一個(gè) key-value 的值進(jìn)行加減操作,
* 如果該 key 不存在 將創(chuàng)建一個(gè)key 并賦值該 number
* 如果 key 存在,但 value 不是 純數(shù)字 ,將報(bào)錯(cuò)
*/
public Double increment(String key, double number) {
return redisTemplate.opsForValue().increment(key, number);
}
//- - - - - - - - - - - - - - - - - - - - - set類(lèi)型 - - - - - - - - - - - - - - - - - - - -
/**
* 將數(shù)據(jù)放入set緩存
*/
public void sSet(String key, String value) {
redisTemplate.opsForSet().add(key, value);
}
/**
* 獲取變量中的值
*/
public Set<Object> members(String key) {
return redisTemplate.opsForSet().members(key);
}
/**
* 隨機(jī)獲取變量中指定個(gè)數(shù)的元素
*/
public void randomMembers(String key, long count) {
redisTemplate.opsForSet().randomMembers(key, count);
}
/**
* 隨機(jī)獲取變量中的元素
*/
public Object randomMember(String key) {
return redisTemplate.opsForSet().randomMember(key);
}
/**
* 彈出變量中的元素
*/
public Object pop(String key) {
return redisTemplate.opsForSet().pop("setValue");
}
/**
* 獲取變量中值的長(zhǎng)度
*/
public Long size(String key) {
return redisTemplate.opsForSet().size(key);
}
/**
* 根據(jù)value從一個(gè)set中查詢(xún),是否存在
*/
public boolean sHasKey(String key, Object value) {
return Boolean.TRUE.equals(redisTemplate.opsForSet().isMember(key, value));
}
/**
* 檢查給定的元素是否在變量中。
*/
public boolean isMember(String key, Object obj) {
return Boolean.TRUE.equals(redisTemplate.opsForSet().isMember(key, obj));
}
/**
* 轉(zhuǎn)移變量的元素值到目的變量。
*/
public boolean move(String key, String value, String destKey) {
return Boolean.TRUE.equals(redisTemplate.opsForSet().move(key, value, destKey));
}
/**
* 批量移除set緩存中元素
*/
public void remove(String key, Object... values) {
redisTemplate.opsForSet().remove(key, values);
}
/**
* 通過(guò)給定的key求2個(gè)set變量的差值
*
*/
public Set<Object> difference(String key, String destKey) {
return redisTemplate.opsForSet().difference(key, destKey);
}
//- - - - - - - - - - - - - - - - - - - - - hash類(lèi)型 - - - - - - - - - - - - - - - - - - - -
/**
* 加入緩存
*
*/
public void add(String key, Map<String, Object> map) {
redisTemplate.opsForHash().putAll(key, map);
}
/**
* 獲取 key 下的 所有 hashkey 和 value
*/
public Map<Object, Object> getHashEntries(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* 驗(yàn)證指定 key 下 有沒(méi)有指定的 hashkey
*/
public boolean hashKey(String key, String hashKey) {
return redisTemplate.opsForHash().hasKey(key, hashKey);
}
/**
* 獲取指定key的值string
*/
public String getMapString(String key, String key2) {
return Objects.requireNonNull(redisTemplate.opsForHash().get("map1", "key1")).toString();
}
/**
* 獲取指定的值Int
*/
public Integer getMapInt(String key, String key2) {
return (Integer) redisTemplate.opsForHash().get("map1", "key1");
}
/**
* 彈出元素并刪除
*/
public String popValue(String key) {
return Objects.requireNonNull(redisTemplate.opsForSet().pop(key)).toString();
}
/**
* 刪除指定 hash 的 HashKey
*/
public Long delete(String key, String... hashKeys) {
return redisTemplate.opsForHash().delete(key, (Object) hashKeys);
}
/**
* 給指定 hash 的 hashkey 做增減操作
*/
public Long increment(String key, String hashKey, long number) {
return redisTemplate.opsForHash().increment(key, hashKey, number);
}
/**
* 給指定 hash 的 hashkey 做增減操作
*/
public Double increment(String key, String hashKey, Double number) {
return redisTemplate.opsForHash().increment(key, hashKey, number);
}
/**
* 獲取 key 下的 所有 hashkey 字段
*/
public Set<Object> hashKeys(String key) {
return redisTemplate.opsForHash().keys(key);
}
/**
* 獲取指定 hash 下面的 鍵值對(duì) 數(shù)量
*/
public Long hashSize(String key) {
return redisTemplate.opsForHash().size(key);
}
//- - - - - - - - - - - - - - - - - - - - - list類(lèi)型 - - - - - - - - - - - - - - - - - - - -
/**
* 在變量左邊添加元素值
*/
public void leftPush(String key, Object value) {
redisTemplate.opsForList().leftPush(key, value);
}
/**
* 獲取集合指定位置的值。
*/
public Object index(String key, long index) {
return redisTemplate.opsForList().index("list", 1);
}
/**
* 獲取指定區(qū)間的值。
*/
public List<Object> range(String key, long start, long end) {
return redisTemplate.opsForList().range(key, start, end);
}
/**
* 把最后一個(gè)參數(shù)值放到指定集合的第一個(gè)出現(xiàn)中間參數(shù)的前面,
* 如果中間參數(shù)值存在的話。
*/
public void leftPush(String key, String pivot, String value) {
redisTemplate.opsForList().leftPush(key, pivot, value);
}
/**
* 向左邊批量添加參數(shù)元素。
*/
public void leftPushAll(String key, String... values) {
redisTemplate.opsForList().leftPushAll(key, values);
}
/**
* 向集合最右邊添加元素。
*
*/
public void leftPushAll(String key, String value) {
redisTemplate.opsForList().rightPush(key, value);
}
/**
* 向左邊批量添加參數(shù)元素。
*
*/
public void rightPushAll(String key, String... values) {
redisTemplate.opsForList().rightPushAll(key, values);
}
/**
* 向已存在的集合中添加元素。
*
*/
public void rightPushIfPresent(String key, Object value) {
redisTemplate.opsForList().rightPushIfPresent(key, value);
}
/**
* 向已存在的集合中添加元素。
*/
public Long listLength(String key) {
return redisTemplate.opsForList().size(key);
}
/**
* 移除集合中的左邊第一個(gè)元素。
*
*/
public void leftPop(String key) {
redisTemplate.opsForList().leftPop(key);
}
/**
* 移除集合中左邊的元素在等待的時(shí)間里,如果超過(guò)等待的時(shí)間仍沒(méi)有元素則退出。
*
*/
public void leftPop(String key, long timeout, TimeUnit unit) {
redisTemplate.opsForList().leftPop(key, timeout, unit);
}
/**
* 移除集合中右邊的元素。
*/
public void rightPop(String key) {
redisTemplate.opsForList().rightPop(key);
}
/**
* 移除集合中右邊的元素在等待的時(shí)間里,如果超過(guò)等待的時(shí)間仍沒(méi)有元素則退出。
*
*/
public void rightPop(String key, long timeout, TimeUnit unit) {
redisTemplate.opsForList().rightPop(key, timeout, unit);
}
}
待解決問(wèn)題:
2025-05-16 13:25:10 INFO [lettuce-nioEventLoop-4-7] io.lettuce.core.protocol.CommandHandler - null Unexpected exception during request: java.net.SocketException: Connection reset
java.net.SocketException: Connection reset
at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:394)
at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:426)
at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:255)
at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1132)
at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:356)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:796)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:732)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:658)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:998)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:833)
可能原因是,服務(wù)端已經(jīng)關(guān)閉,而客戶(hù)端還在連接沒(méi)有及時(shí)關(guān)掉。
bind 127.0.0.1
port 6379
requirepass password
protected-mode no
把保護(hù)模式設(shè)置成 no即可解決,前提是你所有的參數(shù)設(shè)置對(duì)了。