设为首页 收藏本站
查看: 810|回复: 0

[经验分享] redis保存dataset

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-7-21 10:50:10 | 显示全部楼层 |阅读模式
  公司统一走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         }
  
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-88998-1-1.html 上篇帖子: Linux下Redis C++操作的封装 下篇帖子: 九 redis学习笔记之虚拟内存
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表