@Test public void testSetMc() { logger.debug("test set value to mc"); for(int i = 1; i <= 20; i++) { memCachedClient.set("test-"+i, "test value " + i); } }
mc配置了8個服務,就是說有8個節點,以上代碼設置20個緩存,key都是以「test-」做爲前綴的,但緩存會分散在8個服務器上[set(key, value)]會以key的hash值算出存放節點。html
@Test public void testMultiGet() { String keyPrefix = "test-"; String[] keys = new String[20]; for(int i = 0; i < 20; i++) { keys[i] = keyPrefix + (i+1); } Object[] values = memCachedClient.getMultiArray(keys); if(values != null) { for(Object o : values) { logger.debug("value: {}", o); } } } //實際從mc服務器獲取命令 節點1 => get test-5 test-10 test-18 節點2 => get test-1 test-9 test-14 節點3 => get test-8 test-13 節點4 => get test-6 test-11 test-19 節點5 => get test-3 test-16 節點6 => get test-4 test-17 test-20 節點7 => get test-2 test-15 節點8 => get test-7 test-12
從上面getMulti能夠看出要那20個值須要鏈接8次mc服務器,驗證過客戶端是串行的,就是說節點1鏈接獲取數據返回後在鏈接節點2,依次類推,可想而知若是mc服務器的節點在增長呢,每次獲取的數量不是20是200或者更多呢?java
1 -> loadMulti before: java.nio.HeapByteBuffer[pos=111 lim=8192 cap=8192] ret(3): {test-10=test value 10, test-5=test value 5, test-18=test value 18} 1 -> loadMulti after: java.nio.HeapByteBuffer[pos=0 lim=8192 cap=8192] 2 -> loadMulti before: java.nio.HeapByteBuffer[pos=109 lim=8192 cap=8192] ret(6): {test-9=test value 9, test-10=test value 10, test-5=test value 5, test-14=test value 14, test-1=test value 1, test-18=test value 18} 2 -> loadMulti after: java.nio.HeapByteBuffer[pos=0 lim=8192 cap=8192] 3 -> loadMulti before: java.nio.HeapByteBuffer[pos=75 lim=8192 cap=8192] ret(8): {test-9=test value 9, test-10=test value 10, test-8=test value 8, test-5=test value 5, test-13=test value 13, test-14=test value 14, test-1=test value 1, test-18=test value 18} 3 -> loadMulti after: java.nio.HeapByteBuffer[pos=0 lim=8192 cap=8192] 4 -> loadMulti before: java.nio.HeapByteBuffer[pos=111 lim=8192 cap=8192] ret(11): {test-9=test value 9, test-10=test value 10, test-11=test value 11, test-8=test value 8, test-5=test value 5, test-6=test value 6, test-13=test value 13, test-14=test value 14, test-1=test value 1, test-19=test value 19, test-18=test value 18} 4 -> loadMulti after: java.nio.HeapByteBuffer[pos=0 lim=8192 cap=8192] 5 -> loadMulti before: java.nio.HeapByteBuffer[pos=75 lim=8192 cap=8192] ret(13): {test-9=test value 9, test-10=test value 10, test-11=test value 11, test-8=test value 8, test-5=test value 5, test-6=test value 6, test-13=test value 13, test-3=test value 3, test-14=test value 14, test-1=test value 1, test-19=test value 19, test-18=test value 18, test-16=test value 16} 5 -> loadMulti after: java.nio.HeapByteBuffer[pos=0 lim=8192 cap=8192] 6 -> loadMulti before: java.nio.HeapByteBuffer[pos=111 lim=8192 cap=8192] ret(16): {test-9=test value 9, test-10=test value 10, test-11=test value 11, test-8=test value 8, test-5=test value 5, test-6=test value 6, test-13=test value 13, test-3=test value 3, test-14=test value 14, test-20=test value 20, test-4=test value 4, test-1=test value 1, test-19=test value 19, test-18=test value 18, test-17=test value 17, test-16=test value 16} 6 -> loadMulti after: java.nio.HeapByteBuffer[pos=0 lim=8192 cap=8192] 7 -> loadMulti before: java.nio.HeapByteBuffer[pos=75 lim=8192 cap=8192] ret(18): {test-9=test value 9, test-10=test value 10, test-11=test value 11, test-8=test value 8, test-5=test value 5, test-6=test value 6, test-13=test value 13, test-3=test value 3, test-14=test value 14, test-15=test value 15, test-20=test value 20, test-4=test value 4, test-1=test value 1, test-2=test value 2, test-19=test value 19, test-18=test value 18, test-17=test value 17, test-16=test value 16} 7 -> loadMulti after: java.nio.HeapByteBuffer[pos=0 lim=8192 cap=8192] 8 -> loadMulti before: java.nio.HeapByteBuffer[pos=75 lim=8192 cap=8192] ret(20): {test-9=test value 9, test-7=test value 7, test-10=test value 10, test-11=test value 11, test-8=test value 8, test-12=test value 12, test-5=test value 5, test-6=test value 6, test-13=test value 13, test-3=test value 3, test-14=test value 14, test-15=test value 15, test-20=test value 20, test-4=test value 4, test-1=test value 1, test-2=test value 2, test-19=test value 19, test-18=test value 18, test-17=test value 17, test-16=test value 16} 8 -> loadMulti after: java.nio.HeapByteBuffer[pos=0 lim=8192 cap=8192]
這樣看來若是mc數據量大的話增長節點不必定是解決問題,原先只須要一次鏈接的,如今要鏈接不少次,光網絡io就多是個新的問題,並且這個客戶端又是串行鏈接返回的。這就是著名的mutiGet無底洞了。緩存