Memcache and Memcached are actually the same thing, but if the extension used in php is different, one is php_memcache. dll and php_memcached.dll. Around 2009, someone enriched the usage and performance of memcache, and memcached was created. The usage has also been greatly improved, such as adding getMulti() to obtain keys in batches."/> Memcache and Memcached are actually the same thing, but if the extension used in php is different, one is php_memcache. dll and php_memcached.dll. Around 2009, someone enriched the usage and performance of memcache, and memcached was created. The usage has also been greatly improved, such as adding getMulti() to obtain keys in batches.">

Home >Backend Development >PHP Tutorial >Memcache/Memcached PHP operation manual (pure manuscript version) memcached java memcached official website redis memcached comparison

Memcache/Memcached PHP operation manual (pure manuscript version) memcached java memcached official website redis memcached comparison

WBOY
WBOYOriginal
2016-07-29 08:52:081082browse

data-id="1190000005001849" data-licence="">

Memcache and Memcached are actually the same thing, but if the extensions used in php are different, one is php_memcache.dll and php_memcached.dll. Around 2009, someone enriched the usage of memcache. and performance, memcached has been introduced, and its usage has been greatly improved, such as adding getMulti() to obtain key values ​​in batches


Memcache class

<code>$memcache = new Memcache;
</code>
<code>$memcache->connect('127.0.0.1', 11211);
</code>
<code>$memcache->pconnec('127.0.0.1', 11211); 
// 打开一个到服务器的持久化连接 , 连接不会在脚本执行结束后或者close()被调用后关闭
</code>
<code>$memcache->addServer('123.57.210.55', 11211,$persistent,$weight); 
// 向连接池中添加一个memcache服务器 $persistent 是否持久化连接 $weight 
   控制桶的数量提升被选中的权重 $timeout 表示连续持续时间
</code>
<code>$memcache->set('name', 'TK'); 
// 默认存储不压缩 不过期 , 其中字符串和数值直接存储,其他类型序列化后存储 
   set其实是add方法和replace方法集合
</code>
<code>$memcache->set('email', 'julylovin@163.com',MEMCACHE_COMPRESSED,5);
// MEMCACHE_COMPRESSED设置存储是否压缩 , 5表示5秒后过期但是最大只能设置2592000秒(30天)
   如果设置为0 表示永不过期, 可以设置将来的时间戳
</code>
<code>$memcache->set('info',array('age'=>'26','salary'=>'1000'));  
// 可以直接存储数组,redis中存储需要手动serialize()序列化
</code>
<code>$memcache->add('counter', '10', MEMCACHE_COMPRESSED, 0); 
//如果键值存在会返回false , 如果不存在, 和set方法一样,生成一个counter的key并赋值10
</code>
<code>$memcache->replace ('counter', '10');
 //如果键值不存在会返回false , 如果存在, 替换counter的值为10
</code>
<code>$memcache->increment('counter', 3); 
// 首先将元素当前值转换成数值然后减去value 操作counter键值+3 
   若键不存在 则返回false 不能用于压缩的键值操作,否则get键会失败
</code>
<code>$memcache->decrement('counter', 3); // 操作counter键值-3 , 若键不存在 则返回false
</code>
<code>$memcache->delete('counter', 3); // 操作删除键counter , 3表示3秒内删除,默认是0即立即删除
</code>
<code>$memcache->flush(); //flush()立即使所有已经存在的元素失效
</code>
<code>$memcache->getExtendedStats (); 
// 返回一个二维关联数据的服务器统计信息。数组的key由host:port方式组成
</code>
<code>$memcache->getServerStatus ('127.0.0.1'); // 获取返回一个服务器的在线/离线状态  0表示离线 非0在线
</code>
<code>$memcache->getStats(); // 获取服务器统计信息
</code>
<code>$memcache->getVersion(); // 返回服务器版本信息
</code>
<code>$memcache->setCompressThreshold ($threshold, $min_saving); 
//  开启大值自动压缩   $threshold设置压缩阀值 2000字节 ,即字节数大于2K 就压缩
    $min_saving  0--1之间  0.2表示压缩20%
</code>
<code>$memcache->setServerParams('memcache_host', 11211, 1, 15, true, '_callback_memcache_failure'); 
// $memcache->addServer('memcache_host', 11211, false, 1, 1, -1, false);
   已经通过addServer 配置过服务器 使用setServerParams 重新设置配置信息
</code>
rrreeerrreeerrreeerrreeerrreeerrreeerrreeerrreeerrreeerrreeerrreeerrreeerrreeer

The above introduces the PHP operation manual of Memcache/Memcached (pure manuscript version), including the content of Memcached. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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