国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

JAVA實現分布式緩存

2019-11-17 04:01:11
字體:
來源:轉載
供稿:網友
1概述

     通常情況下我們運行程序的過程中會產生一些中間數據,這些中間數據需要在將來的某個時間讀取。這就要求我們要把它存在一個提供高速存取的地方,最好的選擇就是內存中。基于這個以及多個原因需要我們把這部分存儲到其他機器上,這樣就產生了分布式緩存的問題。

    實際上分布式緩存根本上就是提供一個附加內存讓另一臺機器幫忙存儲和查找數據。

2實現方法

    首先建立一個集合對象,該集合對象應保證線程安全。代碼如下所示

    

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     }
為了避免我們的業務邏輯泄露我們向客戶端提供接口以便調用

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     }
好了。這樣我們服務端的代碼就算搞定了。

下面我們來發布服務供客戶端調用

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     }
這樣客戶端就可以通過這個接口來實現遠程數據的存取

在客戶端首先我們獲取遠程的對象

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后續

     以上實現的是最基本的分布式緩存解決方案。其實我們可以把這個集合轉化為其他集合對象,例如HashTable。在對象啟動的時候開啟一個守護線程,這個進程做得工作就是把到期的緩存對象追加到一個集合對象中,然后遍歷該對象實現緩存對象的銷毀。我們還可以把對象進行一次哈希讓對像在多臺緩存服務器上存儲
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 遵义县| 丽水市| 太仓市| 兴业县| 紫阳县| 梁山县| 稻城县| 平南县| 平潭县| 桃园县| 宝应县| 梁山县| 乐陵市| 许昌县| 达日县| 武乡县| 咸阳市| 娄底市| 阳山县| 柳州市| 葫芦岛市| 诸暨市| 石楼县| 东兰县| 赤峰市| 辉南县| 晋宁县| 安阳县| 汉沽区| 沂南县| 逊克县| 宝清县| 万年县| 桐庐县| 绩溪县| 凤山县| 会理县| 平遥县| 富宁县| 韶山市| 临夏县|