亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

memcache中set和replace方法的區別

memcache中set和replace方法的區別

慕姐8265434 2019-04-14 12:08:53
memcache中set和replace方法的區別
查看完整描述

2 回答

?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

 在memcache中replace和set在一定程度上作用是一致的,都是改變某個元素的值,但是之間略有不同。
  我們來用例子說明

  $mem=new Memcache;
  $mem->connect("localhost", 11211);
  //直接set
  $mem->set("mystr1", "this is a memcache test1!", MEMCACHE_COMPRESSED, 3600);
  echo $str=$mem->get("mystr1");
  //直接replace
  $mem->replace("mystr2", "this is a memcache test2!", MEMCACHE_COMPRESSED, 3600);
  var_dump($str=$mem->get("mystr2"));
  //先add在replace
  $mem->add("mystr3", "this is a memcache test3!", MEMCACHE_COMPRESSED, 3600);
  echo $str=$mem->get("mystr3");
  $mem->replace("mystr3", "this is a memcache test31!", MEMCACHE_COMPRESSED, 3600);
  echo $str=$mem->get("mystr31");
  //先add在set
  $mem->add("mystr4", "this is a memcache test4!", MEMCACHE_COMPRESSED, 3600);
  echo $str=$mem->get("mystr4");
  $mem->replace("mystr4", "this is a memcache test41!", MEMCACHE_COMPRESSED, 3600);
  echo $str=$mem->get("mystr41");

  運行結果這里我們就不寫出了,個人強烈建議你運行一下,這樣可以加深印象!
  最終的結論就是在對已有值的元素處理上兩者是相同的,但是對于一個不存在的元素,set的作用就和add相當,replace則是只能對已經存在的元素進行處理



查看完整回答
反對 回復 2019-04-15
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

memcache::add 方法:add方法用于向memcache服務器添加一個要緩存的數據。memcache::set 方法:set方法用于設置一個指定key的緩存內容,set方法是add方法和replace方法的集合體
set和add方法的不同之處是add方法不允許key值相同,如果第二次add的key相同,則存儲失敗,而set方法允許key相同,如果相同,則替換該key對應的value。

查看完整回答
反對 回復 2019-04-15
  • 2 回答
  • 0 關注
  • 864 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號