redis保存dataset

公司统一走redis缓存,也将之前的memcache迁移到redis
碰到问题是redis的dataset缓存。

memcache底层封装了dataset的序列化。

而redis引的DLL包,未支持。所以封装一个类,提供dataset的set,get功能。

dataset以转为byte[]保存,读取byte[] 转为dataset

代码如下

  1   /// <summary>
  2         /// 获取缓存(从2进制流)
  3         /// </summary>
  4         /// <param name="key">缓存键</param>
  5         /// <returns></returns>
  6         public static object Get(string key)
  7         {
  8             byte[] buffer = _redisClient.Get(key);
  9             return GetObjFromBytes(buffer);
 10         }
 11 
 12         /// <summary>
 13         /// 从二进制流得到对象(dataset专用,dataset要序列化为二进制才可保存)
 14         /// </summary>
 15         /// <param name="buffer"></param>
 16         /// <returns></returns>
 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         /// <summary>
 28         /// 获取DATASET缓存
 29         /// </summary>
 30         /// <param name="key">缓存键</param>
 31         /// <returns></returns>
 32         public static DataSet GetMemByDataSet(string key)
 33         {
 34             var item = Get(key);
 35             return (DataSet)item;
 36         }
 37         /// <summary>
 38         /// 获取DATASET缓存(如果没有,则调用Func<T>方法返回对象并加入到缓存)
 39         /// </summary>
 40         /// <param name="key">缓存键</param>
 41         /// <param name="func">委托方法,返回指定对象类型,用于缓存不存在时回调该方法获取数据并插入到缓存</param>
 42         /// <param name="minute">过期时间(分钟)</param>
 43         /// <returns></returns>
 44         public static DataSet GetMemByDataSet(string key, Func<DataSet> 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         /// <summary>
 58         /// 插入DATASET缓存
 59         /// </summary>
 60         /// <param name="key">缓存键</param>
 61         /// <param name="item">缓存对象</param>
 62         /// <param name="minute">过期时间(分钟)</param>
 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         /// <summary>
 76         /// 插入dictionary dataset缓存。
 77         /// </summary>
 78         /// <param name="key">redis保存键</param>
 79         /// <param name="dicdss">Dictionary string 键 dataset 值 </param>
 80         /// <param name="minute">缓存时间</param>
 81         public static void SetDicDataSets(string key, Dictionary<string, DataSet> dicdss, int minute)
 82         {
 83             //dataset转为二进制流
 84             Dictionary<string, byte[]> ndic = new Dictionary<string, byte[]>();
 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         /// <summary>
102         /// 插入List. dictionary .dataset缓存。
103         /// </summary>
104         /// <param name="key">redis保存键</param>
105         /// <param name="dicdss">Dictionary string 键 dataset 值 </param>
106         /// <param name="minute">缓存时间</param>
107         public static void SetListDicDataSets(string key,List<Dictionary<string, DataSet>> 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<Dictionary<string, byte[]>>  nlistdic=new List<Dictionary<string,byte[]>>();
112             System.IO.MemoryStream ms = new System.IO.MemoryStream();//创建内存流对象   
113             foreach (var dicdss in Listdicdss) {
114                 //dataset转为二进制流
115                 Dictionary<string, byte[]> ndic = new Dictionary<string, byte[]>();
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         /// <summary>
131         /// 得到Dictionary【string, DataSet】
132         /// </summary>
133         /// <param name="key"></param>
134         /// <returns></returns>
135         public static List<Dictionary<string, DataSet>> GetListDicDataSets(string key)
136         {
137             List<Dictionary<string, DataSet>> resutl = new List<Dictionary<string, DataSet>>();
138             List<Dictionary<string, byte[]>> rebytes = Get<List<Dictionary<string, byte[]>>>(key); 
139             foreach (var item in rebytes) {
140 
141                 Dictionary<string, DataSet> dss = new Dictionary<string, DataSet>();
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         }

 

 

 

posted @ 2014-01-03 11:01  cclient  阅读(1261)  评论(0编辑  收藏  举报