初始化hash缓存-put

master
DongZeLiang 2024-03-30 11:46:46 +08:00
parent bee6f5678c
commit acc59fd99b
1 changed files with 27 additions and 10 deletions

View File

@ -4,10 +4,7 @@ import com.muyu.common.cache.HashCache;
import com.muyu.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Function;
/**
@ -49,10 +46,7 @@ public abstract class HashCacheAbs<K, HK, HV> implements HashCache<K, HK, HV> {
*/
@Override
public Map<HK, HV> get (K key) {
Map<String, HV> dataMap = redisService.getCacheMap(encode(key));
Map<HK, HV> resultMap = new HashMap<>();
dataMap.forEach((hashKey, hashValue) -> resultMap.put(decodeHashKey(hashKey), hashValue));
return resultMap;
return decodeMap(redisService.getCacheMap(encode(key)));
}
/**
@ -91,7 +85,8 @@ public abstract class HashCacheAbs<K, HK, HV> implements HashCache<K, HK, HV> {
*/
@Override
public List<HV> getToList (K key) {
return null;
Map<HK, HV> hkhvMap = get(key);
return hkhvMap.values().stream().toList();
}
/**
@ -102,7 +97,7 @@ public abstract class HashCacheAbs<K, HK, HV> implements HashCache<K, HK, HV> {
*/
@Override
public void put (K key, Map<HK, HV> map) {
redisService.setCacheMap(encode(key), encodeMap(map));
}
/**
@ -149,4 +144,26 @@ public abstract class HashCacheAbs<K, HK, HV> implements HashCache<K, HK, HV> {
public void remove (K key, HK hashKey) {
}
/**
*
* @param dataMap
* @return
*/
private Map<String, HV> encodeMap(Map<HK, HV> dataMap){
Map<String, HV> encodeDataMap = new HashMap<>();
dataMap.forEach((hashKey, HashValue) -> encodeDataMap.put(encodeHashKey(hashKey), HashValue));
return encodeDataMap;
}
/**
*
* @param encodeDataMap
* @return
*/
private Map<HK, HV> decodeMap(Map<String, HV> encodeDataMap){
Map<HK, HV> dataMap = new HashMap<>();
encodeDataMap.forEach((hashKey, hashValue) -> dataMap.put(decodeHashKey(hashKey), hashValue));
return dataMap;
}
}