In this article, Pig Mastiff shares with you how to use the Memcached command in PHP and makes a summary. I hope it can help everyone.
Portal: http://www.php.net/manual/zh/book.memcached.php
There is no memcached extension under windows, only memcache extension. Personal test, there is still a big difference between the two. So it is recommended to do it in linux.
<?php $mem = new Memcached(); //添加一台服务器资源 $mem->addServer('127.0.0.1', '11211'); //添加多台,分布式存储,第三个参数为权重值 /* $servers = array( array('127.0.0.1', 11211, 33), array('127.0.0.2', 11211, 67), ); $res = $mem->addServers($servers); */ //设置:键 值 过期时间(秒) $mem->set('name', 'huangyuxin', 5); //注意:最大生命周期可设置为60*60*24*30 三十天的时间 //再往后的话要加上时间戳 time()+60*60*24*31(三十一天) //获取值 $value = $mem->get('name'); //添加值,如果存在此键,false $result = $mem->add('name','zhangsan'); //追加: 键 值 ,追加在一个已经存在的值得后面,不存在也为false //setOption 这一句必须加上,不然追加不上 //prepend 前面追加 //如果Memcached::OPT_COMPRESSION常量开启,这个操作会失败,并引发一个警告,因为向压缩数据 后追加数据可能会导致解压不了。 $mem->setOption(Memcached::OPT_COMPRESSION, false); $mem->append('name','haha'); $value = $mem->get('name'); //这个是减掉元素的值,两个参数,第二个参数决定减掉数值几,默认是 1 ,increment 是加 $mem->set('age', 12, 30); $mem->decrement('age'); $mem->decrement('age',2); $value = $mem->get('age'); //删除元素 $mem->delete('age'); $mem->delete('age',60); /* 注意: 服务端在这段时间拒绝对这个key的add和replace命令. 由于这个时间段的存在, 元素被放入一个删除队列 表明它不可以通过get命令获取到值 但是同时 add和replace命令也从服务端内存删除 (表明元素会被立即删除并且之后对这个 key的存储命令也会成功) */ //删除多个 $mem->add('age', 12, 60); $mem->add('name', 'huangyuxin', 60); $res = $mem->deleteMulti(array('age','name')); //作废 :flush不会 真正的释放已有元素的内存, 而是逐渐的存入新元素重用那些内存。 $mem->flush(10);//10秒内清除元素 //获取所有键 $mem->getAllKeys(); /* Memcached::getDelayed()向Memcached服务端发出一个检索 keys指定的多个 key对应元素的请求。这个方法不会等待响应而 是立即返回。当你需要收集元素值时, 调Memcached::fetch() 或 Memcached::fetchAll()。如果with_cas设置为true,会 同时请求每个元素的CAS标记。 */ $m->set('int', 99); $m->set('array', array(11, 12)); $m->getDelayed(array('int', 'array'), true); var_dump($m->fetchAll()); //获取多个值的信息 $mem->set('age', 12, 60); $mem->set('name', 'huangyuxin', 60); $res = $mem->getMulti(array('age', 'name')); //设置多个键 $items = array( 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', ); $mem->setMulti($items); $res = $mem->get('key1');//value //返回系统常量 var_dump($mem->getOption(Memcached::OPT_COMPRESSION)); //返回最后一次操作的结果描述消息 $mem->add('a', 'bar'); // first time should succeed echo $mem->getResultMessage(), "\n"; //SUCCESS //查看此key在哪个服务器上 $mem->add('a', 'bar'); // first time should succeed $res = $mem->getServerByKey('a'); //array(3) { ["host"]=> string(9) "127.0.0.1" ["port"]=> int(11211) ["weight"]=> int(0) } //返回服务器列表 var_dump($mem->getServerList()); //返回服务器状态 var_dump($mem->getServerList()); //服务器版本 print_r($mem->getVersion()); //判断是否是持久链接 $res = $mem->isPersistent(); //Memcached::replace()和Memcached::set()类似,但是如果 服务端不存在key, 操作将失败。 $m->set('hh', 'aaaa'); $m->replace('hh', 'bbbb'); $res = $m->get('hh'); //删除从已知的服务器列表中的所有缓存服务器,重置回空。 $mem->resetServerList(); //对某一key重新设置生命周期 $m->set('aaaa', 'aaaa', 600); $m->touch('aaaa', 5); $value= $m->get('aaaa'); //关闭打开的链接 $m->quit(); var_dump($value);
The following suffix is ByKey, which is generally used by multiple Memcached Servers. If you master the above commands, you will basically use them below.
touch->touchByKey setMulti->setMultiByKey getMulti->getMultiBykey replace->replaceByKey append->appendByKey prepend->prependByKey getServerByKey getdelay->getDelayedByKey increment->incrementByKey decrement->decrementByKey add->addByKey get->getByKey delete->deleteMultiByKey
$m->addByKey('指定服务器','键',"值")
Waiting more than taking action is equal to waiting to die.
Portal: http://www.php.net/manual/zh/book.memcached.php
There is no memcached extension under windows, only memcache extension. Personal test, there is still a big difference between the two. So it is recommended to do it in linux.
<?php $mem = new Memcached(); //添加一台服务器资源 $mem->addServer('127.0.0.1', '11211'); //添加多台,分布式存储,第三个参数为权重值 /* $servers = array( array('127.0.0.1', 11211, 33), array('127.0.0.2', 11211, 67), ); $res = $mem->addServers($servers); */ //设置:键 值 过期时间(秒) $mem->set('name', 'huangyuxin', 5); //注意:最大生命周期可设置为60*60*24*30 三十天的时间 //再往后的话要加上时间戳 time()+60*60*24*31(三十一天) //获取值 $value = $mem->get('name'); //添加值,如果存在此键,false $result = $mem->add('name','zhangsan'); //追加: 键 值 ,追加在一个已经存在的值得后面,不存在也为false //setOption 这一句必须加上,不然追加不上 //prepend 前面追加 //如果Memcached::OPT_COMPRESSION常量开启,这个操作会失败,并引发一个警告,因为向压缩数据 后追加数据可能会导致解压不了。 $mem->setOption(Memcached::OPT_COMPRESSION, false); $mem->append('name','haha'); $value = $mem->get('name'); //这个是减掉元素的值,两个参数,第二个参数决定减掉数值几,默认是 1 ,increment 是加 $mem->set('age', 12, 30); $mem->decrement('age'); $mem->decrement('age',2); $value = $mem->get('age'); //删除元素 $mem->delete('age'); $mem->delete('age',60); /* 注意: 服务端在这段时间拒绝对这个key的add和replace命令. 由于这个时间段的存在, 元素被放入一个删除队列 表明它不可以通过get命令获取到值 但是同时 add和replace命令也从服务端内存删除 (表明元素会被立即删除并且之后对这个 key的存储命令也会成功) */ //删除多个 $mem->add('age', 12, 60); $mem->add('name', 'huangyuxin', 60); $res = $mem->deleteMulti(array('age','name')); //作废 :flush不会 真正的释放已有元素的内存, 而是逐渐的存入新元素重用那些内存。 $mem->flush(10);//10秒内清除元素 //获取所有键 $mem->getAllKeys(); /* Memcached::getDelayed()向Memcached服务端发出一个检索 keys指定的多个 key对应元素的请求。这个方法不会等待响应而 是立即返回。当你需要收集元素值时, 调Memcached::fetch() 或 Memcached::fetchAll()。如果with_cas设置为true,会 同时请求每个元素的CAS标记。 */ $m->set('int', 99); $m->set('array', array(11, 12)); $m->getDelayed(array('int', 'array'), true); var_dump($m->fetchAll()); //获取多个值的信息 $mem->set('age', 12, 60); $mem->set('name', 'huangyuxin', 60); $res = $mem->getMulti(array('age', 'name')); //设置多个键 $items = array( 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', ); $mem->setMulti($items); $res = $mem->get('key1');//value //返回系统常量 var_dump($mem->getOption(Memcached::OPT_COMPRESSION)); //返回最后一次操作的结果描述消息 $mem->add('a', 'bar'); // first time should succeed echo $mem->getResultMessage(), "\n"; //SUCCESS //查看此key在哪个服务器上 $mem->add('a', 'bar'); // first time should succeed $res = $mem->getServerByKey('a'); //array(3) { ["host"]=> string(9) "127.0.0.1" ["port"]=> int(11211) ["weight"]=> int(0) } //返回服务器列表 var_dump($mem->getServerList()); //返回服务器状态 var_dump($mem->getServerList()); //服务器版本 print_r($mem->getVersion()); //判断是否是持久链接 $res = $mem->isPersistent(); //Memcached::replace()和Memcached::set()类似,但是如果 服务端不存在key, 操作将失败。 $m->set('hh', 'aaaa'); $m->replace('hh', 'bbbb'); $res = $m->get('hh'); //删除从已知的服务器列表中的所有缓存服务器,重置回空。 $mem->resetServerList(); //对某一key重新设置生命周期 $m->set('aaaa', 'aaaa', 600); $m->touch('aaaa', 5); $value= $m->get('aaaa'); //关闭打开的链接 $m->quit(); var_dump($value);
The following suffix is ByKey, which is generally used by multiple Memcached Servers. If you master the above commands, you will basically use them below.
touch->touchByKey setMulti->setMultiByKey getMulti->getMultiBykey replace->replaceByKey append->appendByKey prepend->prependByKey getServerByKey getdelay->getDelayedByKey increment->incrementByKey decrement->decrementByKey add->addByKey get->getByKey delete->deleteMultiByKey
$m->addByKey('指定服务器','键',"值")
Related recommendations:
How to install Memcached service method in linux
Ubuntu memcached installation and configuration method in php
PHP memory caching function memcached example
The above is the detailed content of How to use Memcached commands in PHP. For more information, please follow other related articles on the PHP Chinese website!

Memcached是一种常用的缓存技术,它可以使Web应用程序的性能得到很大的提升。在PHP中,常用的Session处理方式是将Session文件存放在服务器的硬盘上。但是,这种方式并不是最优的,因为服务器的硬盘会成为性能瓶颈之一。而使用Memcached缓存技术可以对PHP中的Session处理进行优化,提高Web应用程序的性能。PHP中的Session处

PHP8.0中的缓存库:Memcached随着互联网的快速发展,现代应用程序需要高效可靠的缓存技术来提高性能和处理大量数据。由于PHP的流行和开源特性,PHP缓存库已经成为了Web开发社区的一个必备工具。Memcached是一种广泛使用的开源高速内存缓存系统,它能处理数百万个同时连接的缓存请求,可以用于许多不同类型的应用程序,例如社交网络、在线

随着互联网技术的不断发展,音视频资源已经成为了互联网上非常重要的一种内容形式,而PHP作为网络开发中使用最广泛的语言之一,也在不断地应用于视频和音频播放领域。然而,随着音视频网站的用户日益增加,许多网站已经发现了一个问题:在高并发的情况下,PHP对于音视频的处理速度明显变缓,会导致无法及时播放或者播放卡顿等问题。为了解决这个问题,Memcached缓存技术应

随着互联网的快速发展,大规模MySQL数据库备份和恢复成为各大企业和网站必备的技能之一。而随着Memcached的广泛应用,如何备份和恢复Memcached也成为了一个重要的问题。PHP作为Web开发的主力语言之一,在处理备份和恢复MySQL和Memcached上拥有独特的优势和技巧。本文将详细介绍PHP处理MySQL和Memcached备份与恢复的实现方法

随着网络应用的不断增加和数据量的不断膨胀,数据的读写效率成为影响应用性能的重要因素之一。而缓存技术的应用则可以很好地解决这个问题。在PHP应用中,Memcached是最常用的缓存服务器。Memcached是一个高性能的分布式内存对象缓存系统,可以将常用的数据存储在内存中,提高数据检索的效率。本文将介绍如何使用PHP和Memcached进行缓存管理,以及如何优

随着现代互联网应用的快速发展,用户体验对于一个应用的成功至关重要。如何保证应用的高性能和高可用性,成为了开发人员需要解决的重要问题之一。PHP作为一种广泛应用的编程语言之一,它的性能监控和优化也是非常重要的。Memcached是一个高性能、分布式的内存对象缓存系统,可以帮助应用提高性能和扩展性。本文将介绍如何使用PHP和Memcached实现性能监控的方法。

随着互联网的发展,缓存技术在Web开发中扮演着越来越重要的角色。Redis和Memcached作为两种流行的缓存服务器,被广泛应用于各种Web应用开发中。然而,对于不熟悉Linux系统的开发人员来说,安装和配置这些缓存服务器可能会带来一些麻烦。但是,在宝塔面板的帮助下,这一过程将变得相当简单。一、什么是宝塔面板?宝塔面板是一款Linux服务器管理面板,它可以

Memcached是一种分布式内存缓存技术,被广泛应用于Web应用程序中。它可以帮助Web应用程序提高性能,减少数据库负载,加速数据访问,从而提高用户体验。在PHP中,使用Memcached技术也变得越来越流行。本文将介绍在PHP中如何使用Memcached技术。一、什么是Memcached技术Memcached是一种基于内存的缓存技术,它可以将数据缓存在内


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
