Implement distributed cache

Source: Internet
Author: User

1 Overview

Generally, some intermediate data is generated during the process of running the program, which needs to be read at a certain time in the future. This requires us to put it in a place that provides high-speed access, the best choice is the memory. For this and multiple reasons, we need to store this part on other machines, which leads to the distributed cache problem.

In fact, the distributed cache basically provides an additional memory for another machine to help store and find data.

2. Implementation Method

First, create a collection object, which should ensure thread security. 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}
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}

 

Then we can package it for remote calling. 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 ()
22 {
23 return memobject. Count ();
24}
25
26 public object get (string key)
27 {
28 return memobject. Get (key );
29}
30}

 

To prevent leakage of our business logic, we provide interfaces to clients 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}

 

Okay. In this way, the server code is handled.

Let's publish the service for the client 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 use this interface to achieve remote data access.

On the client, we first obtain the remote object.

 

Code
Public static icarrier carrier ()
{
Icarrier carrier = (icarrier) activator. GetObject (typeof (icarrier), "TCP: // localhost:" + confighelper. Port + "/sharpcatched ");
Return carrier;
}

Next let's wrap it up.

 

Code
1 public class sharpcatchedapi
2 {
3 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. Follow-up

The above is the most basic distributed cache solution. In fact, we can convert this set into other set objects, such as hashtable. When an object is started, start a daemon thread. This process is used to append the expired cache object to a collection object, and traverse the object to destroy the cache object. We can also hash the object to store the object on multiple cache servers.

 

Http://blog.csdn.net/mengzi_ai/article/details/4868094

Implement distributed cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.