Windows下安装配置PHP Memcached

目录
  1. 安装前准备
    1. 下载memcached
    2. php扩展
  2. 试运行:

安装前准备

下载memcached

解压放某个盘下面,比如在c:\memcached

在终端下输入 ‘c:\memcached\memcached.exe -d install’ , memcached将作为windows的一个服务

再输入 ‘c:\memcached\memcached.exe -d start’ , 这样服务器端已经安装完毕了

php扩展

php 所需的 PECL 扩展,即 php_memcache 扩展 http://windows.php.net/downloads/pecl/releases/memcache/ 版本

选择合适的版本,php.ini 加入一行extension=php_memcache.dll

1
2
3
4
5
6
extension=php_memcache.dll
[Memcache]
memcache.allow_failover = 1
memcache.max_failover_attempts=20
memcache.chunk_size =8192
memcache.default_port = 11211

试运行:

写一个 test.php 文件:(更多使用方法可以参看 PHP 手册里的 Memcache Functions 使用说明)

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
header('Content-Type:text/html;charset=utf-8');
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";

$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;

$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";

$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";
print_r($get_result);

输出:

1
2
3
4
Server's version: 1.2.6
Store data in the cache (data will expire in 10 seconds)
Data from the cache:
stdClass Object ( [str_attr] => test [int_attr] => 123 )