//冲突延时 1秒
private static final int MUTEX_EXP = 1;
//冲突键
private static final String MUTEX_KEY_PREFIX = "MUTEX_";
/**
* 冲突判定
* @param key
* @param exp
* @return true 冲突
*/
private boolean isMutex(String key) {
boolean status = true;
try {
if (xmemcachedClient.add(MUTEX_KEY_PREFIX + key, MUTEX_EXP, "true")) {
status = false;
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return status;
}
private List<RecommendDataView> getRecommendDataViewList(String key, int rid, int pageNum, int pageSize) {
List<RecommendDataView> recommendDataViewList = null;
recommendDataViewList = getCache(key);
if (recommendDataViewList == null) {
if (!isMutex(key)) {
recommendDataViewList = recommendDataService.getRecommendDataViewList(rid, pageNum, pageSize);
if (recommendDataViewList != null) {
cache(key, recommendDataViewList);
}
removeMutex(key);
} else {
while(true) {
try {
int interval = 0;
Thread.sleep(50);
recommendDataViewList = (List<RecommendDataView>)getCache(key);
interval++;
if (interval > 5 || recommendDataViewList != null) {
break;
}
} catch (InterruptedException e) {
logger.info(e.getMessage(), e);
}
}
}
}
return recommendDataViewList;
}
memcached高并发防止穿透db
最新推荐文章于 2018-09-17 11:09:44 发布