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則是只能對已經存在的元素進行處理

TA貢獻1871條經驗 獲得超8個贊
memcache::add 方法:add方法用于向memcache服務器添加一個要緩存的數據。memcache::set 方法:set方法用于設置一個指定key的緩存內容,set方法是add方法和replace方法的集合體
set和add方法的不同之處是add方法不允許key值相同,如果第二次add的key相同,則存儲失敗,而set方法允許key相同,如果相同,則替換該key對應的value。
- 2 回答
- 0 關注
- 864 瀏覽
添加回答
舉報