欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

緩存服務(wù)器的建立原理分析第2/2頁(yè)

 更新時(shí)間:2008年10月10日 09:51:09   作者:  
通常情況下我們運(yùn)行程序的過(guò)程中會(huì)產(chǎn)生一些中間數(shù)據(jù),這些中間數(shù)據(jù)需要在將來(lái)的某個(gè)時(shí)間讀取。這就要求我們要把它存在一個(gè)提供高速存取的地方,最好的選擇就是內(nèi)存中。

好了。這樣我們服務(wù)端的代碼就算搞定了。
下面我們來(lái)發(fā)布服務(wù)供客戶(hù)端調(diào)用
Code
1 public partial class SharpCatchedService : ServiceBase
2 {
3 public SharpCatchedService()
4 {
5 InitializeComponent();
6 }
7
8 protected override void OnStart(string[] args)
9 {
10 TcpChannel channel = new TcpChannel(ConfigHelper.Port);
11 ChannelServices.RegisterChannel(channel, false);
12 RemotingConfiguration.RegisterWellKnownServiceType(typeof(DataCatcher),
13 "SharpCatched", WellKnownObjectMode.Singleton);
14 }
15
16 protected override void OnStop()
17 {
18 }
19 }
這樣客戶(hù)端就可以通過(guò)這個(gè)接口來(lái)實(shí)現(xiàn)遠(yuǎn)程數(shù)據(jù)的存取
在客戶(hù)端首先我們獲取遠(yuǎn)程的對(duì)象
Code
public static ICarrier Carrier()
{
ICarrier carrier = (ICarrier)Activator.GetObject(typeof(ICarrier), "tcp://localhost:" + ConfigHelper.Port + "/SharpCatched");
return carrier;
}
接著我們包裝一下
Code
1 public class SharpCatchedAPI
2 {
3 ICarrier icarrier;
4
5 public void Init()
6 {
7 icarrier = DoConnect.Carrier();
8 }
9
10 public void Set(string key, object value)
11 {
12 icarrier.Set(key, value);
13 }
14
15 public void Remove(string key)
16 {
17 icarrier.Remove(key);
18 }
19
20 public object Get(string key)
21 {
22 return icarrier.Get(key);
23 }
24
25 public bool Exits(string key)
26 {
27 return icarrier.Exits(key);
28 }
29 }
3后續(xù)
以上實(shí)現(xiàn)的是最基本的分布式緩存解決方案。其實(shí)我們可以把這個(gè)集合轉(zhuǎn)化為其他集合對(duì)象,例如HashTable。在對(duì)象啟動(dòng)的時(shí)候開(kāi)啟一個(gè)守護(hù)線(xiàn)程,這個(gè)進(jìn)程做得工作就是把到期的緩存對(duì)象追加到一個(gè)集合對(duì)象中,然后遍歷該對(duì)象實(shí)現(xiàn)緩存對(duì)象的銷(xiāo)毀。我們還可以把對(duì)象進(jìn)行一次哈希讓對(duì)像在多臺(tái)緩存服務(wù)器上存儲(chǔ)。好了今天就寫(xiě)到這里吧。
以上文字希望能起到拋磚引玉的作用,引起大家更深層次的思考。

相關(guān)文章

最新評(píng)論