Home > Backend Development > PHP Tutorial > PHP memcached扩展的set和add方法

PHP memcached扩展的set和add方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:44:13
Original
1225 people have browsed it

<code>$sql = "SELECT * FROM typecho_contents WHERE cid='1' ";
$memc_key = md5($sql);

$memc= new Memcached();
$memc->addServer('127.0.0.1', '11211');

$memc_res = $memc->get($memc_key);

if($memc_res){
    echo $memc_res;
}else{
    //数据库查询得到$str
    $memc_res = $memc->set(memc_key,$str);
    //此时用set呢,还是应该$memc->getResultCode()查询上次错误信息,使用add呢?
}
</code>
Copy after login
Copy after login

set 和 add 的使用需要有意区分么,那我岂不是一律使用set就可以了么

回复内容:

<code>$sql = "SELECT * FROM typecho_contents WHERE cid='1' ";
$memc_key = md5($sql);

$memc= new Memcached();
$memc->addServer('127.0.0.1', '11211');

$memc_res = $memc->get($memc_key);

if($memc_res){
    echo $memc_res;
}else{
    //数据库查询得到$str
    $memc_res = $memc->set(memc_key,$str);
    //此时用set呢,还是应该$memc->getResultCode()查询上次错误信息,使用add呢?
}
</code>
Copy after login
Copy after login

set 和 add 的使用需要有意区分么,那我岂不是一律使用set就可以了么

http://serverfault.com/questions/291681/add-vs-set-in-memcached
简单的来说就是如果你不在意是否有这个key的话就直接用set吧。

setadd最显而易见的区别就是:
如果key是已经存在的set是更新原来的数据,而add则不会。

set偏向于赋值更新吧,add偏向于添加数据组。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template