...
|
...
|
@@ -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.models.ICacheRequest;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
...
|
...
|
@@ -31,11 +32,11 @@ public class CacheRequestResponseComponent { |
|
|
* @param <V>
|
|
|
* @param <T>
|
|
|
*/
|
|
|
public <K, V, T extends CacheRequestResponse<K, V>> void batchFillResponseFromCache(final List<T> cacheRequestResponses, boolean useEhCache) {
|
|
|
public <K extends ICacheRequest, V, T extends CacheRequestResponse<K, V>> void batchFillResponseFromCache(final List<T> cacheRequestResponses, boolean useEhCache) {
|
|
|
try {
|
|
|
Collection<RedisKeyBuilder> keys = new ArrayList<>();
|
|
|
for (T requestResponse : cacheRequestResponses) {
|
|
|
RedisKeyBuilder redisKeyBuilder = requestResponse.getRequestRedisKeyBuilder();
|
|
|
RedisKeyBuilder redisKeyBuilder = requestResponse.getRequest().redisKeyBuilder();
|
|
|
keys.add(redisKeyBuilder);
|
|
|
}
|
|
|
List<String> cachedValues = this.mutiGetFromCache(keys, useEhCache);
|
...
|
...
|
@@ -60,7 +61,7 @@ public class CacheRequestResponseComponent { |
|
|
* @param <T>
|
|
|
* @return
|
|
|
*/
|
|
|
public <K, V, T extends CacheRequestResponse<K, V>> List<T> filterMissCacheRequests(List<T> cacheRequestResponses){
|
|
|
public <K extends ICacheRequest, V, T extends CacheRequestResponse<K, V>> List<T> filterMissCacheRequests(List<T> cacheRequestResponses){
|
|
|
List<T> notCachedRequests = new ArrayList<>();
|
|
|
for (T requestResponse : cacheRequestResponses) {
|
|
|
if (requestResponse != null && requestResponse.getResponse()==null) {
|
...
|
...
|
@@ -78,12 +79,12 @@ public class CacheRequestResponseComponent { |
|
|
* @param <V>
|
|
|
* @param <T>
|
|
|
*/
|
|
|
public <K, V, T extends CacheRequestResponse<K, V>> void batchFillResponseWithQueryResults(List<T> cacheRequestResponses,Map<RedisKeyBuilder,V> queryResults){
|
|
|
public <K extends ICacheRequest, V, T extends CacheRequestResponse<K, V>> void batchFillResponseWithQueryResults(List<T> cacheRequestResponses,Map<RedisKeyBuilder,V> queryResults){
|
|
|
for (T sknInfoRequestResponse : cacheRequestResponses) {
|
|
|
if(sknInfoRequestResponse.getResponse()!=null){
|
|
|
continue;
|
|
|
}
|
|
|
RedisKeyBuilder redisKeyBuilder = sknInfoRequestResponse.getRequestRedisKeyBuilder();
|
|
|
RedisKeyBuilder redisKeyBuilder = sknInfoRequestResponse.getRequest().redisKeyBuilder();
|
|
|
V response = queryResults.get(redisKeyBuilder);
|
|
|
if(response!=null){
|
|
|
sknInfoRequestResponse.setResponse(response,true);
|
...
|
...
|
@@ -99,12 +100,12 @@ public class CacheRequestResponseComponent { |
|
|
* @param <V>
|
|
|
* @param <T>
|
|
|
*/
|
|
|
public <K, V, T extends CacheRequestResponse<K, V>> void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache) {
|
|
|
public <K extends ICacheRequest, V, T extends CacheRequestResponse<K, V>> void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache) {
|
|
|
//1、按缓存时间分组
|
|
|
Map<Integer, List<T>> groupMap = CollectionUtils.toListMap(cacheRequestResponses, new Transfer<T, Integer>() {
|
|
|
@Override
|
|
|
public Integer transfer(T t) {
|
|
|
return t.getCacheTimeInSecond();
|
|
|
return t.getRequest().cacheTimeInSecond();
|
|
|
}
|
|
|
});
|
|
|
//2、按缓存时间大小直接加入缓存
|
...
|
...
|
@@ -113,14 +114,14 @@ public class CacheRequestResponseComponent { |
|
|
}
|
|
|
}
|
|
|
|
|
|
private <K, V, T extends CacheRequestResponse<K, V>> void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache, int timeOutInSecond) {
|
|
|
private <K extends ICacheRequest, V, T extends CacheRequestResponse<K, V>> void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache, int timeOutInSecond) {
|
|
|
try {
|
|
|
Map<RedisKeyBuilder, String> toCacheMap = new HashMap<>();
|
|
|
for (T requestResponse : cacheRequestResponses) {
|
|
|
if (!requestResponse.isNeedRecache()){
|
|
|
continue;
|
|
|
}
|
|
|
RedisKeyBuilder redisKeyBuilder = requestResponse.getRequestRedisKeyBuilder();
|
|
|
RedisKeyBuilder redisKeyBuilder = requestResponse.getRequest().redisKeyBuilder();
|
|
|
V response = requestResponse.getResponse();
|
|
|
if(redisKeyBuilder==null || response==null){
|
|
|
continue;
|
...
|
...
|
|