mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-03-13 21:53:41 +08:00
缓存改成全使用string模式
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
package com.abin.mallchat.common.common.config;
|
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.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
@@ -49,10 +53,18 @@ public class RedisConfig {
|
|||||||
if (source == null || source.length == 0) {
|
if (source == null || source.length == 0) {
|
||||||
return null;
|
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 (T) new String(source);
|
||||||
}
|
}
|
||||||
return super.deserialize(source, type);
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -228,7 +228,7 @@ public class RedisUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <T> List<T> mget(Collection<String> keys, Class<T> tClass) {
|
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());
|
return (List<T>) list.stream().map(o -> toBeanOrNull(o, tClass)).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,9 +242,17 @@ public class RedisUtils {
|
|||||||
throw new UnsupportedOperationException(e);
|
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) {
|
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) -> {
|
map.forEach((key, value) -> {
|
||||||
expire(key, time);
|
expire(key, time);
|
||||||
});
|
});
|
||||||
@@ -915,7 +923,7 @@ public class RedisUtils {
|
|||||||
*/
|
*/
|
||||||
public Set<TypedTuple<String>> zReverseRangeWithScores(String key,
|
public Set<TypedTuple<String>> zReverseRangeWithScores(String key,
|
||||||
long pageSize) {
|
long pageSize) {
|
||||||
return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, Double.MIN_VALUE,
|
return stringRedisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, Double.MIN_VALUE,
|
||||||
Double.MAX_VALUE, 0, pageSize);
|
Double.MAX_VALUE, 0, pageSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -927,7 +935,7 @@ public class RedisUtils {
|
|||||||
*/
|
*/
|
||||||
public Set<TypedTuple<String>> zReverseRangeByScoreWithScores(String key,
|
public Set<TypedTuple<String>> zReverseRangeByScoreWithScores(String key,
|
||||||
double max, long pageSize) {
|
double max, long pageSize) {
|
||||||
return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, Double.MIN_VALUE, max,
|
return stringRedisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, Double.MIN_VALUE, max,
|
||||||
1, pageSize);
|
1, pageSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ public class UserCache {
|
|||||||
public Map<Long, User> getUserInfoBatch(Set<Long> uids) {
|
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<String> keys = uids.stream().map(a -> RedisKey.getKey(RedisKey.USER_INFO_STRING, a)).collect(Collectors.toList());
|
||||||
List<User> mget = redisUtils.mget(keys, User.class);
|
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
|
//还需要load更新的uid
|
||||||
List<Long> needLoadUidList = uids.stream().filter(a -> !map.containsKey(a)).collect(Collectors.toList());
|
List<Long> needLoadUidList = uids.stream().filter(a -> !map.containsKey(a)).collect(Collectors.toList());
|
||||||
if (CollUtil.isNotEmpty(needLoadUidList)) {
|
if (CollUtil.isNotEmpty(needLoadUidList)) {
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public class UserBackpackServiceImpl implements IUserBackpackService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@RedissonLock(key = "#uid")
|
@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);
|
String idempotent = getIdempotent(itemId, idempotentEnum, businessId);
|
||||||
UserBackpack userBackpack = userBackpackDao.getByIdp(idempotent);
|
UserBackpack userBackpack = userBackpackDao.getByIdp(idempotent);
|
||||||
//幂等检查
|
//幂等检查
|
||||||
|
|||||||
Reference in New Issue
Block a user