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

緩存服務器的建立原理分析第1/2頁

 更新時間:2008年10月10日 09:51:09   作者:  
通常情況下我們運行程序的過程中會產生一些中間數(shù)據(jù),這些中間數(shù)據(jù)需要在將來的某個時間讀取。這就要求我們要把它存在一個提供高速存取的地方,最好的選擇就是內存中。
 1概述
通常情況下我們運行程序的過程中會產生一些中間數(shù)據(jù),這些中間數(shù)據(jù)需要在將來的某個時間讀取。這就要求我們要把它存在一個提供高速存取的地方,最好的選擇就是內存中?;谶@個以及多個原因需要我們把這部分存儲到其他機器上,這樣就產生了分布式緩存的問題。
實際上分布式緩存根本上就是提供一個附加內存讓另一臺機器幫忙存儲和查找數(shù)據(jù)。
2實現(xiàn)方法
首先建立一個集合對象,該集合對象應保證線程安全。代碼如下所示
Code
1 public static class MemObject
2 {
3 static MemObject()
4 {
5 MemObjl = new Dictionary<string, object>();
6 }
7
8 public static Dictionary<string, object> Get()
9 {
10 if (MemObjl == null)
11 MemObjl = new Dictionary<string, object>();
12 return MemObjl;
13 }
14
15 public static void Add(string key, object obj)
16 {
17 Dictionary<string, object> obg = Get();
18 if (!obg.ContainsKey(key))
19 obg.Add(key, obj);
20 }
21
22 public static void Remove(string key)
23 {
24 Get().Remove(key);
25 }
26
27 public static int Count()
28 {
29 return Get().Count;
30 }
31
32 public static object Get(string key)
33 {
34 Dictionary<string, object> obg = Get();
35 if (obg.ContainsKey(key))
36 return obg[key];
37 return null;
38 }
39
40 public static bool Exits(string key)
41 {
42 return Get().ContainsKey(key);
43 }
44
45 private static Dictionary<string, object> MemObjl;
46 }
接著我們把它包裝起來可以通過遠程調用,代碼如下
Code
1 public class DataCatcher : MarshalByRefObject, ICarrier.ICarrier
2 {
3 public void Set(string key, object value)
4 {
5 //if (Exits(key))
6 // Remove(key);
7 //MemObjl.Add(key, value);
8 MemObject.Add(key, value);
9 }
10
11 public bool Exits(string key)
12 {
13 return MemObject.Exits(key);
14 }
15
16 public void Remove(string key)
17 {
18 MemObject.Remove(key);
19 }
20
21 public int Count()
22 {
23 return MemObject.Count();
24 }
25
26 public object Get(string key)
27 {
28 return MemObject.Get(key);
29 }
30 }
為了避免我們的業(yè)務邏輯泄露我們向客戶端提供接口以便調用
Code
1 public interface ICarrier
2 {
3
4 void Remove(string key);
5
6 bool Exits(string key);
7
8 void Set(string key,object value);
9
10 object Get(string key);
11
12 int Count();
13 }

相關文章

最新評論