一: 安裝
? ? ?memcahce像redis,mongodb一樣都需要開啟他們自己的服務端,我們下載 Memcached_1.2.5.zip ,然后放到C盤,修改文件
名為memcached。
1:install
? ? ?install可以說是萬能通用命令,首先我們轉到memcached目錄,然后?memcached.exe -d install 即可。
? ? ? ??
2:start
? ? 現在我們只要啟動start即可,要注意的就是memecache默認的端口是11211,當然我也不想重新指定端口了。
? ? ? ??
3:stop,uninstall
? ? ?這兩個就不截圖了,一個是停止,一個是卸載,反正都是萬能通用命令。
?
二:驅動程序
? ?memcache的服務器我們就已經開啟好了,由于在公司最近一直都在用php,算了還是用C#驅動吧,誰讓這是.net
社區呢,下載 C#驅動 ,既然是緩存服務器,只要有基本的CURD,我想應該就差不多了。
1
using
System;
2
using
System.Collections.Generic;
3
4
namespace
BeIT.MemCached
5
{
6
class
Example
7
{
8
public
static
void
Main(
string
[] args)
9
{
10
//
通過配置文件初始化memcache實例
11
MemcachedClient cache = MemcachedClient.GetInstance(
"
MyConfigFileCache
"
);
12
13
//
編輯(可以模擬session操作,緩存20分鐘)
14
cache.Set(
"
name
"
,
"
一線碼農
"
, DateTime.Now.AddMinutes(
20
));
15
16
//
獲取
17
var
result = cache.Get(
"
name
"
);
18
19
Console.WriteLine(
"
獲取name的緩存數據為:
"
+
result);
20
21
//
刪除
22
cache.Delete(
"
name
"
);
23
24
Console.WriteLine(
"
\n成功刪除cache中name的數據
"
);
25
26
result = cache.Get(
"
name
"
);
27
28
Console.WriteLine(
"
\n再次獲取cache中name的數據為:
"
+ (result ??
"
null
"
) +
"
\n
"
);
29
30
//
查看下memecahce的運行情況
31
foreach
(KeyValuePair<
string
, Dictionary<
string
,
string
>> host
in
cache.Status())
32
{
33
Console.Out.WriteLine(
"
Host:
"
+
host.Key);
34
foreach
(KeyValuePair<
string
,
string
> item
in
host.Value)
35
{
36
Console.Out.WriteLine(
"
\t
"
+ item.Key +
"
:
"
+
item.Value);
37
}
38
Console.Out.WriteLine();
39
}
40
41
Console.Read();
42
}
43
}
44
}
我們再定義下配置文件,既然memcache可以用于分布式,那就避免不了將cache分攤到幾臺服務器上去,可以看到,下面的
配置也是非常簡單的,當然分配的法則自然是memcache自身的算法決定的,最后別忘了在另一臺服務器上開放一個端口就它
就行了。
<?
xml version="1.0" encoding="utf-8"
?>
<
configuration
>
<
configSections
>
<
section
name
="beitmemcached"
type
="System.Configuration.NameValueSectionHandler"
/>
</
configSections
>
<
appSettings
>
</
appSettings
>
<
beitmemcached
>
<
add
key
="MyConfigFileCache"
value
="127.0.0.1:11211"
/>
<!--
<add key="MyConfigFileCache" value="127.0.0.1:11211,127.0.0.1:8888" />
-->
</
beitmemcached
>
</
configuration
>
?
下面是打包程序: BeITMemcached ?,也可以到 codegoogle 去下載。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

