Authored by hugufei

泛型和变量名重命名

... ... @@ -14,7 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import java.util.*;
public abstract class AbstractCacheBean<K extends ICacheRequest, V, T extends AbstractCacheRequestResponse<K, V>> {
public abstract class AbstractCacheBean<Request extends ICacheRequest, Response, RequestResponse extends AbstractCacheRequestResponse<Request, Response>> {
private static final Logger RECALL_NEW_LOGGER = LoggerFactory.getLogger("RECALL");
... ... @@ -26,44 +26,44 @@ public abstract class AbstractCacheBean<K extends ICacheRequest, V, T extends Ab
/**
* 批量处理请求,以及添加缓存
*
* @param requests
* @param requestResponses
* @return
*/
public void bacthFillResponseWithCache(final List<T> requests,int maxMissRequestCount) {
public void bacthFillResponseWithCache(final List<RequestResponse> requestResponses,int maxMissRequestCount) {
//1、批量从缓存中获取
this.batchFillResponseFromCache(requests);
this.batchFillResponseFromCache(requestResponses);
//2、获取未命中缓存的请求
List<T> missCacheRequests = this.filterMissCacheRequests(requests, maxMissRequestCount);
List<RequestResponse> missCacheRequests = this.filterMissCacheRequests(requestResponses, maxMissRequestCount);
//3、如果remainRequests为空,则说明全部命中了缓存,直接返回即可
if (missCacheRequests.isEmpty()) {
return;
}
//4、处理请求
Map<K, V> requestResponseMap = this.queryMissCacheRequestResults(missCacheRequests);
Map<Request, Response> requestResponseMap = this.queryMissCacheRequestResults(missCacheRequests);
//5、填充查询结果
this.batchFillResponseWithQueryResults(requests, requestResponseMap);
this.batchFillResponseWithQueryResults(requestResponses, requestResponseMap);
//6、将CacheRequestResponse中需要缓存的key加入缓存
this.batchAddResponseToCache(requests);
this.batchAddResponseToCache(requestResponses);
}
/**
* 从缓存中批量填充response
*
* @param cacheRequestResponses
* @param requestResponses
*/
private void batchFillResponseFromCache(final List<T> cacheRequestResponses) {
private void batchFillResponseFromCache(final List<RequestResponse> requestResponses) {
try {
Collection<RedisKeyBuilder> keys = new ArrayList<>();
for (T requestResponse : cacheRequestResponses) {
for (RequestResponse requestResponse : requestResponses) {
RedisKeyBuilder redisKeyBuilder = requestResponse.getRequest().redisKeyBuilder();
keys.add(redisKeyBuilder);
}
List<String> cachedValues = this.mutiGetFromCache(keys);
for (int i = 0; i < cacheRequestResponses.size(); i++) {
T requestResponse = cacheRequestResponses.get(i);
for (int i = 0; i < requestResponses.size(); i++) {
RequestResponse requestResponse = requestResponses.get(i);
String cachedValue = cachedValues.get(i);
if (!StringUtils.isBlank(cachedValue)) {
V response = requestResponse.getToResponseTransfer().transfer(cachedValue);
Response response = requestResponse.getToResponseTransfer().transfer(cachedValue);
requestResponse.setResponse(response, false);
}
}
... ... @@ -75,12 +75,12 @@ public abstract class AbstractCacheBean<K extends ICacheRequest, V, T extends Ab
/**
* 过滤出未命中缓存的结果
*
* @param cacheRequestResponses
* @param requestResponses
* @return
*/
private List<T> filterMissCacheRequests(List<T> cacheRequestResponses, int maxMissRequestCount) {
List<T> notCachedRequests = new ArrayList<>();
for (T requestResponse : cacheRequestResponses) {
private List<RequestResponse> filterMissCacheRequests(List<RequestResponse> requestResponses, int maxMissRequestCount) {
List<RequestResponse> notCachedRequests = new ArrayList<>();
for (RequestResponse requestResponse : requestResponses) {
if (requestResponse != null && requestResponse.getResponse() == null) {
notCachedRequests.add(requestResponse);
}
... ... @@ -104,29 +104,29 @@ public abstract class AbstractCacheBean<K extends ICacheRequest, V, T extends Ab
* @param missCacheRequests
* @return
*/
protected abstract Map<K, V> queryMissCacheRequestResults(List<T> missCacheRequests);
protected abstract Map<Request, Response> queryMissCacheRequestResults(List<RequestResponse> missCacheRequests);
/**
* 使用查询结果填充请求-转成String
*
* @param cacheRequestResponses
* @param requestResponses
* @param queryResults
*/
private void batchFillResponseWithQueryResults(List<T> cacheRequestResponses, Map<K, V> queryResults) {
private void batchFillResponseWithQueryResults(List<RequestResponse> requestResponses, Map<Request, Response> queryResults) {
//1、先转成map
Map<String, V> toCacheResults = new HashMap<>();
for (Map.Entry<K, V> entry : queryResults.entrySet()) {
Map<String, Response> toCacheResults = new HashMap<>();
for (Map.Entry<Request, Response> entry : queryResults.entrySet()) {
toCacheResults.put(entry.getKey().redisKeyBuilder().getKey(), entry.getValue());
}
//2、填充结果
for (T sknInfoRequestResponse : cacheRequestResponses) {
if (sknInfoRequestResponse.getResponse() != null) {
for (RequestResponse requestResponse : requestResponses) {
if (requestResponse.getResponse() != null) {
continue;
}
RedisKeyBuilder redisKeyBuilder = sknInfoRequestResponse.getRequest().redisKeyBuilder();
V response = toCacheResults.get(redisKeyBuilder.getKey());
RedisKeyBuilder redisKeyBuilder = requestResponse.getRequest().redisKeyBuilder();
Response response = toCacheResults.get(redisKeyBuilder.getKey());
if (response != null) {
sknInfoRequestResponse.setResponse(response, true);
requestResponse.setResponse(response, true);
}
}
}
... ... @@ -134,31 +134,31 @@ public abstract class AbstractCacheBean<K extends ICacheRequest, V, T extends Ab
/**
* 将未缓存的response添加至缓存
*
* @param cacheRequestResponses
* @param requestResponses
*/
protected void batchAddResponseToCache(List<T> cacheRequestResponses) {
protected void batchAddResponseToCache(List<RequestResponse> requestResponses) {
//1、按缓存时间分组
Map<Integer, List<T>> groupMap = CollectionUtils.toListMap(cacheRequestResponses, new Transfer<T, Integer>() {
Map<Integer, List<RequestResponse>> groupMap = CollectionUtils.toListMap(requestResponses, new Transfer<RequestResponse, Integer>() {
@Override
public Integer transfer(T t) {
return t.getRequest().cacheTimeInSecond();
public Integer transfer(RequestResponse requestResponse) {
return requestResponse.getRequest().cacheTimeInSecond();
}
});
//2、按缓存时间大小直接加入缓存
for (Map.Entry<Integer, List<T>> entry : groupMap.entrySet()) {
for (Map.Entry<Integer, List<RequestResponse>> entry : groupMap.entrySet()) {
this.batchAddResponseToCache(entry.getValue(), entry.getKey());
}
}
private void batchAddResponseToCache(List<T> cacheRequestResponses,int timeOutInSecond) {
private void batchAddResponseToCache(List<RequestResponse> requestResponses,int timeOutInSecond) {
try {
Map<RedisKeyBuilder, String> toCacheMap = new HashMap<>();
for (T requestResponse : cacheRequestResponses) {
for (RequestResponse requestResponse : requestResponses) {
if (!requestResponse.isNeedRecache()) {
continue;
}
RedisKeyBuilder redisKeyBuilder = requestResponse.getRequest().redisKeyBuilder();
V response = requestResponse.getResponse();
Response response = requestResponse.getResponse();
if (redisKeyBuilder == null || response == null) {
continue;
}
... ...