memcached java client

memcached java client

1. http://www.whalin.com/memcached/ v2.0.1
2. http://code.google.com/p/spymemcached/ v2.2

a. 使用whalin版会导致File Descriptor leak,而使用spy版则不会,原因是whalin版没有使用selector管理socketchannel.
       Thread.sleep( 1000   *   30 );
        System.out.println(
" begin " );

        Selector selector 
=   null ;
        SocketChannel channel 
=   null ;
        
try  {
            String host 
=   " 192.168.0.74 " ;
            
int  port  =   11211 ;
            
int  timeout  =   1000   *   60 ;

            selector 
=  Selector.open();
            channel 
=  SocketChannel.open();
            channel.configureBlocking(
false );

            channel.connect(
new  InetSocketAddress(host, port));
            channel.register(selector, channel.validOps());

            
try  {
                selector.select();
            } 
catch  (IOException e) {
                e.printStackTrace();
            }

            Iterator it 
=  selector.selectedKeys().iterator();
            
int  i  =   0 ;
            
while  (it.hasNext()) {
                i
++ ;
                System.out.println(i);
                SelectionKey selKey 
=  (SelectionKey) it.next();
                it.remove();

                
try  {
                    processSelectionKey(selKey);
                } 
catch  (IOException e) {
                    e.printStackTrace();
                    selKey.cancel();
                }
            }

            System.out.println(
" unclose " );
            Thread.sleep(
1000   *   30 );
        } 
catch  (Exception e) {
            e.printStackTrace();
        } 
finally  {
            
if  (channel  !=   null   &&  channel.isOpen()) {
                
try  {
                    channel.close();
                } 
catch  (Exception e) {
                    e.printStackTrace();
                }
            }
            
if  (selector != null ) {
                selector.close();      
//  not fd leak
            }
        }

        System.out.println(
" end " );
        Thread.sleep(
1000   *   30   *   1 );
通过lsof -p pid | grep pipe可以观察是否有fd leak.

b. w版set 1000000 object 需要600s左右,s版只需150s左右

你可能感兴趣的:(memcached java client)