1 Overview
Usually when we run the program, some intermediate data will be generated, and these intermediate data need to be read at some time in the future. This requires us to store it in a place that provides high-speed access. The best choice is in memory. For this and many reasons, we need to store this part on other machines, which creates a distributed cache problem.
In fact, a distributed cache basically provides an additional memory for another machine to help store and find data.
2 Implementation methods
First create a collection object, which should be thread-safe. The code is as follows
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}
twenty one
22 public static void Remove(string key)
twenty three {
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 }
Then we wrap it up and call it remotely. The code is as follows
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()
twenty two {
23 return MemObject.Count();
twenty four }
25
26 public object Get(string key)
27 {
28 return MemObject.Get(key);
29 }
30}
In order to avoid leakage of our business logic, we provide an interface to the client for calling
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}
alright. In this way, our server-side code is completed.
Next we publish the service for clients to call
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}
In this way, the client can access remote data through this interface.
On the client side, first we get the remote object
Code
public static ICarrier Carrier()
{
ICarrier carrier = (ICarrier)Activator.GetObject(typeof(ICarrier), "tcp://localhost:" + ConfigHelper.Port + "/SharpCatched");
return carrier;
}
Then we package it
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 {
12icarrier.Set(key, value);
13}
14
15 public void Remove(string key)
16 {
17 icarrier.Remove(key);
18}
19
20 public object Get(string key)
twenty one {
22 return icarrier.Get(key);
twenty three }
twenty four
25 public bool Exits(string key)
26 {
27 return icarrier.Exits(key);
28 }
29 }
3 follow-up
The above implementation is the most basic distributed cache solution. In fact, we can convert this collection into other collection objects, such as HashTable. A daemon thread is started when the object is started. The job of this process is to append the expired cache object to a collection object, and then traverse the object to destroy the cache object. We can also hash the object once and store the object on multiple cache servers.