memcache/memcached PHP Operating Manual (pure manuscript version) memcached Java memcached official website Redis memcached Comparison

Source: Internet
Author: User
Data-id= "1190000005001849" data-licence= "" >

Memcache and memcached is actually a thing, just php if the extension is not the same one is Php_memcache.dll and Php_memcached.dll, 2009 years or so someone rich memcache usage and performance, Have memcached, usage also has a great improvement such as adding getmulti () Bulk Get key value

Memcache class

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

The above describes the memcache/memcached PHP operating manual (pure manuscript version), including the content of the memcached, I hope to be interested in PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.