1、安裝memcache擴展
首先咱們經過phpinfo()函數查看一下咱們當前的php環境是否支持memcache擴展,在服務器的根目錄下新建一個文件info.php,在文件中寫入php
1
2
|
<?php
phpinfo();
|
而後在瀏覽器中輸入 http://localhost/info.php 訪問,而後查找是否有memcache擴展,通常咱們的服務器默認是沒有安裝memcache擴展的,因此仍是得咱們本身來安裝。咱們先到網上下載 php_memcache.dll文件,把文件拷貝到php擴展目錄下(個人是php5/ext/),在擴展目錄下加上這個文件還沒完成,咱們要在php 的配置文件php.ini文件中加入extension=php_memcache.dll,php環境會自動找到php擴展目錄將這個擴展加到php環 境中,這個時候咱們再重啓apache,而後再來訪問 http://localhost/info.php ,就能夠看到mysql
這就說明咱們的memcache擴展安裝好了!咱們再查看php手冊,發現memcache擴展的使用有兩種方式,第一種是面向過程的使用方式,還有一種是面向對象的使用方式,而咱們通常經常使用的是面向對象的方式。web
2、memcache的使用實例
直接貼代碼了!sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
<?php
//實例化memcache類
$mem
=
new
Memcache;
//鏈接memcache服務器(參數爲服務器IP, 端口),
//pconnect--表示持續鏈接
$mem
->connect(
'localhost'
, 11211);
//addserver表示增長memcache服務器,
//多個memcache服務器能夠實現分佈式緩存
//$mem->addsever('www.pccxin.com', 11211);
//$mem->addsever('www.frontu.net', 11211);
//向memcache服務器中增長元素
//bool Memcache::add ( string $key ,
//mixed $var [, int $flag [, int $expire ]] )
//參數爲 鍵名, 值(字符串|數組|對象),
//使用 MEMCACHE_COMPRESSED 標記對數據進行壓縮(使用zlib),
// 保存時間(單位秒)
$mem
->add(
'mystr'
,
'This is my first memcache test!'
,
MEMCACHE_COMPRESSED, 3600);
//add不會重複添加,要想改變值可用replace(),或者set
//$mem->add('mystr', 'This is my first memcache test!',
MEMCACHE_COMPRESSED, 3600);
//向服務器中保存數據
$mem
->set(
'mystr'
,
'This is my second memcache test!'
,
MEMCACHE_COMPRESSED, 3600);
//從服務端刪除一個元素
//$mem->delete('mystr');
//清洗(刪除)已經存儲的全部的元素
//$mem->flush();
//獲取memcache中的數據
echo
$mem
->get(
'mystr'
).
'<br />'
;
//向memcache服務器中增長元素
$mem
->add(
'myarr'
,
array
(
'1'
=>
'aaa'
,
'2'
=>
'bb'
,
'3'
=>
'cc'
),
MEMCACHE_COMPRESSED, 3600);
var_dump(
$mem
->get(
'myarr'
));
echo
'<br />'
;
class
Person{
var
$name
=
'shawnking'
;
var
$sex
=
'男'
;
}
//向memcache服務器中增長元素
$mem
->add(
'myobj'
,
new
Person);
var_dump(
$mem
->get(
'myobj'
));
echo
'<br />'
;
//獲取memcache的版本信息
echo
'Version:'
,
$mem
->getVersion();
//獲得memcache的參數信息
echo
'<pre>'
;
print_r(
$mem
->getStats());
echo
'</pre>'
;
//關閉到memcached服務端的鏈接。這個函數不會關閉持久化鏈接,
// 持久化鏈接僅僅會在web服務器關機/重啓時關閉。與之對應的,你也能夠使用
$mem
->close();
|
3、php在什麼地方使用memcache
a、數據庫中讀出來的數據(select) 使用memcache處理
一般狀況下咱們訪問一次頁面php就會鏈接一次數據庫,就會到數據庫中讀取數據,若是訪問量大的時候數據庫就沒法承受壓力了,咱們使用 memcache的 話,只要頁面第一次被訪問php就會把數據存到memcache服務器中,而且設定一個過時時間,這樣在過時時間以前都不須要去數據庫讀取數據,這個能夠 大大提成網站性能(咱們memcache的數據是存在內存中的,因此讀取起來很是快)。下面我就貼出在數據庫中使用memcache的示例代碼:數據庫
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
<?php
//實例化一個memcache對象
$mem
=
new
Memcache;
//鏈接memcache服務器
$mem
->connect(
'localhost'
, 11211);
/**
* 注意:
* 一、同一個項目安裝兩次,key要有前綴
* $key = 'a_test';
* $key = 'b_test';
* 二、 用sql語句做爲下標,這樣可讓相同sql語句的數據只要存一份到memcache
*/
$sql
=
'SELECT * FROM test'
;
$key
=
substr
(md5(
$sql
), 10, 8);
//從memcache服務器獲取數據
$data
=
$mem
->get(
$key
);
//判斷memcache是否有數據
if
( !
$data
){
$mysqli
=
new
mysqli(
'localhost'
,
'root'
,
'123456'
,
'testdb'
);
$result
=
$mysqli
->query(
$sql
);
$data
=
array
();
while
(
$row
=
$result
->fetch_assoc() ){
$data
[] =
$row
;
}
$result
->free();
//釋放內存
$mysqli
->close();
//斷開mysql鏈接
//向memcache服務器存儲數據,還要設置失效時間(單位爲秒)
$mem
->set(
$key
,
$data
, MEMCACHE_COMPRESSED, 3600);
}
print_r(
$data
);
$mem
->close();
//關閉memcache鏈接
b、在會話控制session中使用
將session信息寫入到memcache服務器當中
<?php
/**
* session保存到memcache類
*/
class
MemSession{
private
static
$handler
= null;
private
static
$lifetime
= null;
private
static
$time
= null;
const
NS =
'session_'
;
/**
* 初始化函數
*/
private
static
function
init(
$handler
){
self::
$handler
=
$handler
;
self::
$lifetime
=
ini_get
(
'session.gc_maxlifetime'
);
self::
$time
= time();
}
public
static
function
start(Memcache
$memcache
){
self::init(
$memcache
);
session_set_save_handler(
array
(
__CLASS__
,
'open'
);
array
(
__CLASS__
,
'close'
);
array
(
__CLASS__
,
'read'
);
array
(
__CLASS__
,
'write'
);
array
(
__CLASS__
,
'destrory'
);
array
(
__CLASS__
,
'gc'
);
);
session_start();
}
public
static
function
open(
$path
,
$name
){
return
true;
}
public
static
function
close(){
return
true;
}
public
static
function
read(
$PHPSESSID
){
$out
= self::
$handler
->get(self::
$session_key
(
$PHPSESSID
));
if
(
$out
=== false ||
$out
= null )
return
''
;
return
$out
;
}
public
static
function
write(
$PHPSESSID
,
$data
){
$method
=
$data
?
'set'
:
'replace'
;
return
self::
$handler
->
$method
(self::
$session_key
(
$PHPSESSID
),
$data
, MEMCACHE_COMPRESSED, self::
$lifetime
);
}
public
static
function
destroy(
$PHPSESSID
){
return
sele::
$handler
->
delete
(self::
$session_key
(
$PHPSESSID
));
}
public
static
function
gc(
$lifetime
){
return
true;
}
private
static
session_key(
$PHPSESSID
){
$session_key
= self::NS.
$PHPSESSID
;
return
$session_key
;
}
}
$memcache
=
new
Memcache;
$memcache
->connect(
'localhost'
, 11211)
or
die
(
'could not connect!'
);
MemSession::start(
$memcache
);
|
4、memcache的安全(不讓別人訪問)
一、內網鏈接apache
二、設置防火牆數組
iptables -A INPUT -p tcp -dport 11211 -j DROP 來拒絕所有的訪問,瀏覽器
再設置能夠訪問的IP緩存
iptables -A INPUT -p tcp -s 192.168.1.111 -dport 11211 -j ACCEPT安全
iptables -A INPUT -p udp -s 192.168.1.111 -dpost 11211 -j ACCEPT