缓存改成全使用string模式

This commit is contained in:
zhongzb 2023-04-24 22:59:16 +08:00
parent 0d2ba4e70a
commit 1c58a17cbc
4 changed files with 27 additions and 7 deletions

View File

@ -1,5 +1,9 @@
package com.abin.mallchat.common.common.config;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import net.sf.json.util.JSONUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
@ -49,10 +53,18 @@ public class RedisConfig {
if (source == null || source.length == 0) {
return null;
}
if (type.isInstance(String.class) || type.isInstance(Character.class)) {
if (type.isAssignableFrom(String.class) || type.isAssignableFrom(Character.class)) {
return (T) new String(source);
}
return super.deserialize(source, type);
}
}
@SneakyThrows
public static void main(String[] args) {
ObjectMapper objectMapper =new ObjectMapper();
System.out.println(objectMapper.writeValueAsString(1));
System.out.println(objectMapper.writeValueAsString("1"));
System.out.println(objectMapper.writeValueAsString(Boolean.TRUE));
}
}

View File

@ -228,7 +228,7 @@ public class RedisUtils {
}
public <T> List<T> mget(Collection<String> keys, Class<T> tClass) {
List list = redisTemplate.opsForValue().multiGet(keys);
List list = stringRedisTemplate.opsForValue().multiGet(keys);
return (List<T>) list.stream().map(o -> toBeanOrNull(o, tClass)).collect(Collectors.toList());
}
@ -242,9 +242,17 @@ public class RedisUtils {
throw new UnsupportedOperationException(e);
}
}
public static String objToStr(Object o) {
try {
return jsonMapper.writeValueAsString(o);
} catch (Exception e) {
throw new UnsupportedOperationException(e);
}
}
public <T> void mset(Map<String, T> map, long time) {
redisTemplate.opsForValue().multiSet(map);
Map<String, String> collect = map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, (e) -> objToStr(e.getValue())));
stringRedisTemplate.opsForValue().multiSet(collect);
map.forEach((key, value) -> {
expire(key, time);
});
@ -915,7 +923,7 @@ public class RedisUtils {
*/
public Set<TypedTuple<String>> zReverseRangeWithScores(String key,
long pageSize) {
return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, Double.MIN_VALUE,
return stringRedisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, Double.MIN_VALUE,
Double.MAX_VALUE, 0, pageSize);
}
@ -927,7 +935,7 @@ public class RedisUtils {
*/
public Set<TypedTuple<String>> zReverseRangeByScoreWithScores(String key,
double max, long pageSize) {
return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, Double.MIN_VALUE, max,
return stringRedisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, Double.MIN_VALUE, max,
1, pageSize);
}

View File

@ -92,7 +92,7 @@ public class UserCache {
public Map<Long, User> getUserInfoBatch(Set<Long> uids) {
List<String> keys = uids.stream().map(a -> RedisKey.getKey(RedisKey.USER_INFO_STRING, a)).collect(Collectors.toList());
List<User> mget = redisUtils.mget(keys, User.class);
Map<Long, User> map = mget.stream().collect(Collectors.toMap(User::getId, Function.identity()));
Map<Long, User> map = mget.stream().filter(Objects::nonNull).collect(Collectors.toMap(User::getId, Function.identity()));
//还需要load更新的uid
List<Long> needLoadUidList = uids.stream().filter(a -> !map.containsKey(a)).collect(Collectors.toList());
if (CollUtil.isNotEmpty(needLoadUidList)) {

View File

@ -34,7 +34,7 @@ public class UserBackpackServiceImpl implements IUserBackpackService {
@Override
@RedissonLock(key = "#uid")
public void acquireItem(Long uid, Long itemId, IdempotentEnum idempotentEnum, String businessId) {//todo 分布式锁
public void acquireItem(Long uid, Long itemId, IdempotentEnum idempotentEnum, String businessId) {
String idempotent = getIdempotent(itemId, idempotentEnum, businessId);
UserBackpack userBackpack = userBackpackDao.getByIdp(idempotent);
//幂等检查