PHP memcache

Jun 23, 2016 pm 02:37 PM

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); ?>
Copy after login

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

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

See all articles