win安装memcache
1.第一步下载memcache
下载地址:菜鸟驿站: https://www.runoob.com/memcached/window-install-memcached.html
直接下载地址:
http://static.runoob.com/download/memcached-win64-1.4.4-14.zip
第二步:安装
将下载下来的压缩包进行解压
使用管理员启动cmd,使用管理员权限运行以下命令
3、输入命令:memcached -d install,将memcache安装为系统服务
4、验证安装:memcached -h
5、启动服务:memcached -d start
6、连接memcache:telnet localhost 11211
第三步:链接memcache
打开命令行工具:输入telnet localhost 11211
如果没报错,说明已经链接成功了,如果报了telnet的客户端错误的话,说明需要先安装telnet的客户端
第四步:php安装memcache扩展
1.查看php的版本信息
2.下载扩展,php7的memcache扩展,官方目前只支持到5.6就停了,去github上可以找到
https://github.com/nono303/PHP7-memcache-dll
根据自己的版本进行选择
下载解压后,
就到 php/ext 目录下 把 php_memcache.dll 放到里面
然后在 php 目录下的 php.ini 增加一段内容
extension=php_memcache.dll
加完之后,重启 apache或者nginx
然后 在php页面输出phpinfo();
第五步:memcache常用命令
1.set:用于向缓存添加新的键值对。如果键已经存在,则之前的值将被替换。
set userId 0 0 5 \n 12345
key flag expiretime bytes value
2.get:用于检索与键值对相关的值,如果键存在于缓存中,则返回相应的值。如果不存在,则不返回任何内容。
get userId
key
3.delete:用于删除 memcached 中的任何现有值
delete userId
key
4.flush_all:用于清空缓存中的所有键/值对(设置所有键/值对过期)
第六步:api操作:
<?php
$mem = new Memcache();
// 链接memcache
if (!$mem ->connect('127.0.0.1')) {
exit('链接memcache失败');
}
// 设置mywork为 hello world到memcache中
if ($mem ->set('mywork','hello world',MEMCACHE_COMPRESSED,50)) {
echo '设置成功','<hr />';
}
// 删除值
// $mem ->delete('mywork');
// 删除所有的值
// $mem -> flush();
// 从memcache中读取数据
$value = $mem ->get('mywork');
echo 'mywork对应的值为:'.$value,'<hr />';
?>