A PHP uses memcache caching technology to improve response speed. Friends in need can refer to it.
Connect to database
The code is as follows 代码如下 | 复制代码 |
$memcache_obj = memcache_connect(”localhost”, 11211);
memcache_add($memcache_obj, ’var_key’, ’test variable’, false, 30);
$memcache_obj->add(’var_key’, ’test variable’, false, 30);
| |
Copy code
|
$memcache_obj = memcache_connect("localhost", 11211); 代码如下 | 复制代码 | //连接 $mem = new Memcache; $mem->connect(“127.0.0.1″, 11211) or die (“Could not connect”); //显示版本 $version = $mem->getVersion(); echo “Memcached Server version: “.$version.” ”; //保存数据 $mem->set(‘key1′, ‘This is first value’, 0, 60); $val = $mem->get(‘key1′); echo “Get key1 value: ” . $val .” ”; //替换数据 $mem->replace(‘key1′, ‘This is replace value’, 0, 60); $val = $mem->get(‘key1′); echo “Get key1 value: ” . $val . “ ”; //保存数组 $arr = array(‘aaa’, ‘bbb’, ‘ccc’, ‘ddd’); $mem->set(‘key2′, $arr, 0, 60); $val2 = $mem->get(‘key2′); echo “Get key2 value: “; print_r($val2); echo “ ”; //删除数据 $mem->delete(‘key1′); $val = $mem->get(‘key1′); echo “Get key1 value: ” . $val . “ ”; //清除所有数据 $mem->flush(); $val2 = $mem->get(‘key2′); echo “Get key2 value: “; print_r($val2); echo “ ”; //关闭连接 $mem->close(); ?> |
memcache_add($memcache_obj, 'var_key', 'test variable', false, 30);
$memcache_obj->add('var_key', 'test variable', false, 30);
Instance 1
|
The code is as follows |
Copy Code |
//Connect $mem = new Memcache; $mem->connect(“127.0.0.1″, 11211) or die (“Could not connect”); //Display version $version = $mem->getVersion(); echo “Memcached Server version: “.$version.” ”; //Save data $mem->set('key1′, 'This is first value', 0, 60); $val = $mem->get('key1′);echo “Get key1 value: ” . $val .” ”; | //Replace data
$mem->replace('key1′, 'This is replace value', 0, 60);
$val = $mem->get('key1′);echo “Get key1 value: ” . $val . “
”;//Save the array$arr = array('aaa', 'bbb', 'ccc', 'ddd');$mem->set('key2′, $arr, 0, 60);$val2 = $mem- >get('key2′);echo “Get key2 value: “;print_r($val2);echo “
”;//Delete data $mem->delete('key1′);$val = $mem->get('key1′);echo “Get key1 value: ” . $val . “
” ;//Clear all data$mem->flush();$val2 = $mem->get('key2′);echo "Get key2 value: "; print_r($val2);echo “
”;//Close the connection$mem->close();?> Memcache function library is in PECL (PHP Extension Community Library). Its main function is to build a temporary storage area for large-capacity memory data , which is used when distributed The reflection is very obvious, otherwise Internet marketing training is not recommended. Memcache::add - Adds a value, returns false if it already exists Memcache::addServer - Adds a server address for use Memcache::close - Closes a Memcache object Memcache::connect - Create a Memcache objectmemcache_debug - Control the debugging function! [4 F8 C2 e, XMemcache::decrement - Subtract the value in a saved keyMemcache::delete - Delete a key valueMemcache::flush - Clear all cached dataMemcache::get - Get a key valueMemcache::getExtendedStats - Get the status of all processes in the process pool Running system statisticsMemcache::getServerStatus - Gets the parameters of the running serverMemcache::getStats - Returns some running statistics of the serverMemcache::getVersion - Returns the version information of the running MemcacheMemcache ::increment - Add the value in a saved keyMemcache::pconnect - Create a Memcache persistent connection objectMemcache::replace - Overwrite an existing keyMemcache::set - Add a value, overwrite if it already exists Memcache::setCompressThreshold - Compress data larger than a certain size Memcache::setServerParams - Modify server parameters at runtime
http://www.bkjia.com/PHPjc/444628.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444628.htmlTechArticleA PHP uses memcache caching technology to improve response speed. Friends in need can refer to it. The code to connect to the database is as follows. Copy the code $memcache_obj = memcache_connect(localhost, 11211...
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