memcache使用方法测试 # 转自 简单--生活 #,
memcache使用方法测试 # 转自 简单--生活 #,
<?php
//php操作memcache的使用测试总结--学习
//1 Memcache::connect;
//$memcache = new Memcache;
//$memcache->connect('127.0.0.1',11211) or die("链接失败!");
//2 Memcache::pconnect; 长链接
//$memcache = new Memcache;
//$ret = $memcache->pconnect('127.0.0.1',11211) or die("链接失败");
//var_dump($ret);
//3 Memcache::close; 关闭对象(对常链接不起作用)
/*$memcache = new Memcache;
$memcache->connect('127.0.0.1',11211) or die("链接失败!");
$result = $memcache->close();
var_dump($result);*/
//4 Memcache::addServer; 向对象添加一个服务器
/*$mem = new Memcache;
$is_add = $mem->addServer('localhost',11211,true, 1, 1, 15, true);
$is_set = $mem->set('key1','中华人民共和国');
var_dump($is_set);*/
/*$mem = new Memcache;
$is_add = $mem->addServer('localhost',11211,true, 1, 1, -1, false);
$is_set = $mem->set('key1','中华人民共和国');
var_dump($is_set);*/
//5 Memcache::add 添加一个要缓存的数据如果作为这个缓存的数据在键在服务器上还不存在的情况下
/*$mem = new Memcache;
$is_add = $mem->addServer('localhost',11211);
$is_set = $mem->add('key2','中华人民共和国', false, 60);
var_dump($is_set);*/
//6 Memcache::replace() 替换一个指定已存在key的缓存变量内容
/*$mem = new Memcache;
$is_add = $mem->addServer('localhost',11211);
$mem->add('key2','中华人民共和国', false, 60);
$is_set = $mem->replace('key2','台湾人民共和国',false,60);
var_dump($is_set);*/
//7 Memcace::set 设置一个指定key的缓存变量内容
/*
$mem = new Memcache;
$is_add = $mem->addServer('localhost',11211);
$mem->set('key2','中华人民共和国', false, 60);
$key2 = $mem->get('key2');
echo $key2."<br>";
$mem->replace('key2','台湾人民',false,60);
$key2 = $mem->get('key2');
echo $key2;*/
//8 Memcache::get() 获取某个key的变量缓存值
/*
$mem = new Memcache;
$is_add = $mem->addServer('localhost',11211);
$mem->set('key2','中华人民共和国', false, 60);
$mem->set('key1','台湾人民',false,60);
$arr = $mem->get(array('key1','key2'));
var_dump($arr);
*/
//9 Memcache::delete 删除某个变量的缓存
/*
$mem = new Memcache;
$is_add = $mem->addServer('localhost',11211);
$mem->set('key2','中华人民共和国', false, 60);
$mem->set('key1','台湾人民',false,60);
$mem->delete('key2');
$arr = $mem->get(array('key1','key2'));
var_dump($arr);
*/
//10 Memcach::flush 清空所缓存内容,不是真的删除缓存的内容,只是使所有变量的缓存过期,使内存中的内容被重写
/*
$mem = new Memcache;
$is_add = $mem->addServer('localhost',11211);
$mem->flush();
*/
//11 Memcach::getExtendedStats 获取所有服务器扩展静态信息
/*
$mem = new Memcache;
$is_add = $mem->addServer('localhost',11211);
$stats = $mem->getExtendedStats();
var_dump($stats);
*/
//12 Memcache:getStats; 获取最后添加服务器静态信息
//13 Memcache::getServerStatus 通过输入的host及port来获取相应的服务器信息
/*
$mem = new Memcache;
$is_add = $mem->addServer('localhost',11211);
$info = $mem->getServerStatus('localhost');
var_dump($info);
*/
//返回值
//返回服务器状态,0为失败,其他情况返回非0数字
//14 Memcache::getVersion() 获取服务器的版本号信息
/*
$mem = new Memcache;
$is_add = $mem->addServer('localhost',11211);
$version = $mem->getVersion();
echo $version;
*/
//15 Memcache::setCompressThreshold 设置压缩级根
//bool Memcache::setCompressThreshold ( int $threshold [, float $min_savings ] )
//threshold 设置控制自动压缩的变量长度的最小值
//min_saving 指定的最低压缩比率,值必须介于 0 - 1 之间,默认为 0.2 代表 20% 的压缩比率
//$mem->setCompressThreshold(20000,0.2);
//16 Memcache::setServerParams Memcache version 2.1.0后增加的函数,运行时设置服务器参数
//17 Memcache::increment 给指定kye的缓存变量一个增值,如查该变量不是数字时不会被转化为数字
//这个增值将会加到该变量原有的数字之上,变量不存在不会新增变量
//对于压缩存储的变量不要使用本函数因为相应的取值方法会失败
/*
$mem = new Memcache;
$is_add = $mem->addServer('localhost',11211);
$mem->set('key2',11, false, 60);
$key = $mem->get('key2');
echo $key."<br><br>";
$mem->increment('key2',3);
$key = $mem->get('key2');
echo $key;
*/
//18 Memcache::decrement
//给指定key的缓存变量一个递减值,与increment操作类拟,将在原有变量基础上减去这个值,该项的值将会在转化为数字后减去,新项的值不会小于0,对于压缩的变量不要使用本函数因为相应的取值方法会失败
/*$mem = new Memcache;
$is_add = $mem->addServer('localhost',11211);
$mem->set('key2',11, false, 60);
$key = $mem->get('key2');
echo $key."<br><br>";
$mem->decrement('key2',3);
$key = $mem->get('key2');
echo $key;*/
//19 Memcache_debug
//设置memecache的调用器是否开启,值为true或者false,受影响于php这安装时是否使用了 --enable-debug选项,如果使用了该函数才会返回true,其他情况将始终返回false
?>
|
地址:http://www.cnblogs.com/qiantuwuliang/archive/2011/03/07/1974499.html

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











In web development, we often need to use caching technology to improve website performance and response speed. Memcache is a popular caching technology that can cache any data type and supports high concurrency and high availability. This article will introduce how to use Memcache in PHP development and provide specific code examples. 1. Install Memcache To use Memcache, we first need to install the Memcache extension on the server. In CentOS operating system, you can use the following command

How to use Memcache for efficient data writing and querying in PHP development? With the continuous development of Internet applications, the requirements for system performance are getting higher and higher. In PHP development, in order to improve system performance and response speed, we often use various caching technologies. One of the commonly used caching technologies is Memcache. Memcache is a high-performance distributed memory object caching system that can be used to cache database query results, page fragments, session data, etc. By storing data in memory

In PHP development, using the Memcache caching system can greatly improve the efficiency of data reading and writing. Memcache is a memory-based caching system that can cache data in memory to avoid frequent reading and writing of the database. This article will introduce how to use Memcache in PHP for efficient data reading and writing operations, and provide specific code examples. 1. Install and configure Memcache First, you need to install the Memcache extension on the server. able to pass

The practice and thinking of Memcache caching technology to optimize data interaction in PHP In modern Web applications, data interaction is a very important issue. It is not efficient enough and will limit the scalability and performance of Web applications. In order to speed up data interaction, our usual approach is to optimize the design of the database, improve the performance of the hardware and increase the server capacity. However, these methods all have a common limitation: they increase the cost of the system. In recent years, Memcache technology has made progress in solving this problem.

Memcache is an open source, distributed caching technology. It greatly improves the speed of data access by storing data in memory, thus improving the performance and responsiveness of the website. In PHP projects, Memcache caching technology is also widely used and has achieved good results. This article will deeply explore the application and practice of Memcache caching technology in PHP projects. 1. Principles and advantages of Memcache Memcache is a memory caching technology that can store data

With the rapid development of the Internet, more and more applications need to face a large number of concurrent requests. How to improve the concurrent processing capabilities of applications has become a problem that developers need to solve. Among them, using Memcache caching technology for concurrency optimization has become a relatively popular solution. Memcache is an efficient caching technology suitable for large-scale web applications, databases and distributed systems. Its characteristic is to store data in memory to achieve high-speed read and write operations. During the data access process of web applications,

As web applications become increasingly complex, performance has become a critical issue. In many applications, database queries are one of the most time-consuming operations. In order to avoid frequently reading data from the database, a caching system can be used to store frequently read data in memory for quick access. In PHP development, using Memcached for distributed caching is an extremely common practice. In this article we will introduce how to use Memcached for distributed caching. What is Memca

How to use Memcache to optimize data storage operations in your PHP application? In web application development, data storage is a crucial link. In PHP applications, Memcache, as a memory cache system, can effectively improve the efficiency of data storage and reading operations. This article will introduce how to use Memcache to optimize data storage operations in PHP applications, and attach specific code examples. Step 1: Install the Memcache extension First, you need to install Me in your PHP environment
