为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 791 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信