测试代码
List<Integer> lids = new ArrayList<Integer>();
for(int i = 0; i < 10000; i++){
lids.add(i);
}
lids.add(8700);
lids.add(8703);
Map<Integer, Integer> map = new HashMap();
long startTime = System.currentTimeMillis();
map = playlistServiceClient.getPlaylistPlaytimsByLids(lids);
long endTime = System.currentTimeMillis();
logger.info((endTime - startTime) + " millseconds");
一次性去memcache多值的代码
/**
* 获取豆单的历史播发总数
*
* @param lid
* 豆单ID
* @return 个数
*/
public int getPlaylistPlaytimsByLid(int lid) {
Object cacheResult = ptsMemcacheClient.get(PLAYLIST_PTS_KEY_PL_HIS_PT + lid);
String key = PLAYLIST_PTS_KEY_PL_HIS_PT + lid;
if (cacheResult != null) {
return Integer.parseInt((String) cacheResult);
} else {
return -1;
}
}
/**
* 获取豆单列表的历史播发总数
*
* @param lids
* 豆单ID列表
* @return 个数
*/
public Map<Integer, Integer> getPlaylistPlaytimsByLids(List<Integer> lids) {
if (CollectionUtils.isEmpty(lids)) {
return Collections.EMPTY_MAP;
}
Map<Integer, Integer> result = new HashMap<Integer, Integer>();
int size = lids.size();
String[] keys = new String[lids.size()];
for (int i = 0; i < size; i++) {
keys[i] = PLAYLIST_PTS_KEY_PL_HIS_PT + lids.get(i);
}
Map<String, Object> cacheMap = ptsMemcacheClient.getMulti(keys);
for (int i = 0; i < size; i++) {
Object value = cacheMap.get(PLAYLIST_PTS_KEY_PL_HIS_PT + lids.get(i));
result.put(lids.get(i), (value == null) ? -1 : Integer.parseInt((String) value));
}
return result;
}
性能
2013-04-26 15:09:22 [com.tudou.programs.action.TestAction]-[INFO] lid : 99
2013-04-26 15:09:22 [com.tudou.programs.action.TestAction]-[INFO] 99: -1
2013-04-26 15:09:22 [com.tudou.programs.action.TestAction]-[INFO] 363 millseconds
2013-04-26 15:09:33 [com.tudou.programs.action.TestAction]-[INFO] lid : 8700
2013-04-26 15:09:33 [com.tudou.programs.action.TestAction]-[INFO] 8700: 33
2013-04-26 15:09:33 [com.tudou.programs.action.TestAction]-[INFO] 115 millseconds
2013-04-26 15:09:39 [com.tudou.programs.action.TestAction]-[INFO] lid : 8701
2013-04-26 15:09:39 [com.tudou.programs.action.TestAction]-[INFO] 8701: 33
2013-04-26 15:09:39 [com.tudou.programs.action.TestAction]-[INFO] 50 millseconds
2013-04-26 15:09:42 [com.tudou.programs.action.TestAction]-[INFO] lid : 8701
2013-04-26 15:09:42 [com.tudou.programs.action.TestAction]-[INFO] 8701: 33
2013-04-26 15:09:42 [com.tudou.programs.action.TestAction]-[INFO] 52 millseconds
2013-04-26 15:09:43 [com.tudou.programs.action.TestAction]-[INFO] lid : 8701
2013-04-26 15:09:43 [com.tudou.programs.action.TestAction]-[INFO] 8701: 33
2013-04-26 15:09:43 [com.tudou.programs.action.TestAction]-[INFO] 57 millseconds
2013-04-26 15:10:21 [com.tudou.programs.action.TestAction]-[INFO] lid : 10000
2013-04-26 15:10:21 [com.tudou.programs.action.TestAction]-[INFO] 10000: -1
2013-04-26 15:10:21 [com.tudou.programs.action.TestAction]-[INFO] 78 millseconds
多次从memcache取值的代码
/**
* 获取豆单的历史播发总数
*
* @param lid
* 豆单ID
* @return 个数
*/
public int getPlaylistPlaytimsByLid(int lid) {
Object cacheResult = ptsMemcacheClient.get(PLAYLIST_PTS_KEY_PL_HIS_PT + lid);
String key = PLAYLIST_PTS_KEY_PL_HIS_PT + lid;
if (cacheResult != null) {
return Integer.parseInt((String) cacheResult);
} else {
return -1;
}
}
/**
* 获取豆单列表的历史播发总数
*
* @param lids
* 豆单ID列表
* @return 个数
*/
public Map<Integer, Integer> getPlaylistPlaytimsByLids(List<Integer> lids) {
if (CollectionUtils.isEmpty(lids)) {
return Collections.EMPTY_MAP;
}
HashMap<Integer, Integer> resultMap = new HashMap<Integer, Integer>();
for (Integer lid : lids) {
resultMap.put(lid, getPlaylistPlaytimsByLid(lid));
}
return resultMap;
}
性能
2013-04-26 15:13:03 [com.tudou.programs.action.TestAction]-[INFO] lid : 10000
2013-04-26 15:13:03 [com.tudou.programs.action.TestAction]-[INFO] 10000: -1
2013-04-26 15:13:17 [com.tudou.programs.action.TestAction]-[INFO] 14206 millseconds
2013-04-26 15:13:27 [com.tudou.programs.action.TestAction]-[INFO] lid : 8700
2013-04-26 15:13:27 [com.tudou.programs.action.TestAction]-[INFO] 8700: 33
2013-04-26 15:13:41 [com.tudou.programs.action.TestAction]-[INFO] 13703 millseconds
2013-04-26 15:14:25 [com.tudou.programs.action.TestAction]-[INFO] lid : 99
2013-04-26 15:14:25 [com.tudou.programs.action.TestAction]-[INFO] 99: -1
2013-04-26 15:14:38 [com.tudou.programs.action.TestAction]-[INFO] 13457 millseconds
结论,一次取多值的性能要高于多次取值。
-----------------------------------------------------
Silence, the way to avoid many problems;
Smile, the way to solve many problems;