当前位置: 移动技术网 > 网络运营>服务器>Linux > 缓存服务器的建立原理分析第1/2页

缓存服务器的建立原理分析第1/2页

2019年05月07日  | 移动技术网网络运营  | 我要评论
 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 }
1

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网