发布于 2015-06-28 02:31:02 | 361 次阅读 | 评论: 1 | 来源: 网络整理
Memcached的delete命令用于删除memcached服务器现有的键。
memcached delete命令的基本语法如下所示:
delete key
如果键成功删除,则返回DELETED,如果key没有找到则返回NOT_FOUND,否则返回ERROR。
set phperz 0 900 9
memcached
STORED
get phperz
VALUE phperz 0 9
memcached
END
delete phperz
DELETED
get phperz
END
delete phperz
NOT_FOUND
在上面的例子中,我们已经使用phperz作为键,并存储在memcached其900秒后失效,之后删除所存储的键。
要从memcached服务器中删除数据,需要使用memcached的delete方法。
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
//Connecting to Memcached server on localhost
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server sucessfully");
System.out.println("set status:"+mcc.set("phperz", 900, "memcached").done);
//Get value from cache
System.out.println("Get from Cache:"+mcc.get("phperz"));
//delete value from cache
System.out.println("Delete from Cache:"+mcc.delete("phperz").isDone());
//check whether value exists or not
System.out.println("Get from Cache:"+mcc.get("phperz"));
}
}
当上述程序编译和运行,它提供了以下的输出:
Connection to server successfully
set status:true
Get from Cache:memcached
Delete from Cache:true
Get from Cache:null