exists
1. 基本語法
exist(key , callback)
key , callback) 2. 範例
(1) 確認key名稱是否已被使用
key名稱是否已被使用client.set('foo', 'bar');
client.exists('foo',redis.print);
client.exists('woo',redis.print);Reply: 1
Reply: 0(2) 情境:key值存在則讀取(get),不存在則新增(set)
key值存在則讀取(get),不存在則新增(set)const mykey = 'new_foo';
client.exists(mykey, (err, isExist)=>{
if (isExist){
console.log('[key exist]');
client.get(mykey, redis.print);
}else{
console.log('[new key]');
client.set(mykey, 'new_bar', redis.print);
}
}); Last updated