abstract:一.什么是memcache + memcache 是一个分布式的内存数据库缓存服务器 + 在应用系统中处于数据库层和应用层之间 + memcache本身是多线程的,读写速度非常快二.如何安装memcache 1.wi
一.什么是memcache
+ memcache 是一个分布式的内存数据库缓存服务器
+ 在应用系统中处于数据库层和应用层之间
+ memcache本身是多线程的,读写速度非常快
二.如何安装memcache
1.windows安装memcache
1. 下载memcache 的windows版本,32位系统安装32位,64位安装64位
2. 进入memcache.exe所在目录,管理员身份打开cmd命令行,不能通过双击安装!
3. 输入命令: memcache -d install ,将memcache 安装为系统服务
4. 验证安装: memcache -h
5. 启动服务: memcache -d start
6. 链接memcache:telnet localhost 11211
这块呢 由于下载的问题,第一次安装失败了,在网上查了下问题,最终还是安装成功了
2.linux安装memcache (由于单位电脑linux改ip连不上网回家实验一下,不过吐槽一下,真的好长的命令...)
1. 安装libevent-devel(memcached 依赖 libevent-devel):
yum -y install libevent-devel
2.官网下载memcache的linux版本:http://memcached.org/
wget http://memcached.org/files/memcached-1.4.35.tar.gz
3.解压:tar -zxvf memcached-1.4.35.tar.gz
4. 进入memcache目录:cd memcached-1.4.35.tar.gz
4.1 yum install gcc 安装编译器
5. 编译安装:./configure && make && sudo make install(如果成功,可以在/usr/local/bin找到memcache)
6.启动memcache: /usr/local/bin/memcached -d -m 100 -u root -l 127.0.0.1 -p 12000 -c 256 -P /tmp/memcached.pid
7. 检测是否启动成功: ps aux|grep memcached
----------------------------------------------------------------------------
三.PHP中安装memcache扩展
1.windows上安装
1. 下载memcache的windows版本
https://windows.php.net/downloads/pecl/releases/memcache/3.0.8
2. 找到php_memcache.dll,赋值到对应php/ext目录中.
3. 打开php.ini文件,添加一行:extension=php_memcache.dll
4.重启appche/nginx
5.使用phpinfo查看memcache扩展是否成功
不过这地方没有看到php7.0的版本下载,所以切换了php版本到5.6下载安装的
2.linux安装扩展(还是需要回家测试一下,不得不说,感觉安装比操作还要难..)
yum install httpd 安装appche
yum install php 安装php环境
service httpd restart 重启appche
1. 安装zlib zlib-devel
yum install zlib
yum install zlib-devel
2. 下载memcache扩展源码:
wget http://pecl.php.net/get/memcached-2.2.0.tgz
3. 解压
tar -zxvf memcached-2.2.0.tgz
4. 生成configure
/usr/bin/phpize
/usr/local/php/bin/phpize
4.1 yum install php-devel 安装devel
4.2 yum install libmemcahced 安装libmemcahced
5. 编译
./configure --with-php-config=/usr/local/php-config --enable-memcached --disable-memcached-sasl
./configure --with-php-config=/usr/local/php-config --enable-memcached
make && make install
6. 添加模块到php:vim/etc/php.ini,添加: extension=memcached.so
6.1 vi /etc/php.ini
7. 重启appche/nginx service httpd restart
8. 使用phpinfo查看memcache扩展是否安装成功
--------------------------------------------------------------------------------
四.memcache的常用操作
1.set :用于向缓存添加新的键值对,如果键已存在,则之前的值将被替换
如: set name 0 0 5 然后回车 开始输入值
这里name 是键的意思 0 是flag 第二个0是存在时间(0代表永久) 5是字符长度 然后回车输入的值的长度要与字符长度相等
2.get:用于检索与键值对相关的值,如果键存在于缓存中,则返回相对应的值.如果不存在,则不返回任何内容
如 get name name就是键名(要在存在的时间内获取)
3.delete 用于删除memcached中的任何现有值
如:delete name
4.flush_all:用于清空缓存中数据
如:flush_all name
经过测试 好像是有一些关键字是不可以当键名的? 还是有什么其他原因? set id 失败了 set ids 也失败了
五.php使用api来操作memcache
1.new一个memcache类 new Memcache();
$mem = new Memcache();
2.链接memcache $mem->conncet("主机名")
if(!$mem->connect("127.0.0.1")){ //失败 退出输出信息 exit('连接memcache失败'); }
3.设置firstmemcache 值为 我是第一个memcache到memcache中
set('键名','值','压缩内容',存在时间单位秒)
if($mem->set('firstmemcache','我是第一个memcache',MEMCACHE_COMPRESSED,50)){ echo '设置成功!'; }
4.删除memcache的值 $mem->delete('键名')
$mem->delete('firstmemcache')
5.删除所有memcache的值
$mem->flush();
6.提取memcache的数据
$value = $mem->get('firstmemcache'); echo 'firstmemcache对应的值为:'.$value;
六.tp5.1内置的memcache
1.配置config/cache.php的type的值为memcache即可使用
2.调用tp5.1的Cache类的get 和set方法来管理memcache
等学到tp5.1来实际操作一下
Correcting teacher:查无此人Correction time:2018-12-07 14:39:25
Teacher's summary:不错,逻辑很清晰。memcache是php常用的技术,学好了对你帮助很大。还有个memcached也可以自己看看。