public boolean delete(String key, Integer hashCode, Date expiry,boolean isReply) {
if (key == null) {
log.error("null value for key passed to delete()");
return false;
}
try {
key = sanitizeKey(key);
} catch (UnsupportedEncodingException e) {
// if we have an errorHandler, use its hook
if (errorHandler != null)
errorHandler.handleErrorOnDelete(this, e, key);
log.error("failed to sanitize your key!", e);
return false;
}
// get SockIO obj from hash or from key
SchoonerSockIO sock = pool.getSock(key, hashCode);
// return false if unable to get SockIO obj
if (sock == null) {
if (errorHandler != null)
errorHandler.handleErrorOnDelete(this, new IOException("no socket to server available"), key);
return false;
}
// build command
StringBuilder command = new StringBuilder("delete ").append(key);
if (expiry != null)
command.append(" " + expiry.getTime() / 1000);
/**如果不要需要用到服务器返回的结果最好是加上noreply
* 这样服务器之间同步缓存的时候就不会出现线程阻塞的情况**/
if(!isReply){
command.append(" noreply");
}
command.append("\r\n");
try {
sock.write(command.toString().getBytes());
//判断是否需要服务器返回结果
if(!isReply){
return true;
}
// if we get appropriate response back, then we return true
// get result code
String line = new SockInputStream(sock, Integer.MAX_VALUE).getLine();
if (DELETED.equals(line)) { // successful
if (log.isDebugEnabled())
log.debug(new StringBuffer().append("++++ deletion of key: ").append(key)
.append(" from cache was a success").toString());
return true;
} else if (NOTFOUND.equals(line)) { // key not found
if (log.isDebugEnabled())
log.debug(new StringBuffer().append("++++ deletion of key: ").append(key)
.append(" from cache failed as the key was not found").toString());
} else { // other error information
log.error(new StringBuffer().append("++++ error deleting key: ").append(key).toString());
log.error(new StringBuffer().append("++++ server response: ").append(line).toString());
}
} catch (IOException e) {
// if we have an errorHandler, use its hook
if (errorHandler != null)
errorHandler.handleErrorOnDelete(this, e, key);
// exception thrown
log.error("++++ exception thrown while writing bytes to server on delete");
log.error(e.getMessage(), e);
try {
sock.sockets.invalidateObject(sock);
} catch (Exception e1) {
log.error("++++ failed to close socket : " + sock.toString());
}
sock = null;
} finally {
if (sock != null) {
sock.close();
sock = null;
}
}
return false;
}?