...
|
...
|
@@ -5,6 +5,7 @@ import com.yoho.search.base.utils.CollectionUtils; |
|
|
import com.yoho.search.base.utils.Transfer;
|
|
|
import com.yoho.search.common.cache.impls.EhCache;
|
|
|
import com.yoho.search.core.redis.components.YohoSearchRedisComponent;
|
|
|
import com.yoho.search.recall.scene.beans.CacheSknInfoRequestResponse;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
...
|
...
|
@@ -13,8 +14,7 @@ import org.springframework.stereotype.Component; |
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
@Component
|
|
|
public class CacheRequestResponseComponent {
|
|
|
public abstract class CacheRequestResponseComponent<K extends ICacheRequest, V,T extends CacheRequestResponse<K, V>> {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(CacheRequestResponseComponent.class);
|
|
|
|
...
|
...
|
@@ -24,14 +24,35 @@ public class CacheRequestResponseComponent { |
|
|
private EhCache ehCache;
|
|
|
|
|
|
/**
|
|
|
* 从缓存中获取response
|
|
|
* 批量处理请求,以及添加缓存
|
|
|
*
|
|
|
* @param requests
|
|
|
* @param useEhCache
|
|
|
* @return
|
|
|
*/
|
|
|
public void bacthFillResponseWithCache(final List<T> requests, boolean useEhCache) {
|
|
|
//1、批量从缓存中获取
|
|
|
this.batchFillResponseFromCache(requests, useEhCache);
|
|
|
//2、获取未命中缓存的请求
|
|
|
List<T> missCacheRequests = this.filterMissCacheRequests(requests);
|
|
|
//3、如果remainRequests为空,则说明全部命中了缓存,直接返回即可
|
|
|
if (missCacheRequests.isEmpty()) {
|
|
|
return ;
|
|
|
}
|
|
|
//4、处理请求
|
|
|
Map<K, V> requestResponseMap = this.queryMissCacheRequestResults(missCacheRequests);
|
|
|
//5、填充查询结果
|
|
|
this.batchFillResponseWithQueryResults(requests, requestResponseMap);
|
|
|
//6、将CacheRequestResponse中需要缓存的key加入缓存
|
|
|
this.batchAddResponseToCache(requests, useEhCache);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 从缓存中批量填充response
|
|
|
* @param cacheRequestResponses
|
|
|
* @param useEhCache
|
|
|
* @param <K>
|
|
|
* @param <V>
|
|
|
* @param <T>
|
|
|
*/
|
|
|
public <K extends ICacheRequest, V, T extends CacheRequestResponse<K, V>> void batchFillResponseFromCache(final List<T> cacheRequestResponses, boolean useEhCache) {
|
|
|
private void batchFillResponseFromCache(final List<T> cacheRequestResponses, boolean useEhCache) {
|
|
|
try {
|
|
|
Collection<RedisKeyBuilder> keys = new ArrayList<>();
|
|
|
for (T requestResponse : cacheRequestResponses) {
|
...
|
...
|
@@ -55,15 +76,12 @@ public class CacheRequestResponseComponent { |
|
|
/**
|
|
|
* 过滤出未命中缓存的结果
|
|
|
* @param cacheRequestResponses
|
|
|
* @param <K>
|
|
|
* @param <V>
|
|
|
* @param <T>
|
|
|
* @return
|
|
|
*/
|
|
|
public <K extends ICacheRequest, V, T extends CacheRequestResponse<K, V>> List<T> filterMissCacheRequests(List<T> cacheRequestResponses){
|
|
|
private List<T> filterMissCacheRequests(List<T> cacheRequestResponses) {
|
|
|
List<T> notCachedRequests = new ArrayList<>();
|
|
|
for (T requestResponse : cacheRequestResponses) {
|
|
|
if (requestResponse != null && requestResponse.getResponse()==null) {
|
|
|
if (requestResponse != null && requestResponse.getResponse() == null) {
|
|
|
notCachedRequests.add(requestResponse);
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -71,22 +89,33 @@ public class CacheRequestResponseComponent { |
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 使用查询结果填充response
|
|
|
* 处理未命中缓存的请求-子类实现
|
|
|
*
|
|
|
* @param missCacheRequests
|
|
|
* @return
|
|
|
*/
|
|
|
public abstract Map<K, V> queryMissCacheRequestResults(List<T> missCacheRequests);
|
|
|
|
|
|
/**
|
|
|
* 使用查询结果填充请求-转成String
|
|
|
* @param cacheRequestResponses
|
|
|
* @param queryResults
|
|
|
* @param <K>
|
|
|
* @param <V>
|
|
|
* @param <T>
|
|
|
*/
|
|
|
public <K extends ICacheRequest, V, T extends CacheRequestResponse<K, V>> void batchFillResponseWithQueryResults(List<T> cacheRequestResponses,Map<String,V> queryResults){
|
|
|
private void batchFillResponseWithQueryResults(List<T> cacheRequestResponses, Map<K, V> queryResults) {
|
|
|
//1、先转成map
|
|
|
Map<String, V> toCacheResults = new HashMap<>();
|
|
|
for (Map.Entry<K, V> entry : queryResults.entrySet()) {
|
|
|
toCacheResults.put(entry.getKey().redisKeyBuilder().getKey(), entry.getValue());
|
|
|
}
|
|
|
//2、填充结果
|
|
|
for (T sknInfoRequestResponse : cacheRequestResponses) {
|
|
|
if(sknInfoRequestResponse.getResponse()!=null){
|
|
|
if (sknInfoRequestResponse.getResponse() != null) {
|
|
|
continue;
|
|
|
}
|
|
|
RedisKeyBuilder redisKeyBuilder = sknInfoRequestResponse.getRequest().redisKeyBuilder();
|
|
|
V response = queryResults.get(redisKeyBuilder.getKey());
|
|
|
if(response!=null){
|
|
|
sknInfoRequestResponse.setResponse(response,true);
|
|
|
V response = toCacheResults.get(redisKeyBuilder.getKey());
|
|
|
if (response != null) {
|
|
|
sknInfoRequestResponse.setResponse(response, true);
|
|
|
}
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -95,11 +124,8 @@ public class CacheRequestResponseComponent { |
|
|
* 将未缓存的response添加至缓存
|
|
|
* @param cacheRequestResponses
|
|
|
* @param useEhCache
|
|
|
* @param <K>
|
|
|
* @param <V>
|
|
|
* @param <T>
|
|
|
*/
|
|
|
public <K extends ICacheRequest, V, T extends CacheRequestResponse<K, V>> void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache) {
|
|
|
private void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache) {
|
|
|
//1、按缓存时间分组
|
|
|
Map<Integer, List<T>> groupMap = CollectionUtils.toListMap(cacheRequestResponses, new Transfer<T, Integer>() {
|
|
|
@Override
|
...
|
...
|
@@ -113,16 +139,16 @@ public class CacheRequestResponseComponent { |
|
|
}
|
|
|
}
|
|
|
|
|
|
private <K extends ICacheRequest, V, T extends CacheRequestResponse<K, V>> void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache, int timeOutInSecond) {
|
|
|
private void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache, int timeOutInSecond) {
|
|
|
try {
|
|
|
Map<RedisKeyBuilder, String> toCacheMap = new HashMap<>();
|
|
|
for (T requestResponse : cacheRequestResponses) {
|
|
|
if (!requestResponse.isNeedRecache()){
|
|
|
if (!requestResponse.isNeedRecache()) {
|
|
|
continue;
|
|
|
}
|
|
|
RedisKeyBuilder redisKeyBuilder = requestResponse.getRequest().redisKeyBuilder();
|
|
|
V response = requestResponse.getResponse();
|
|
|
if(redisKeyBuilder==null || response==null){
|
|
|
if (redisKeyBuilder == null || response == null) {
|
|
|
continue;
|
|
|
}
|
|
|
String cacheValue = requestResponse.getFromResponseTransfer().transfer(response);
|
...
|
...
|
|