문제

I wanna perform a code which has pseudo-code like this:

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

The problem is: how can I check whether c.get("key") exists?

도움이 되었습니까?

해결책

The get(String key) method returns null if there is no key associated with the key.

Therefore, you can get the value, and check if it is null to know if it "exists":

Object myObject = c.get("key");
if(c == null) { // the object does not exist
    // add the value
} else {
    System.out.println(myObject);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top