Skip to content

Commit ce214c1

Browse files
committed
use nio loader for single get
1 parent 7877c69 commit ce214c1

File tree

1 file changed

+17
-42
lines changed

1 file changed

+17
-42
lines changed

src/com/danga/MemCached/MemCachedClient.java

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,50 +1262,23 @@ public Object get( String key, Integer hashCode, boolean asString ) {
12621262
if ( sock == null )
12631263
return null;
12641264

1265-
try {
1266-
String cmd = String.format( "get %s\r\n", key );
1267-
log.debug( "++++ memcache get command: " + cmd );
1268-
1269-
sock.write( cmd.getBytes() );
1270-
sock.flush();
1271-
1272-
// build empty map
1273-
// and fill it from server
1274-
Map<String,Object> hm =
1275-
new HashMap<String,Object>();
1276-
loadItems( sock, hm, asString );
1277-
1278-
// debug code
1279-
log.debug( "++++ memcache: got back " + hm.size() + " results" );
1280-
1281-
// return the value for this key if we found it
1282-
// else return null
1283-
sock.close();
1284-
return hm.get( key );
1285-
}
1286-
catch ( IOException e ) {
1287-
1288-
// if we have an errorHandler, use its hook
1289-
if ( errorHandler != null )
1290-
errorHandler.handleErrorOnGet( this, e, key );
1265+
Map<String,StringBuilder> cmdMap =
1266+
new HashMap<String,StringBuilder>();
12911267

1292-
// exception thrown
1293-
log.error( "++++ exception thrown while trying to get object from cache for key: " + key );
1294-
log.error( e.getMessage(), e );
1268+
cmdMap.put( sock.getHost(),
1269+
new StringBuilder( String.format( "get %s\r\n", key ) ) );
12951270

1296-
try {
1297-
sock.trueClose();
1298-
}
1299-
catch ( IOException ioe ) {
1300-
log.error( "++++ failed to close socket : " + sock.toString() );
1301-
}
1302-
sock = null;
1303-
}
1271+
sock.close();
13041272

1305-
if ( sock != null )
1306-
sock.close();
1273+
// build empty map
1274+
// and fill it from server
1275+
Map<String,Object> hm =
1276+
new HashMap<String,Object>();
1277+
(new NIOLoader()).loadItemsNIO( asString, cmdMap, new String[] { key }, hm );
13071278

1308-
return null;
1279+
// return the value for this key if we found it
1280+
// else return null
1281+
return ( hm.containsKey( key ) ) ? hm.get( key ) : null;
13091282
}
13101283

13111284
/**
@@ -1490,6 +1463,10 @@ public Map<String,Object> getMulti( String[] keys, Integer[] hashCodes, boolean
14901463
ret.put( key, ret.get( cleanKey ) );
14911464
ret.remove( cleanKey );
14921465
}
1466+
1467+
// backfill missing keys w/ null value
1468+
if ( ! ret.containsKey( key ) )
1469+
ret.put( key, null );
14931470
}
14941471

14951472
log.debug( "++++ memcache: got back " + ret.size() + " results" );
@@ -2026,7 +2003,6 @@ public void loadItemsNIO( boolean asString, Map<String, StringBuilder> sockKeys,
20262003

20272004
// get the sockets, flip them to non-blocking, and set up data
20282005
// structures
2029-
//
20302006
conns = new Connection[sockKeys.keySet().size()];
20312007
numConns = 0;
20322008
for ( Iterator<String> i = sockKeys.keySet().iterator(); i.hasNext(); ) {
@@ -2097,7 +2073,6 @@ public void loadItemsNIO( boolean asString, Map<String, StringBuilder> sockKeys,
20972073
// Done! Build the list of results and return them. If we get
20982074
// here by a timeout, then some of the connections are probably
20992075
// not done. But we'll return what we've got...
2100-
//
21012076
for ( Connection c : conns ) {
21022077
try {
21032078
if ( c.incoming.size() > 0 && c.isDone() )

0 commit comments

Comments
 (0)