一、安装php_memcache.dll
打开phpinfo()
查看PHP Extension Build,如TS,VC11
查看Architecture,如X86、X64
查看PHP版本,如5.6.17
然后根据这三个条件选择对应的DLL下载
1. php_memcache.dll 下载:
基础测试:
5.2-5.5系列下载地址:http://download.csdn.net/detail/zsjangel/7104749
2. 把php_memcache.dll放到php的ext目录:
例如:E:\AppServ\php5\ext\php_memcache.dll
3. 打开 php.ini 文件:
我的php.ini的位置:D:\WampServer\bin\php\php5.3.11\php.ini
4. 在 php.ini上增加一行:
extension=php_memcache.dll
5. 重启Wampserver的apache服务
二、安装memcached
2. 解压放某个盘下面,比如:
E:\memcache\memcached.exe
3. 在终端(也即cmd命令界面)下输入以下命令安装windows服务:
E:\memcache>memcached.exe -d install
4. 再输入下面命令启动:
E:\memcache>memcached.exe -d start
打开phpinfo()页面
三、开始使用
基础测试:
$memcache = new Memcache; // instantiating memcache extension class
$memcache->connect("localhost",11211); // try 127.0.0.1 instead of localhost
// if it is not working echo "Server's version: " . $memcache->getVersion() . "<br />\n"; // we will create an array which will be stored in cache serialized
$testArray = array('horse', 'cow', 'pig');
$tmp = serialize($testArray); $memcache->add("key", $tmp, 0,30); echo "Data from the cache:<br />\n";
print_r(unserialize($memcache->get("key")));
函数封装
//memcache
define("CACHE_IP", "127.0.0.1"); //默认端口
define("CACHE_PORT", "11211"); function set_cache($key,$value,$expireTime)
{
$m = new Memcache();
$m->connect(CACHE_IP,CACHE_PORT);
$m->set($key,$value,0,$expireTime);
} function get_cache($key)
{
$m = new Memcache();
$m->connect(CACHE_IP,CACHE_PORT);
return $m->get($key);
}
调用
set_cache("ProdName", "商品", 10); echo get_cache("ProdName");