search

windows环境:添加服务.sc create Memcachedserver11212 binpath= "C:\memcache\memcached.exe -d runservice -m 500 -p 11212" start= auto displayname= "Memcached server (11212)"要是一台机有多个,那就改下端口再搞一次撒~php使用Memcache函数库是在PECL(PHP Extension Community Library)中,主要作用是搭建大容量的内存数据的临时存放区域,在分布式的时候作用体现的非常明显,否则不建议使用。按照:《libeven、memcached、libmemcache安装》中的方法,使用:sudo ln -s /usr/local/lib/libevent-1.4.so.2 /usr/lib/libevent-1.4.so.2可以修正这个BUG通过新得立安装php的memcached模块,注销/etc/php5/conf.d/memcached.ini里面的“;”,重启apache,调用phpinfo()出现memcached的信息执行:/usr/local/bin/memcached -d -m 10 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pidmemcached的服务正式启动Memcache::add ? 添加一个值,如果已经存在,则返回falseMemcache::addServer ? 添加一个可供使用的服务器地址Memcache::close ? 关闭一个Memcache对象Memcache::connect ? 创建一个Memcache对象memcache_debug ? 控制调试功能Memcache::decrement ? 对保存的某个key中的值进行减法操作Memcache::delete ? 删除一个key值Memcache::flush ? 清除所有缓存的数据Memcache::get ? 获取一个key值Memcache::getExtendedStats ? 获取进程池中所有进程的运行系统统计Memcache::getServerStatus ? 获取运行服务器的参数Memcache::getStats ? 返回服务器的一些运行统计信息Memcache::getVersion ? 返回运行的Memcache的版本信息Memcache::increment ? 对保存的某个key中的值进行加法操作Memcache::pconnect ? 创建一个Memcache的持久连接对象Memcache::replace ? R对一个已有的key进行覆写操作Memcache::set ? 添加一个值,如果已经存在,则覆写Memcache::setCompressThreshold ? 对大于某一大小的数据进行压缩Memcache::setServerParams ? 在运行时修改服务器的参数建议用面向对象的方式来测试这个库:<?php  $memcache = new Memcache;  $memcache->connect('localhost', 11211) or die ("Could not connect");  $version = $memcache->getVersion();  echo "Server's version: ".$version."\n";  ?>  <?php$memcache = new Memcache; $memcache->connect('localhost', 11211) or die ("Could not connect");$version = $memcache->getVersion();echo "Server's version: ".$version." \n";?>Memcache::getVersion方法的作用是返回运行的Memcache的版本信息。Memcache::getStats 方法的作用是返回服务器的一些运行统计信息。Memcache::getStats方法有三个参数,第一个参数表示要求返回的类型:reset, malloc, maps, cachedump, slabs, items, sizes;第二个参数和第三个参数是在第一个参数设置为“cachedump”时使用的。Memcache::getExtendedStats方法的 作用是获取进程池中所有进程的运行系统统计。<?php  $memcache = new Memcache;  $memcache->connect('localhost', 11211) or die ("Could not connect");  print_r($memcache->getStats());    ?>  <?php$memcache = new Memcache; $memcache->connect('localhost', 11211) or die ("Could not connect");print_r($memcache->getStats()); ?>Memcache::connect方法的作用是创建一个Memcache对象。Memcache::pconnect方法的作用是创建一个Memcache的持久连接对象。Memcache::close方法的作用是关闭一个Memcache对象。Memcache::set 方法的作用是添加一个值,Memcache::set方法有四个参数,第一个参数是key,第二个参数是value,第三个参数可选,表示是否压缩保存, 第四个参数可选,用来设置一个过期自动销毁的时间。Memcache::add方法的作用和Memcache::set方法类似,区别是如果 Memcache::add方法的返回值为false,表示这个key已经存在,而Memcache::set方法则会直接覆写。 Memcache::get方法的作用是获取一个key值,Memcache::get方法有一个参数,表示key。Memcache::replace 方法的作用是对一个已有的key进行覆写操作,Memcache::replace方法有四个参数,作用和Memcache::set方法的相同。 Memcache::delete方法的作用是删除一个key值,Memcache::delete方法有两个参数,第一个参数表示key,第二个参数可 选,表示删除延迟的时间。<?php  $memcache = new Memcache;  $memcache->connect('localhost', 11211) or die ("Could not connect");  $memcache->set( 'name', 'leo', 0, 30);  if(!$memcache->add( 'name', 'susan', 0, 30))  {      echo 'susan is exist';  };  $memcache->replace( 'name', 'lion', 0, 300);  echo $memcache->get( 'name');  $memcache->delete( 'name', 5);  ?>  <?php$memcache = new Memcache;$memcache->connect('localhost', 11211) or die ("Could not connect");$memcache->set( 'name', 'leo', 0, 30);if(!$memcache->add( 'name', 'susan', 0, 30)) {      echo 'susan is exist'; }; $memcache->replace( 'name', 'lion', 0, 300); echo $memcache->get( 'name');$memcache->delete( 'name', 5);?>memcache_debug()函数的作用是控制调试功能,前提是php在编译的时候使用了?enable-debug选项,否则这个函数不会有作用。Memcache::addServer 方法的作用是添加一个可供使用的服务器地址,Memcache::addServer方法有8个参数,除了第一个参数意外,其他都是可选的,第一个参数表 示服务器的地址,第二个参数表示端口,第三个参数表示是否是一个持久连接,第四个参数表示这台服务器在所有服务器中所占的权重,第五个参数表示连接的持续 时间,第六个参数表示连接重试的间隔时间,默认为15,设置为-1表示不进行重试,第七个参数用来控制服务器的在线状态,第8个参数允许设置一个回掉函数 来处理错误信息。Memcache::setServerParams方法的作用是在运行时修改服务器的参 数,Memcache::setServerParams方法有六个参数,Memcache::addServer方法少了第三和第四个参数。 Memcache::getServerStatus方法的作用是获取运行服务器的参数,两个参数分别表示的地址和端口。<?php  function _callback_memcache_failure($host, $port) {       print "memcache '$host:$port' failed";  }  $memcache = new Memcache;  $memcache->addServer('192.168.1.116', 11211);  $memcache->setServerParams('192.168.1.116', 11211, 1, 15, true, '_callback_memcache_failure');  echo $memcache->getServerStatus('192.168.1.116', 11211);  ?>  <?php function _callback_memcache_failure($host, $port) {      print "memcache '$host:$port' failed"; } $memcache = new Memcache; $memcache->addServer('192.168.1.116', 11211); $memcache->setServerParams('192.168.1.116', 11211, 1, 15, true, '_callback_memcache_failure'); echo $memcache->getServerStatus('192.168.1.116', 11211); ?>Memcache::flush方法的作用是清除所有缓存的数据,但是不会削去使用的内存空间。Memcache::increment方法的作用是对保存的某个key中的值进行加法操作,Memcache::decremen方法的作用是对保存的某个key中的值进行减法操作。<?php  $memcache = new Memcache;  $memcache->connect('localhost', 11211);  $memcache->set('test_item', 8);  $memcache->increment('test_item', 4);  echo $memcache->decrement('test_item', 7);  // 显示 5  ?>  <?php $memcache = new Memcache; $memcache->connect('localhost', 11211); $memcache->set('test_item', 8); $memcache->increment('test_item', 4); echo $memcache->decrement('test_item', 7); // 显示 5 ?>setCompressThreshold方法的作用是对大于某一大小的数据进行压缩。setCompressThreshold方法有两个参数,第一个参数表示处理数据大小的临界点,第二个参数表示压缩的比例,默认为0.2。<?php  $memcache = new Memcache;  $memcache->addServer('memcache_host', 11211);  $memcache->setCompressThreshold(20000, 0.2);  ?>  <?php $memcache = new Memcache; $memcache->addServer('memcache_host', 11211);$memcache->setCompressThreshold(20000, 0.2); ?>

 转自: http://blog.sina.com.cn/s/blog_5e53c7f80100f30p.html

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
What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

What is the importance of setting the httponly flag for session cookies?What is the importance of setting the httponly flag for session cookies?May 03, 2025 am 12:10 AM

Setting the httponly flag is crucial for session cookies because it can effectively prevent XSS attacks and protect user session information. Specifically, 1) the httponly flag prevents JavaScript from accessing cookies, 2) the flag can be set through setcookies and make_response in PHP and Flask, 3) Although it cannot be prevented from all attacks, it should be part of the overall security policy.

What problem do PHP sessions solve in web development?What problem do PHP sessions solve in web development?May 03, 2025 am 12:02 AM

PHPsessionssolvetheproblemofmaintainingstateacrossmultipleHTTPrequestsbystoringdataontheserverandassociatingitwithauniquesessionID.1)Theystoredataserver-side,typicallyinfilesordatabases,anduseasessionIDstoredinacookietoretrievedata.2)Sessionsenhances

What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

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.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)