目錄
一、什么是NoSQL
二消恍、常見NoSQL數(shù)據(jù)庫
三煎楣、Memcached
一豺总、什么是NoSQL
? 非關(guān)系型數(shù)據(jù)庫就是NoSQL,關(guān)系型數(shù)據(jù)庫代表MySQL择懂。
? 對(duì)于關(guān)系型數(shù)據(jù)庫來說喻喳,是需要把數(shù)據(jù)存儲(chǔ)到庫、表困曙、行表伦、字段里,查詢的時(shí)候根據(jù)條件一行一行地去匹配慷丽,當(dāng)量非常大的時(shí)候就很耗費(fèi)時(shí)間和資源蹦哼,尤其是數(shù)據(jù)是需要從磁盤里去檢索。
? NoSQL數(shù)據(jù)庫存儲(chǔ)原理非常簡(jiǎn)單(典型的數(shù)據(jù)類型為k-v)盈魁,不存在繁雜的關(guān)系鏈翔怎,比如mysql查詢的時(shí)候窃诉,需要找到對(duì)應(yīng)的庫杨耙、表(通常是多個(gè)表)以及字段。
? NoSQL數(shù)據(jù)可以存儲(chǔ)在內(nèi)存里飘痛,查詢速度非成耗ぃ快。
? NoSQL在性能表現(xiàn)上雖然能優(yōu)于關(guān)系型數(shù)據(jù)庫宣脉,但是它并不能完全替代關(guān)系型數(shù)據(jù)庫车柠。
? NoSQL因?yàn)闆]有復(fù)雜的數(shù)據(jù)結(jié)構(gòu),擴(kuò)展非常容易塑猖,支持分布式竹祷。
二、常見NoSQL數(shù)據(jù)庫
? k-v形式的:memcached羊苟、redis 適合儲(chǔ)存用戶信息塑陵,比如會(huì)話、配置文件蜡励、參數(shù)令花、購物車等等阻桅。這些信息一般都和ID(鍵)掛鉤,這種情景下鍵值數(shù)據(jù)庫是個(gè)很好的選擇兼都。
? 文檔數(shù)據(jù)庫:mongodb 將數(shù)據(jù)以文檔的形式儲(chǔ)存嫂沉。每個(gè)文檔都是一系列數(shù)據(jù)項(xiàng)的集合。每個(gè)數(shù)據(jù)項(xiàng)都有一個(gè)名稱與對(duì)應(yīng)的值扮碧,值既可以是簡(jiǎn)單的數(shù)據(jù)類型趟章,如字符串、數(shù)字和日期等慎王;也可以是復(fù)雜的類型尤揣,如有序列表和關(guān)聯(lián)對(duì)象。數(shù)據(jù)存儲(chǔ)的最小單位是文檔柬祠,同一個(gè)表中存儲(chǔ)的文檔屬性可以是不同的北戏,數(shù)據(jù)可以使用XML、JSON或者JSONB等多種形式存儲(chǔ)漫蛔。
? 列存儲(chǔ) Hbase
? 圖 Neo4J嗜愈、Infinite Graph、OrientDB莽龟。
三蠕嫁、Memcached
Memcached簡(jiǎn)介
? Memcached是國外社區(qū)網(wǎng)站LiveJournal團(tuán)隊(duì)開發(fā),目的是為了通過緩存數(shù)據(jù)庫查詢結(jié)果毯盈,減少數(shù)據(jù)庫訪問次數(shù)剃毒,從而提高動(dòng)態(tài)web站點(diǎn)性能。
? 官方站點(diǎn) http://www.memcached.org/
? 數(shù)據(jù)結(jié)構(gòu)簡(jiǎn)單(k-v)搂赋,數(shù)據(jù)存放在內(nèi)存里赘阀,不支持持久化,可以自行保存在磁盤里
? 多線程
? 基于c/s架構(gòu)脑奠,協(xié)議簡(jiǎn)單
? 基于libevent的事件處理
? 自主內(nèi)存存儲(chǔ)處理(slab allowcation)
? 數(shù)據(jù)過期方式:Lazy Expiration 和 LRU-
Memcached原理圖
Slab Allocation
? Slab Allocation的原理
? 將分配的內(nèi)存分割成各種尺寸的塊(chunk)基公, 并把尺寸相同的塊分成組(chunk的集合),每個(gè)chunk集合被稱為slab宋欺。
? Memcached的內(nèi)存分配以Page為單位轰豆,Page默認(rèn)值為1M,可以在啟動(dòng)時(shí)通過-I參數(shù)來指定齿诞。
? Slab是由多個(gè)Page組成的酸休,Page按照指定大小切割成多個(gè)chunk。
Growth factor
? Memcached在啟動(dòng)時(shí)通過-f選項(xiàng)可以指定 Growth Factor因子祷杈。該值控制chunk大小的差異斑司。默認(rèn)值為1.25。
? 通過memcached-tool命令查看指定Memcached實(shí)例的不同slab狀態(tài)吠式,可以看到各Item所占大卸咐濉(chunk大谐槊住)差距為1.25
? 命令:# memcached-tool 127.0.0.1:11211 displayMemcached的數(shù)據(jù)過期方式
? Lazy Expiration
Memcached 內(nèi)部不會(huì)監(jiān)視記錄是否過期,而是在get時(shí)查看記錄的時(shí)間戳糙置,檢查記錄是否過期云茸。這種技術(shù)被稱為lazy(惰性)expiration。因此谤饭,Memcached不會(huì)在過期監(jiān)視上耗費(fèi)CPU時(shí)間标捺。
? LRU
Memcached會(huì)優(yōu)先使用已超時(shí)的記錄的空間,但即使如此揉抵,也會(huì)發(fā)生追加新記錄時(shí)空間不足的情況亡容,此時(shí)就要使用名為L(zhǎng)east Recently Used(LRU)機(jī)制來分配空間。顧名思義冤今,這是刪除“最近最少使用”的記錄的機(jī)制闺兢。因此,當(dāng)內(nèi)存空間不足時(shí)(無法從slab class獲取到新的空間時(shí))戏罢,就從最近未被使用的記錄中搜索屋谭,并將其空間分配給新的記錄。從緩存的實(shí)用角度來看龟糕,該模型十分理想桐磁。安裝memcached
[root@minglinux-01 ~] yum list |grep memcache
collectd-memcachec.x86_64 5.8.1-1.el7 epel
dmlite-plugins-memcache.x86_64 1.10.4-2.el7 epel
libmemcached.i686 1.0.16-5.el7 base
libmemcached.x86_64 1.0.16-5.el7 base
libmemcached-devel.i686 1.0.16-5.el7 base
libmemcached-devel.x86_64 1.0.16-5.el7 base
memcached.x86_64 1.4.15-10.el7_3.1 base
memcached-devel.i686 1.4.15-10.el7_3.1 base
memcached-devel.x86_64 1.4.15-10.el7_3.1 base
opensips-memcached.x86_64 1.10.5-4.el7 epel
pcp-pmda-memcache.x86_64 4.1.0-5.el7_6 updates
php-ZendFramework-Cache-Backend-Libmemcached.noarch
php-pecl-memcache.x86_64 3.0.8-4.el7 base
php-pecl-memcached.x86_64 2.2.0-1.el7 epel
python-memcached.noarch 1.48-4.el7 base
rubygem-memcache-client.noarch 1.8.5-13.el7 epel
uwsgi-router-memcached.x86_64 2.0.17.1-1.el7 epel
[root@minglinux-01 ~] yum install -y memcached #yum安裝memcached
[root@minglinux-01 ~] systemctl start memcached #啟動(dòng)memcached服務(wù)
[root@minglinux-01 ~] ps aux |grep memcache
memcach+ 80200 0.0 0.0 344080 1664 ? Ssl 22:46 0:00 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
root 80275 0.0 0.0 112720 984 pts/0 S+ 22:47 0:00 grep --color=auto memcache
#-u指定運(yùn)行memcached服務(wù)的用戶,-m指定memcached分配內(nèi)存讲岁,-c指定最大并發(fā)數(shù)我擂,指定參數(shù)可以修改
#修改方法1是以如下方式修改參數(shù)后啟動(dòng)服務(wù)
[root@minglinux-01 ~] /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
#修改方法2是編輯/etc/sysconfig/memcached文件修改
[root@minglinux-01 ~] vim /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="" #監(jiān)聽的ip
- 查看memcached狀態(tài)
[root@minglinux-01 ~] memcached-tool 127.0.0.1:11211 stats
#127.0.0.1:11211 Field Value
accepting_conns 1
auth_cmds 0
auth_errors 0
bytes 0
bytes_read 7
bytes_written 0
cas_badval 0
cas_hits 0
cas_misses 0
cmd_flush 0
cmd_get 0
cmd_set 0
cmd_touch 0
conn_yields 0
connection_structures 11
curr_connections 10
curr_items 0 ##需關(guān)注數(shù)字
decr_hits 0
decr_misses 0
delete_hits 0
delete_misses 0
evicted_unfetched 0
evictions 0
expired_unfetched 0
get_hits 0 #需關(guān)注數(shù)字
get_misses 0
hash_bytes 524288
hash_is_expanding 0
hash_power_level 16
incr_hits 0
incr_misses 0
libevent 2.0.21-stable
limit_maxbytes 67108864
listen_disabled_num 0
pid 80200
pointer_size 64
reclaimed 0
reserved_fds 20
rusage_system 0.036318
rusage_user 0.036318
threads 4
time 1548083101
total_connections 11
total_items 0
touch_hits 0
touch_misses 0
uptime 1111
version 1.4.15
或者使用echo stats |nc 127.0.0.1 11211 需要安裝nc工具 yum install -y nc
[root@minglinux-01 ~] echo stats |nc 127.0.0.1 11211
STAT pid 80200
STAT uptime 1347
STAT time 1548083337
STAT version 1.4.15
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 0.042126
STAT rusage_system 0.042126
STAT curr_connections 10
STAT total_connections 12
STAT connection_structures 11
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 13
STAT bytes_written 1026
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
END
#若安裝libmemcached后,可以使用命令
# memstat --servers=127.0.0.1:11211 查看memcached服務(wù)狀態(tài)
[root@minglinux-01 ~] rpm -qa libmemcached
[root@minglinux-01 ~] yum install -y libmemcached
[root@minglinux-01 ~] memstat --servers=127.0.0.1:11211
Server: 127.0.0.1 (11211)
pid: 80200
uptime: 1927
time: 1548083917
version: 1.4.15
libevent: 2.0.21-stable
pointer_size: 64
rusage_user: 0.066200
rusage_system: 0.052014
curr_connections: 10
total_connections: 13
connection_structures: 11
reserved_fds: 20
cmd_get: 0
cmd_set: 0
cmd_flush: 0
cmd_touch: 0
get_hits: 0
get_misses: 0
delete_misses: 0
delete_hits: 0
incr_misses: 0
incr_hits: 0
decr_misses: 0
decr_hits: 0
cas_misses: 0
cas_hits: 0
cas_badval: 0
touch_hits: 0
touch_misses: 0
auth_cmds: 0
auth_errors: 0
bytes_read: 30
bytes_written: 2072
limit_maxbytes: 67108864
accepting_conns: 1
listen_disabled_num: 0
threads: 4
conn_yields: 0
hash_power_level: 16
hash_bytes: 524288
hash_is_expanding: 0
bytes: 0
curr_items: 0
total_items: 0
expired_unfetched: 0
evicted_unfetched: 0
evictions: 0
reclaimed: 0
部分需關(guān)注的參數(shù)說明如下:
pid #memcache服務(wù)器的進(jìn)程ID
uptime #服務(wù)器已經(jīng)運(yùn)行的秒數(shù)
time #服務(wù)器當(dāng)前的unix時(shí)間戳
version #memcache版本
pointer_size #當(dāng)前操作系統(tǒng)的指針大谢貉蕖(32位系統(tǒng)一般是32bit)
rusage_user #進(jìn)程的累計(jì)用戶時(shí)間
rusage_system #進(jìn)程的累計(jì)系統(tǒng)時(shí)間
curr_items #服務(wù)器當(dāng)前存儲(chǔ)的items數(shù)量
total_items #從服務(wù)器啟動(dòng)以后存儲(chǔ)的items總數(shù)量
bytes #當(dāng)前服務(wù)器存儲(chǔ)items占用的字節(jié)數(shù)
curr_connections #當(dāng)前打開著的連接數(shù)
total_connections #從服務(wù)器啟動(dòng)以后曾經(jīng)打開過的連接數(shù)
connection_structures #服務(wù)器分配的連接構(gòu)造數(shù)
cmd_get #get命令(獲刃DΑ)總請(qǐng)求次數(shù)
cmd_set #set命令(保存)總請(qǐng)求次數(shù)
get_hits #總命中次數(shù)
get_misses #總未命中次數(shù)
evictions #為獲取空閑內(nèi)存而刪除的items數(shù)
bytes_read #總讀取字節(jié)數(shù)(請(qǐng)求字節(jié)數(shù))
bytes_written #總發(fā)送字節(jié)數(shù)(結(jié)果字節(jié)數(shù))
limit_maxbytes #分配給memcache的內(nèi)存大小(字節(jié))
threads #當(dāng)前線程數(shù)
- memcached命令行操作
#使用telnet進(jìn)入memcached命令行
[root@minglinux-01 ~] yum install -y telnet
[root@minglinux-01 ~] telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
- Memcached語法規(guī)則
<command name> <key> <flags> <exptime> <bytes>\r\n <data block>\r\n
注:\r\n在windows下是Enter鍵
<command name>可以是set, add, replace
set表示按照相應(yīng)的<key>存儲(chǔ)該數(shù)據(jù)郎任,沒有的時(shí)候增加秧耗,有的時(shí)候覆蓋
add表示按照相應(yīng)的<key>添加該數(shù)據(jù),但是如果該<key>已經(jīng)存在則會(huì)操作失敗
replace表示按照相應(yīng)的<key>替換數(shù)據(jù),但是如果該<key>不存在則操作失敗备籽。
<key>客戶端需要保存數(shù)據(jù)的key
<flags>是一個(gè)16位的無符號(hào)的整數(shù)(以十進(jìn)制的方式表示)舶治。該標(biāo)志將和需要存儲(chǔ)的數(shù)據(jù)一起存儲(chǔ),并在客戶端get數(shù)據(jù)時(shí)返回〕碘客戶端可以將此標(biāo)志用做特殊用途霉猛,此標(biāo)志對(duì)服務(wù)器來說是不透明的。
<exptime>為過期的時(shí)間珠闰。若為0表示存儲(chǔ)的數(shù)據(jù)永遠(yuǎn)不過期(但可被服務(wù)器算法:LRU 等替換)惜浅。如果非0(unix時(shí)間或者距離此時(shí)的秒數(shù)),當(dāng)過期后,服務(wù)器可以保證用戶得不到該數(shù)據(jù)(以服務(wù)器時(shí)間為標(biāo)準(zhǔn))。
<bytes>需要存儲(chǔ)的字節(jié)數(shù)伏嗜,當(dāng)用戶希望存儲(chǔ)空數(shù)據(jù)時(shí)<bytes>可以為0
<data block>需要存儲(chǔ)的內(nèi)容坛悉,輸入完成后伐厌,最后客戶端需要加上\r\n(直接點(diǎn)擊Enter)作為結(jié)束標(biāo)志。
- Memcached數(shù)據(jù)示例
set key1 1 50 3 #儲(chǔ)存key名為key1裸影,flags為1挣轨,過期時(shí)間為50s,字節(jié)數(shù)為3的數(shù)據(jù)
cba
STORED
get key1 #查看key1
VALUE key1 1 3
cba
END
add key2 2 0 4 #添加數(shù)據(jù)
qwer
STORED
get key2
VALUE key2 2 4
qwer
END
replace key2 2 0 5 #替換數(shù)據(jù)
abcde
STORED
get key2
VALUE key2 2 5
abcde
END
delete key2 #刪除數(shù)據(jù)
DELETED
get key2
END
- Memcached數(shù)據(jù)導(dǎo)出和導(dǎo)入
[root@minglinux-01 ~] telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set name 1 0 4
ming
STORED
set age 1 0 2
20
STORED
^] #ctrl+]退出
telnet> quit
Connection closed.
[root@minglinux-01 ~] memstat --servers=127.0.0.1:11211
Server: 127.0.0.1 (11211)
pid: 80200
uptime: 46369
time: 1548128359
version: 1.4.15
libevent: 2.0.21-stable
pointer_size: 64
rusage_user: 1.038780
rusage_system: 1.175762
curr_connections: 10
total_connections: 17
connection_structures: 11
reserved_fds: 20
cmd_get: 5
cmd_set: 6
cmd_flush: 0
cmd_touch: 0
get_hits: 3
get_misses: 2
delete_misses: 0
delete_hits: 1
incr_misses: 0
incr_hits: 0
decr_misses: 0
decr_hits: 0
cas_misses: 0
cas_hits: 0
cas_badval: 0
touch_hits: 0
touch_misses: 0
auth_cmds: 0
auth_errors: 0
bytes_read: 278
bytes_written: 4315
limit_maxbytes: 67108864
accepting_conns: 1
listen_disabled_num: 0
threads: 4
conn_yields: 0
hash_power_level: 16
hash_bytes: 524288
hash_is_expanding: 0
bytes: 143
curr_items: 2
total_items: 5
expired_unfetched: 0
evicted_unfetched: 0
evictions: 0
reclaimed: 1
[root@minglinux-01 ~] memcached-tool 127.0.0.1:11211 dump
Dumping memcache contents
Number of buckets: 1
Number of items : 2
Dumping bucket 1 - 2 total items
add name 1 1548081990 4
ming
add age 1 1548081990 2
20
[root@minglinux-01 ~] memcached-tool 127.0.0.1:11211 dump > data.txt #數(shù)據(jù)導(dǎo)出
Dumping memcache contents
Number of buckets: 1
Number of items : 2
Dumping bucket 1 - 2 total items
[root@minglinux-01 ~] cat data.txt
add name 1 1548081990 4
ming
add age 1 1548081990 2
20
[root@minglinux-01 ~] systemctl restart memcached #memcached存儲(chǔ)在內(nèi)存轩猩,重啟服務(wù)清除數(shù)據(jù)
[root@minglinux-01 ~] nc 127.0.0.1 11211 < data.txt
STORED
STORED
#注意:導(dǎo)出的數(shù)據(jù)是帶有一個(gè)時(shí)間戳的卷扮,這個(gè)時(shí)間戳就是該條數(shù)據(jù)過期的時(shí)間點(diǎn),
#如果當(dāng)前時(shí)間已經(jīng)超過該時(shí)間戳均践,那么是導(dǎo)入不進(jìn)去的
[root@minglinux-01 ~] telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get name
END
get age
END
^]
telnet> quit
Connection closed.
[root@minglinux-01 ~] date
2019年 02月 15日 星期五 21:39:50 CST
[root@minglinux-01 ~] date -d @1548081990
2019年 01月 21日 星期一 22:46:30 CST #該時(shí)間戳已經(jīng)過期晤锹,數(shù)據(jù)導(dǎo)入不成功
[root@minglinux-01 ~] date -d "+1 hour" +%s
1550241775
[root@minglinux-01 ~] date -d @1550241775
2019年 02月 15日 星期五 22:42:55 CST
[root@minglinux-01 ~] cat data.txt #修改data.txt文件里的時(shí)間戳如下
add name 1 1550241775 4
ming
add age 1 1000 2
20
[root@minglinux-01 ~] systemctl restart memcached.service
[root@minglinux-01 ~] nc 127.0.0.1 11211 < data.txt
STORED
STORED
[root@minglinux-01 ~] telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get name
VALUE name 1 4
ming
END
get age #age數(shù)據(jù)也導(dǎo)入成功
VALUE age 1 2
20
END
- PHP連接Memcached
#安裝php的memcache擴(kuò)展
[root@minglinux-01 /usr/local/sbin] wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz
[root@minglinux-01 /usr/local/sbin] tar -zxvf memcache-2.2.3.tgz
[root@minglinux-01 /usr/local/sbin] cd memcache-2.2.3/
[root@minglinux-01 /usr/local/sbin/memcache-2.2.3] ls
config9.m4 memcache.c memcache_session.c
config.m4 memcache_consistent_hash.c memcache_standard_hash.c
config.w32 memcache.dsp php_memcache.h
CREDITS memcache_queue.c README
example.php memcache_queue.h
[root@minglinux-01 /usr/local/sbin/memcache-2.2.3] /usr/local/php-fpm/bin/phpize #生成configure文件
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
[root@minglinux-01 /usr/local/sbin/memcache-2.2.3] ls
acinclude.m4 config.sub Makefile.global missing
aclocal.m4 configure memcache.c mkinstalldirs
autom4te.cache configure.in memcache_consistent_hash.c php_memcache.h
build config.w32 memcache.dsp README
config9.m4 CREDITS memcache_queue.c run-tests.php
config.guess example.php memcache_queue.h
config.h.in install-sh memcache_session.c
config.m4 ltmain.sh memcache_standard_hash.c
[root@minglinux-01 /usr/local/sbin/memcache-2.2.3] ./configure --with-php-config=/usr/local/php-fpm/bin/php-config #安裝
[root@minglinux-01 /usr/local/sbin/memcache-2.2.3] echo $?
0
[root@minglinux-01 /usr/local/sbin/memcache-2.2.3] make && make install
#安裝完成有如下提示,表示memcache擴(kuò)展模塊的存放路徑
#Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
[root@minglinux-01 /usr/local/sbin/memcache-2.2.3] ls /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
memcache.so opcache.a opcache.so
#然后修改php.ini添加一行extension="memcache.so"
[root@minglinux-01 /usr/local/sbin/memcache-2.2.3] vim /usr/local/php-fpm/etc/php.ini
#重啟php-fpm服務(wù)
#查看memcache模塊是否成功加載
[root@minglinux-01 /usr/local/sbin/memcache-2.2.3] /usr/local/php-fpm/sbin/php-fpm -m | grep memcache
memcache
#創(chuàng)建一個(gè)測(cè)試腳本
#腳本內(nèi)容參考https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/1.php
[root@minglinux-01 /usr/local/sbin/memcache-2.2.3] vim 1.php
#內(nèi)容如下:
1 <?php
2 //連接Memcache Memcache
3 $mem = new Memcache;
4 $mem->connect("localhost", 11211);
5 //保存數(shù)據(jù)
6 $mem->set('key1', 'This is first value', 0, 60);
7 $val = $mem->get('key1');
8 echo "Get key1 value: " . $val ."<br>";
9 //替換數(shù)據(jù)
10 $mem->replace('key1', 'This is replace value', 0, 60);
11 $val = $mem->get('key1');
12 echo "Get key1 value: " . $val . "<br>";
13 //保存數(shù)組數(shù)據(jù)
14 $arr = array('aaa', 'bbb', 'ccc', 'ddd');
15 $mem->set('key2', $arr, 0, 60);
16 $val2 = $mem->get('key2');
17 echo "Get key2 value: ";
18 print_r($val2);
19 echo "<br>";
20 //刪除數(shù)據(jù)
21 $mem->delete('key1');
22 $val = $mem->get('key1');
23 echo "Get key1 value: " . $val . "<br>";
24 //清除所有數(shù)據(jù)
25 $mem->flush();
26 $val2 = $mem->get('key2');
27 echo "Get key2 value: ";
28 print_r($val2);
29 echo "<br>";
30 //關(guān)閉連接
31 $mem->close();
32 ?>
#執(zhí)行腳本
[root@minglinux-01 /usr/local/sbin/memcache-2.2.3] /usr/local/php-fpm/bin/php 1.php
Get key1 value: This is first value<br>Get key1 value: This is replace value<br>Get key2 value: Array
(
[0] => aaa
[1] => bbb
[2] => ccc
[3] => ddd
)
<br>Get key1 value: <br>Get key2 value: <br>
#或者將1.php放到某個(gè)虛擬主機(jī)根目錄下面彤委,在瀏覽器訪問鞭铆,即可看到效果
- Memcached中存儲(chǔ)session
#實(shí)例是在lamp/lnmp環(huán)境下實(shí)現(xiàn)
修改配置
- 編輯php.ini添加兩行
session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211"- apache在httpd.conf中對(duì)應(yīng)的虛擬主機(jī)中添加
php_value session.save_handler "memcache"
php_value session.save_path "tcp://127.0.0.1:11211"- nginx在php-fpm.conf對(duì)應(yīng)的pool中添加(這里使用3)
php_value[session.save_handler] = memcache
php_value[session.save_path] = " tcp://127.0.0.1:11211 "
測(cè)試
#下載測(cè)試代碼
[root@minglinux-01 ~] wget http://study.lishiming.net/.mem_se.txt
#session.php內(nèi)容可以參考https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/session.php
[root@minglinux-01 /data/wwwroot/default] mv /root/.mem_se.txt 1.php #將測(cè)試網(wǎng)頁放到站點(diǎn)根目錄
[root@minglinux-01 /data/wwwroot/default] curl localhost/1.php
1550302840<br><br>1550302840<br><br>q6e9f7hck0jdnpv33k1cmlpak6
[root@minglinux-01 /data/wwwroot/default] ls -lt /tmp/
總用量 4
-rw------- 1 php-fpm php-fpm 37 2月 16 15:35 sess_lg5nghee3afa3jr68u1icctsd1
-rw-r--r-- 1 root root 0 2月 16 15:33 test.com.log
drwx------ 2 root root 40 2月 14 21:58 vmware-root
srwxrwxrwx 1 mysql mysql 0 12月 24 22:45 mysql.sock
srw-rw-rw- 1 root root 0 12月 24 22:45 ming.sock
srw-rw-rw- 1 root root 0 12月 24 22:45 php-fcgi.sock
[root@minglinux-01 /data/wwwroot/default] rm -rf /tmp/sess_lg5nghee3afa3jr68u1icctsd1
[root@minglinux-01 /data/wwwroot/default] ls -lt /tmp/
總用量 0
-rw-r--r-- 1 root root 0 2月 16 15:33 test.com.log
drwx------ 2 root root 40 2月 14 21:58 vmware-root
srwxrwxrwx 1 mysql mysql 0 12月 24 22:45 mysql.sock
srw-rw-rw- 1 root root 0 12月 24 22:45 ming.sock
srw-rw-rw- 1 root root 0 12月 24 22:45 php-fcgi.sock
#在php-fpm.conf對(duì)應(yīng)的pool中添加兩行
[root@minglinux-01 /data/wwwroot/default] vim /usr/local/php-fpm/etc/php-fpm.d/ming.conf
1 [ming]
4 user = php-fpm
5 group = php-fpm
6 pm = dynamic
7 pm.max_children = 50
8 pm.start_servers = 20
9 pm.min_spare_servers = 5
10 pm.max_spare_servers = 35
11 pm.max_requests = 500
12 rlimit_files = 1024
13 php_value[session.save_handler] = memcache
14 php_value[session.save_path] = " tcp://127.0.0.1:11211 "
[root@minglinux-01 /data/wwwroot/default] /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@minglinux-01 /data/wwwroot/default] curl localhost/1.php
1550325224<br><br>1550325224<br><br>5nt1uusu6871nid87o5b54qs26
[root@minglinux-01 /data/wwwroot/default] telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get 5nt1uusu6871nid87o5b54qs26
VALUE 5nt1uusu6871nid87o5b54qs26 0 37
TEST|i:1550325224;TEST3|i:1550325224;
END