惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Palo Alto Networks Blog
D
Docker
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Schneier on Security
Engineering at Meta
Engineering at Meta
I
InfoQ
L
LangChain Blog
Cyberwarzone
Cyberwarzone
T
Tenable Blog
WordPress大学
WordPress大学
P
Privacy & Cybersecurity Law Blog
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Jina AI
Jina AI
C
CERT Recently Published Vulnerability Notes
Scott Helme
Scott Helme
博客园 - 三生石上(FineUI控件)
酷 壳 – CoolShell
酷 壳 – CoolShell
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Last Watchdog
The Last Watchdog
Last Week in AI
Last Week in AI
Cloudbric
Cloudbric
S
SegmentFault 最新的问题
爱范儿
爱范儿
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 叶小钗
AI
AI
T
Tor Project blog
I
Intezer
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
N
News and Events Feed by Topic
Latest news
Latest news
S
Security Affairs
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog RSS Feed
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
S
Securelist

博客园 - FreeBird

研发过程度量平台资料整理 confluence使用经验总结 【转】用chrome滚动截屏 (转)六大因素决定供应链金融互联网平台的竞争力 (转)抢跑2018!星辰亿链区块链金融供应链项目率先落地 (转)2018实战型供应链金融研修班第一站:走进传化集团、川山甲供应链,探讨智慧供应链金融创新 (转)2017年12月宋华教授携IBM中国研究院、猪八戒网、中航信托、33复杂美共同论道智慧供应链金融 物联网+知识储备 淘客相关知识 京东相关知识 Redmine开源项目管理搭建 (转)spring hibernate properties详解 Redis学习第八课:Redis高级实用特性(二) Redis学习第八课:Redis高级实用特性(一) Redis学习第七课:键值命令和服务器命令 Redis学习第六课:Redis ZSet类型及操作 Redis学习第五课:Redis Set类型及操作 Redis学习第四课:Redis List类型及操作 (error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk.
Redis在.net中的应用学习
FreeBird · 2015-06-19 · via 博客园 - FreeBird

在Redis的官网(http://redis.io/clients#c)上可以看到支持Redis C#的客户端。

redis的网络连接方式和传统的rdbms相似,一种是长连接,一种是连接池,此处使用长连接进行连接。

目前redis官方版本不支持.net直接进行连接,需要使用一些开源类库。目前最流行的就是ServiceStack.redis,可以通过https://github.com/ServiceStack/ServiceStack.Redis下载最新版本。

下载完成解压,在\ServiceStack.Redis-master\build\release\MonoDevelop目录下看到ServiceStack.Redis.zip文件,这个就是需要引入到.net项目中的4个dll文件。

测试时发现四个文件版本比较旧,可以通过编译redis源码,生成最新的dll文件。

打开VS2013,创建一个控制台应用程序,写了一些简单的Redis操作

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 using ServiceStack.Redis;
 8 using ServiceStack.Redis.Support;
 9 
10 namespace RedisDemo
11 {
12     class Program
13     {
14         private static RedisClient redis = new RedisClient("192.168.32.216", 6379, "anny");
15         static void Main(string[] args)
16         {
17             //单个字符串写入
18             redis.SetValue("age", "20");
19             //读取指定key的字符串
20             redis.GetValue("age");
21 
22             //存储数字
23             redis.Set<int>("int_age", 30);
24             int age = redis.Get<int>("int_age");
25             Console.WriteLine("int_age={0}", age);
26     
27             //将字符串列表写入Redis List
28             List<string> colourList = new List<string>{"red","pink","green","blue","black","white"};
29             colourList.ForEach(item => redis.AddItemToList("colourList", item));
30 
31             //读取Redis List内容
32             List<string> colourList1 = redis.GetAllItemsFromList("colourList");
33             colourList1.ForEach(item => Console.Write(item + " "));
34 
35             //存储实体对象,在Redis中以json格式存储
36             UserInfo user = new UserInfo(){Id=1, Name="Mark", Age=32, City="ShangHai" };
37             redis.Set<UserInfo>("user_1", user);
38             UserInfo user1 = redis.Get<UserInfo>("user_1");
39             Console.WriteLine("id={0},name={1},age={2},city={3}", user1.Id, user1.Name, user1.Age, user1.City);
40 
41             //object序列化方式
42             var ser = new ObjectSerializer();
43             redis.Set<byte[]>("user1", ser.Serialize(user));
44             UserInfo user11 = ser.Deserialize(redis.Get<byte[]>("user1")) as UserInfo;
45             Console.WriteLine("id={0},name={1},age={2},city={3}", user1.Id, user1.Name, user1.Age, user1.City);
46 
47             //存储对象列表到redis中
48             List<UserInfo> userList = new List<UserInfo>{
49                 new UserInfo{Id=2, Name="Jack", Age=27, City="beijing" },
50                 new UserInfo{Id=3, Name="Tom", Age=25, City="XiaMen" }
51             };
52 
53             redis.Set<byte[]>("userlist", ser.Serialize(userList));
54             List<UserInfo> userList1 = ser.Deserialize(redis.Get<byte[]>("userlist")) as List<UserInfo>;
55             userList1.ForEach(i =>
56             {
57                 Console.WriteLine("id={0},name={1},age={2},city={3}", i.Id, i.Name, i.Age, i.City);
58             });
59 
60             
61             Console.Read();
62         }
63 
64         [Serializable]
65         class UserInfo
66         {
67             public int Id { get; set; }
68             public string Name { get; set; }
69             public int Age { get; set; }
70 
71             public string City { get; set; }
72         }
73     }
74 }

 

Redis linux环境下查看:

root@ubuntu:/usr/local/redis/bin# ./redis-cli -a anny
127.0.0.1:6379> keys *
1) "age"
2) "colourList"
3) "int_age"
4) "user1"
5) "userlist"
6) "user_1"
127.0.0.1:6379> type colourList
list
127.0.0.1:6379> lrange colourList 0 -1
1) "red"
2) "pink"
3) "green"
4) "blue"
5) "black"
6) "white"
127.0.0.1:6379> type userlist
string
127.0.0.1:6379> get userlist
"\x00\x01\x00\x00\x00\xff\xff\xff\xff\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x02\x00\x00\x00@RedisDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\x04\x01\x00\x00\x00\x81\x01System.Collections.Generic.List`1[[RedisDemo.Program+UserInfo, RedisDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]\x03\x00\x00\x00\x06_items\x05_size\b_version\x04\x00\x00\x1cRedisDemo.Program+UserInfo[]\x02\x00\x00\x00\b\b\t\x03\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\a\x03\x00\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x04\x1aRedisDemo.Program+UserInfo\x02\x00\x00\x00\t\x04\x00\x00\x00\t\x05\x00\x00\x00\r\x02\x05\x04\x00\x00\x00\x1aRedisDemo.Program+UserInfo\x04\x00\x00\x00\x13<Id>k__BackingField\x15<Name>k__BackingField\x14<Age>k__BackingField\x15<City>k__BackingField\x00\x01\x00\x01\b\b\x02\x00\x00\x00\x02\x00\x00\x00\x06\x06\x00\x00\x00\x04Jack\x1b\x00\x00\x00\x06\a\x00\x00\x00\abeijing\x01\x05\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x06\b\x00\x00\x00\x03Tom\x19\x00\x00\x00\x06\t\x00\x00\x00\x06XiaMen\x0b"

127.0.0.1:6379> type user_1
string
127.0.0.1:6379> get user_1
"{\"Id\":1,\"Name\":\"Mark\",\"Age\":32,\"City\":\"ShangHai\"}"