memcache中set和replace方法的区别

memcache中set和replace方法的区别


慕姐8265434
浏览 794回答 2
2回答

千巷猫影

 在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则是只能对已经存在的元素进行处理

ITMISS

memcache::add 方法:add方法用于向memcache服务器添加一个要缓存的数据。memcache::set 方法:set方法用于设置一个指定key的缓存内容,set方法是add方法和replace方法的集合体set和add方法的不同之处是add方法不允许key值相同,如果第二次add的key相同,则存储失败,而set方法允许key相同,如果相同,则替换该key对应的value。
打开App,查看更多内容
随时随地看视频慕课网APP