我想执行一个具有如下伪代码的代码:

MemcachedClient c = new MemcachedClient(....)
if c.get("key") exists
  print(c.get("key"))
else
  c.add("key",expTime, value)


问题是:如何检查c.get("key")是否存在?

最佳答案

如果没有与键关联的键,get(String key)方法将返回null。

因此,您可以获取该值,并检查它是否为空以知道它是否“存在”:

Object myObject = c.get("key");
if(c == null) { // the object does not exist
    // add the value
} else {
    System.out.println(myObject);
}

关于java - 检查 key 是否存在于memcached中(spememchaced-Java memcached客户端),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19629787/

10-11 22:21
查看更多