1 ///
2 ///获取缓存(从2进制流)3 ///
4 /// 缓存键
5 ///
6 public static object Get(stringkey)7 {8 byte[] buffer =_redisClient.Get(key);9 returnGetObjFromBytes(buffer);10 }11
12 ///
13 ///从二进制流得到对象(dataset专用,dataset要序列化为二进制才可保存)14 ///
15 ///
16 ///
17 private static object GetObjFromBytes(byte[] buffer) {18 using (System.IO.MemoryStream stream = newSystem.IO.MemoryStream(buffer))19 {20 stream.Position = 0;21 System.Runtime.Serialization.IFormatter bf = newSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter();22 Object reobj =bf.Deserialize(stream);23 returnreobj;24 }25 }26
27 ///
28 ///获取DATASET缓存29 ///
30 /// 缓存键
31 ///
32 public static DataSet GetMemByDataSet(stringkey)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, intminute)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 returnitem;56 }57 ///
58 ///插入DATASET缓存59 ///
60 /// 缓存键
61 /// 缓存对象
62 /// 过期时间(分钟)
63 public static void SetMemByDataSet(string key, DataSet ds, intminute)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, intminute)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 indicdss)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, intminute)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 inListdicdss) {114 //dataset转为二进制流
115 Dictionary ndic = new Dictionary();116 foreach (var dsentry indicdss)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(stringkey)136 {137 List> resutl = new List>();138 List> rebytes = Get>>(key);139 foreach (var item inrebytes) {140
141 Dictionary dss = new Dictionary();142 foreach (var itementry initem)143 {144 DataSet ds =(DataSet)GetObjFromBytes(itementry.Value);145 dss.Add(itementry.Key, ds);146 }147 resutl.Add(dss);148 }149 returnresutl;150 }