Created
June 29, 2022 02:18
-
-
Save friddle/f7035f03e258504141adb30799dbcbf3 to your computer and use it in GitHub Desktop.
Redis Lambda Functions. 一些Redis的Lambda优化方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Slf4j | |
public class RedisLambda { | |
public static long DEFAULT_DURATION=60*60*24; | |
public static RedisTemplate getRedisTemplate() | |
{ | |
RedisTemplate redisTemplate=SpringContextHolder.getBean(RedisTemplate.class); | |
return redisTemplate; | |
} | |
public static<T,R> List<R> cacheItems(Function<List<T>, Map<T,R>> function, List<T> keys, String cacheName){ | |
return cacheItems(function,keys,cacheName,null,false); | |
} | |
public static<T,R> R cache(Function<T,R> function, String cacheName,long duration,boolean forceReload){ | |
RedisTemplate template=getRedisTemplate(); | |
ValueOperations ops=getRedisTemplate().opsForValue(); | |
Object value=ops.get(cacheName); | |
if(value==null||forceReload){ | |
R result=function.apply(null); | |
ops.set(cacheName,result); | |
if(duration>0){ | |
template.expire(cacheName, Duration.ofSeconds(duration)); | |
} | |
value=result; | |
} | |
return (R)value; | |
} | |
public static<T,R> R cache(Function<T,R> function, String cacheName){ | |
return cache(function,cacheName,DEFAULT_DURATION,false); | |
} | |
public static<T,R> R cacheWithKey(Function<T,R> function, String cacheName,String key,long duration,boolean forceReload){ | |
return cache(function,String.format(cacheName,key),duration,forceReload); | |
} | |
public static<T,R> R cacheWithKey(Function<T,R> function, String cacheName,String key){ | |
return cacheWithKey(function,cacheName,key,DEFAULT_DURATION,false); | |
} | |
public static void cacheClear(String cacheName){ | |
RedisTemplate ops=getRedisTemplate(); | |
ops.delete(cacheName); | |
return; | |
} | |
public static<T,R> List<R> cacheItems(Function<List<T>, Map<T,R>> function, List<T> keys, String cacheName,T emptyObj, boolean forceReload) | |
{ | |
HashOperations ops=getRedisTemplate().opsForHash(); | |
List<R> redisValues= (List<R>) ops.multiGet(cacheName,keys).stream().map(item->(R)item).collect(Collectors.toList()); | |
//redisValues是可以为空的 | |
List<T> emptyKeys=IntStream.of(keys.size()).filter(index->redisValues.get(index)==null) | |
.mapToObj(index->(T)keys.get(index)) | |
.collect(Collectors.toList()); | |
//先临时搞下吧。反正没啥区别 | |
//假如没有那么多key。反正要补全的... | |
if(!emptyKeys.isEmpty()||forceReload){ | |
Map<T,R> functionRsp=function.apply(emptyKeys); | |
ops.putAll(cacheName,functionRsp); | |
if(functionRsp.size()!=emptyKeys.size()){ | |
List<T> invokeNotFoundKeys=emptyKeys.stream().filter(key->!functionRsp.keySet().contains(key)).collect(Collectors.toList()); | |
log.error("invoke not found"+invokeNotFoundKeys.stream().map(item->item.toString()).collect(Collectors.joining(","))); | |
Map<T,R> emptyValues=invokeNotFoundKeys.stream().collect(Collectors.toMap(item->(T)item,item->(R)emptyObj)); | |
ops.putAll(cacheName,emptyValues); | |
} | |
redisValues.addAll(functionRsp.values()); | |
} | |
return redisValues; | |
} | |
public static void cacheItemsClear(String cacheName) | |
{ | |
HashOperations ops=getRedisTemplate().opsForHash(); | |
ops.delete(cacheName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment