緩存服務器的建立原理分析第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 }
通常情況下我們運行程序的過程中會產生一些中間數(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 }
相關文章
apache,nginx上傳目錄無執(zhí)行權限的設置方法
至于為什么設置上傳目錄無權限這個我就不累贅了,現(xiàn)在比較流行的web服務有iis,apache,nginx,使用操作系統(tǒng)無非是windows or *nux2010-12-12CentOS 6.5平臺實現(xiàn)快速部署FTP的方法
這篇文章主要介紹了CentOS 6.5平臺實現(xiàn)快速部署FTP的方法,結合實例形式分析了CentOS6.5平臺配置與部署FTP的具體步驟、相關操作命令與注意事項,需要的朋友可以參考下2018-04-04在CentOS VPS上通過SSH安裝 MySQL的方法圖解
這篇文章主要介紹了在CentOS VPS上通過SSH安裝 MySQL,需要的朋友可以參考下2018-12-12win7中VMware安裝CentOs7搭建Linux環(huán)境教程
這篇文章主要為大家詳細介紹了win7中VMware虛擬機安裝CentOs7搭建Linux環(huán)境教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11Canonical通過Flutter啟用Linux桌面應用程序(推薦)
這篇文章主要介紹了Canonical通過Flutter啟用Linux桌面應用程序,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07詳解如何在Ubuntu 16.04上增加Swap分區(qū)
本篇文章主要介紹了詳解如何在Ubuntu 16.04上增加Swap分區(qū),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Linux tomcat下catalina.out日志文件分割
這篇文章主要介紹了Linux tomcat下catalina.out日志文件分割的相關資料,需要的朋友可以參考下2017-02-02使用FileZilla從Linux系統(tǒng)下載文件的方法
最近做項目,遇到這樣的需求,要求將Linux系統(tǒng)的的某個文件夾下載到我Windows系統(tǒng)某個文件夾里,怎么實現(xiàn)這個功能呢?下面腳本之家小編給大家?guī)砹耸褂肍ileZilla從Linux系統(tǒng)下載文件的方法,感興趣的朋友一起看看吧2018-07-07