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 }
12下一页阅读全文
通常情况下我们运行程序的过程中会产生一些中间数据,这些中间数据需要在将来的某个时间读取。这就要求我们要把它存在一个提供高速存取的地方,最好的选择就是内存中。基于这个以及多个原因需要我们把这部分存储到其他机器上,这样就产生了分布式缓存的问题。
实际上分布式缓存根本上就是提供一个附加内存让另一台机器帮忙存储和查找数据。
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 }
12下一页阅读全文
标签:
缓存,服务器
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金钱帮资源网 Copyright www.kbjia.com
暂无“缓存服务器的建立原理分析第1/2页”评论...
更新日志
2024年06月14日
2024年06月14日
- 世界启元新手进阶开荒队攻略 首充礼包阵容推荐
- [中友缘唱片]原创音乐《寂寞缠绕DSD》WAV+CUE
- 顶级发烧制作-群星《大牌翻唱1-2》WAV
- 詹姆斯·拉斯特-世界级指挥及演奏大师《天堂鸟Paradiesvogel》[WAV+CUE]
- 吕方 情歌王子《旧情人·老情歌精选 2CD》[WAV+CUE][900MB]
- 邰正宵《用情太深》DSD+SBM版[WAV+CUE][600MB]
- 梅艳芳《绝代芳华》DSD+SBM版[WAV+CUE][600MB]
- 霍正奇.1991-雨的脸我的脸【飞鹰】【WAV+CUE】
- 张雨生.1995-还是朋友【飞碟】【WAV+CUE】
- 艾斯卡尔.2020-祝福【喜洋洋】【WAV+CUE】
- 世界启元巴罗萨怎么样 世界启元巴罗萨介绍
- 世界启元世界进程系统是什么 世界进程系统机制介绍
- 世界启元汉尼怎么样 世界启元汉尼英雄介绍
- 老鹰乐队《加州旅店》1976[FLAC+CUE]
- 清晰稳定的结像聚集《专辑主打我最红》2CD[DTS-WAV]