Skip to content

Commit 7877c69

Browse files
committed
fix for clean keys in getMulti
1 parent cf4a568 commit 7877c69

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

src/com/danga/MemCached/MemCachedClient.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,19 +1417,19 @@ public Map<String,Object> getMulti( String[] keys, Integer[] hashCodes, boolean
14171417

14181418
for ( int i = 0; i < keys.length; ++i ) {
14191419

1420-
Integer hash = null;
1421-
if ( hashCodes != null && hashCodes.length > i )
1422-
hash = hashCodes[ i ];
1423-
14241420
String key = keys[i];
1425-
14261421
if ( key == null ) {
14271422
log.error( "null key, so skipping" );
14281423
continue;
14291424
}
14301425

1426+
Integer hash = null;
1427+
if ( hashCodes != null && hashCodes.length > i )
1428+
hash = hashCodes[ i ];
1429+
1430+
String cleanKey = key;
14311431
try {
1432-
key = sanitizeKey( key );
1432+
cleanKey = sanitizeKey( key );
14331433
}
14341434
catch ( UnsupportedEncodingException e ) {
14351435

@@ -1445,7 +1445,7 @@ public Map<String,Object> getMulti( String[] keys, Integer[] hashCodes, boolean
14451445
if ( pool == null )
14461446
pool = SockIOPool.getInstance( poolName );
14471447

1448-
SockIOPool.SockIO sock = pool.getSock( key, hash );
1448+
SockIOPool.SockIO sock = pool.getSock( cleanKey, hash );
14491449

14501450
if ( sock == null )
14511451
continue;
@@ -1454,7 +1454,7 @@ public Map<String,Object> getMulti( String[] keys, Integer[] hashCodes, boolean
14541454
if ( !cmdMap.containsKey( sock.getHost() ) )
14551455
cmdMap.put( sock.getHost(), new StringBuilder( "get" ) );
14561456

1457-
cmdMap.get( sock.getHost() ).append( " " + key );
1457+
cmdMap.get( sock.getHost() ).append( " " + cleanKey );
14581458

14591459
// return to pool
14601460
sock.close();
@@ -1469,6 +1469,29 @@ public Map<String,Object> getMulti( String[] keys, Integer[] hashCodes, boolean
14691469
// now use new NIO implementation
14701470
(new NIOLoader()).loadItemsNIO( asString, cmdMap, keys, ret );
14711471

1472+
// fix the return array in case we had to rewrite any of the keys
1473+
for ( String key : keys ) {
1474+
1475+
String cleanKey = key;
1476+
try {
1477+
cleanKey = sanitizeKey( key );
1478+
}
1479+
catch ( UnsupportedEncodingException e ) {
1480+
1481+
// if we have an errorHandler, use its hook
1482+
if ( errorHandler != null )
1483+
errorHandler.handleErrorOnGet( this, e, key );
1484+
1485+
log.error( "failed to sanitize your key!", e );
1486+
continue;
1487+
}
1488+
1489+
if ( ! key.equals( cleanKey ) && ret.containsKey( cleanKey ) ) {
1490+
ret.put( key, ret.get( cleanKey ) );
1491+
ret.remove( cleanKey );
1492+
}
1493+
}
1494+
14721495
log.debug( "++++ memcache: got back " + ret.size() + " results" );
14731496
return ret;
14741497
}

0 commit comments

Comments
 (0)