摘要:這章涉及了memcache的概念,安裝,php安裝memcache擴展,memcache常用的命令以及memcache與php的集成。(1)Memcache 是分布式內(nèi)存對象緩存數(shù)據(jù)庫(本身不具備分布式功能),通過key-value的方式把數(shù)據(jù)存儲到內(nèi)存中去。(2)安裝memcache。1、windows上安裝步驟:1、下載memcache的windows版本,32位系統(tǒng)選擇32版,64位系統(tǒng)選擇
這章涉及了memcache的概念,安裝,php安裝memcache擴展,memcache常用的命令以及memcache與php的集成。
(1)Memcache 是分布式內(nèi)存對象緩存數(shù)據(jù)庫(本身不具備分布式功能),通過key-value的方式把數(shù)據(jù)存儲到內(nèi)存中去。
(2)安裝memcache。
1、windows上安裝步驟:1、下載memcache的windows版本,32位系統(tǒng)選擇32版,64位系統(tǒng)選擇64位版。2、進入memcache.exe所在目錄,管理員身份打開cmd命令行(不能通過雙擊安裝)。3、輸入命令:memcached -d install,將memcache安裝為系統(tǒng)服務(wù)。4、驗證安裝:memcached -h。5、啟動服務(wù):memcached -d start。6、連接memcache:telnet localhost 11211。
2、linux上安裝
1、安裝libevent-devel(memcached 依賴 libevent-devel): yum -y install libevent-devel。
2、官網(wǎng)下載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
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
8、結(jié)束memcached:kill `cat /tmp/memcached.pid`。
(3)、php中安裝memcache擴展
一、windows上安裝
1、下載memcache的windows版本:
https://windows.php.net/downloads/pecl/releases/memcache/3.0.8/
2、找到php_memcache.dll,復(fù)制到對應(yīng)的php/ext目錄中。
3、打開php.ini文件,添加一行:extension=php_memcache.dll
4、重啟apache/nginx
5、使用phpinfo查看memcache擴展是否安裝成功。
linux上安裝:
準(zhǔn)備工作:
1.安裝apache
Yum install httpd
2.安裝php
Yum install php
3.重啟服務(wù):service httpd restart
4.服務(wù)器訪問地址,訪問不了,防火墻緣故,給防火墻添加80端口
Firewall-cmd –permanent --add-port=80/tcp
Service firewalld restart
5.網(wǎng)頁可以正常訪問地址,查看phpinfo.php文件,是否有memcache擴展
一、linux上安裝
1、安裝zlib,zlib-devel
yum install zlib
yum install zlib-devel
2、下載memcached擴展源碼:
wget http://pecl.php.net/get/memcached-2.2.0.tgz
3、解壓
tar -zxvf memcached-2.2.0.tgz
4、生成configure,進入memcached-2.2.0目錄
/usr/local/php/bin/phpize
若為 phpstudy
find / name phpize,找到位置,然后執(zhí)行文件。
(yum install libmemcached)
5、編譯 ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcached --disable-memcached-sasl
./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcached make && make install
?若為phpstudy
./configure -enable-memcache --with-php-config=/phpstudy/server/php/bin/php-config --with-zlib-dir
make && make install安裝 (yum install libmemcached libmemcached-devel) 6、添加模塊到php:vi /etc/php.ini,添加extension=memcached.so 7、重啟apache/nginx 8、使用phpinfo查看memcache擴展是否安裝成功。
(4)memcache常用命令
開啟:selnet localhost 11211
set:用于向緩存添加新的鍵值對。如果鍵已經(jīng)存在,則之前的值將被替換。
set userId 0 0 5 \n 12345
key flag expiretime bytes value
get:用于檢索與鍵值對相關(guān)的值,如果鍵存在于緩存中,則返回相應(yīng)的值。如果不存在,則不返回任何內(nèi)容。
get userId
key
delete:用于刪除 memcached 中的任何現(xiàn)有值
delete userId
key
flush_all:用于清空緩存中的所有鍵/值對(設(shè)置所有鍵/值對過期)
(5)php使用api操作memcache
<?php /** *memcache */ $mem = new Memcache(); //連接memcache if(!$mem->connect("127.0.0.1")){ exit('連接失敗'); } //設(shè)置mywork為hello world到memcache, //參數(shù):鍵,值,是否需要壓縮,設(shè)置時間 if($mem->set('myword','hello world',MEMCACHE_COMPRESSED,50)){ echo '設(shè)置成功'; } //刪除數(shù)據(jù) //$mem->delete('myword'); //清空所有值 $mem->flush(); //從memcache中取值 $value = $mem->get('myword'); echo 'myword對應(yīng)的值為:'.$value;
(6)thinkphp5.1集成了memcache,需要進行緩存配置。
通過這章,基本掌握了memcache的相關(guān)知識了,還需要多應(yīng)用
批改老師:韋小寶批改時間:2019-02-01 09:50:17
老師總結(jié):總結(jié)的很棒!非常的完整 ,沒事要記得多研究研究哦!