|
公司统一走redis缓存,也将之前的memcache迁移到redis
碰到问题是redis的dataset缓存。
memcache底层封装了dataset的序列化。
而redis引的DLL包,未支持。所以封装一个类,提供dataset的set,get功能。
dataset以转为byte[]保存,读取byte[] 转为dataset
代码如下
1 ///
2 /// 获取缓存(从2进制流)
3 ///
4 /// 缓存键
5 ///
6 public static object Get(string key)
7 {
8 byte[] buffer = _redisClient.Get(key);
9 return GetObjFromBytes(buffer);
10 }
11
12 ///
13 /// 从二进制流得到对象(dataset专用,dataset要序列化为二进制才可保存)
14 ///
15 ///
16 ///
17 private static object GetObjFromBytes(byte[] buffer) {
18 using (System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer))
19 {
20 stream.Position = 0;
21 System.Runtime.Serialization.IFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
22 Object reobj = bf.Deserialize(stream);
23 return reobj;
24 }
25 }
26
27 ///
28 /// 获取DATASET缓存
29 ///
30 /// 缓存键
31 ///
32 public static DataSet GetMemByDataSet(string key)
33 {
34 var item = Get(key);
35 return (DataSet)item;
36 }
37 ///
38 /// 获取DATASET缓存(如果没有,则调用Func方法返回对象并加入到缓存)
39 ///
40 /// 缓存键
41 /// 委托方法,返回指定对象类型,用于缓存不存在时回调该方法获取数据并插入到缓存
42 /// 过期时间(分钟)
43 ///
44 public static DataSet GetMemByDataSet(string key, Func func, int minute)
45 {
46 DataSet item = GetMemByDataSet(key);
47 if (item == null)
48 {
49 item = func();
50 if (item != null)
51 {
52 SetMemByDataSet(key, item, minute);
53 }
54 }
55 return item;
56 }
57 ///
58 /// 插入DATASET缓存
59 ///
60 /// 缓存键
61 /// 缓存对象
62 /// 过期时间(分钟)
63 public static void SetMemByDataSet(string key, DataSet ds, int minute)
64 {
65 DateTime expiryTime = DateTime.Now.AddMinutes(minute);
66 System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
67 System.IO.MemoryStream ms = new System.IO.MemoryStream();//创建内存流对象
68 formatter.Serialize(ms, ds);//把DataSet对象序列化到内存流
69 byte[] buffer = ms.ToArray();//把内存流对象写入字节数组
70 ms.Close();//关闭内存流对象
71 ms.Dispose();//释放资源
72 _redisClient.Set(key, buffer, expiryTime);
73 }
74
75 ///
76 /// 插入dictionary dataset缓存。
77 ///
78 /// redis保存键
79 /// Dictionary string 键 dataset 值
80 /// 缓存时间
81 public static void SetDicDataSets(string key, Dictionary dicdss, int minute)
82 {
83 //dataset转为二进制流
84 Dictionary ndic = new Dictionary();
85 DateTime expiryTime = DateTime.Now.AddMinutes(minute);
86 System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
87 System.IO.MemoryStream ms = new System.IO.MemoryStream();//创建内存流对象
88 foreach (var dsentry in dicdss)
89 {
90 formatter.Serialize(ms, dsentry.Value);//把DataSet对象序列化到内存流
91 byte[] buffer = ms.ToArray();//把内存流对象写入字节数组
92 ndic.Add(dsentry.Key, buffer);
93 //清空流
94 ms.SetLength(0); ms.Position = 0;
95 }
96 ms.Close();//关闭内存流对象
97 ms.Dispose();//释放资源
98 _redisClient.Set(key, ndic, expiryTime);
99 }
100
101 ///
102 /// 插入List. dictionary .dataset缓存。
103 ///
104 /// redis保存键
105 /// Dictionary string 键 dataset 值
106 /// 缓存时间
107 public static void SetListDicDataSets(string key,List Listdicdss, int minute)
108 {
109 DateTime expiryTime = DateTime.Now.AddMinutes(minute);
110 System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
111 List nlistdic=new List();
112 System.IO.MemoryStream ms = new System.IO.MemoryStream();//创建内存流对象
113 foreach (var dicdss in Listdicdss) {
114 //dataset转为二进制流
115 Dictionary ndic = new Dictionary();
116 foreach (var dsentry in dicdss)
117 {
118 formatter.Serialize(ms, dsentry.Value);//把DataSet对象序列化到内存流
119 byte[] buffer = ms.ToArray();//把内存流对象写入字节数组
120 ndic.Add(dsentry.Key, buffer);
121 //清空流
122 ms.SetLength(0); ms.Position = 0;
123 }
124 nlistdic.Add(ndic);
125 }
126 ms.Close();//关闭内存流对象
127 ms.Dispose();//释放资源
128 _redisClient.Set(key, nlistdic, expiryTime);
129 }
130 ///
131 /// 得到Dictionary【string, DataSet】
132 ///
133 ///
134 ///
135 public static List GetListDicDataSets(string key)
136 {
137 List resutl = new List();
138 List rebytes = Get(key);
139 foreach (var item in rebytes) {
140
141 Dictionary dss = new Dictionary();
142 foreach (var itementry in item)
143 {
144 DataSet ds = (DataSet)GetObjFromBytes(itementry.Value);
145 dss.Add(itementry.Key, ds);
146 }
147 resutl.Add(dss);
148 }
149 return resutl;
150 }
|
|
|