.NET平台下Redis使用(二)【StackExchange.Redis学习】
Program.cs内容:using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace stackexchangeredis
{
class Program
{
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost:6379,password=123456");
IDatabase db = redis.GetDatabase();
long count = 10000 * 10;
DateTime start = DateTime.Now;
#region 所有Redis的数据写入方法
db.StringSet("key_test", "shaocan");
db.HashSet("userinfo", "name", "shaocan");
db.SetAdd("set_test", "user1");
db.SetAdd("set_test", "user2");
db.SortedSetAdd("sset_test", "user1", DateTime.Now.Ticks);
db.SortedSetAdd("sset_test", "user2", DateTime.Now.Ticks);
db.ListLeftPush("list_test", "user1");
#endregion
start = DateTime.Now;
/* BinaryFormat */
//二进制格式
for (int i = 0; i < count; i++)
{
User user = new User { Id = i, Name = "YouNameIt" + i , Age = 20};
string key = "myObject" + i;
byte[] bytes;
using (var stream = new MemoryStream())
{
new BinaryFormatter().Serialize(stream, user);
bytes = stream.ToArray();
}
//设置值
//SET键保存字符串值。如果key已经拥有一个值,它被覆盖,而不管其类型。
db.StringSet(key, bytes);
}
//读取10w条数据
for (int i = 0; i < count; i++)
{
string key = "myObject" + i;
User user = null;
byte[] bytes = (byte[])db.StringGet(key);
if (bytes != null)
{
using (var stream = new MemoryStream(bytes))
{
//二进制流,反序列化
user = (User)new BinaryFormatter().Deserialize(stream);
}
}
Console.WriteLine(user.Name);
}
System.Console.WriteLine(string.Format("Binary Format {0} items takes {1} seconds" , count ,(DateTime.Now - start).TotalSeconds));
start = DateTime.Now;
/* 100000 */
for (int i = 0; i < count; i++)
{
User user = new User { Id = i, Name = "极客神殿" + i, Age = 20 };
string json = JsonConvert.SerializeObject(user);
string key = "json" + i;
db.StringSet(key, json);
}
//读取10W条数据
for (int i = 0; i < count; i++)
{
string key = "json" + i;
string json = db.StringGet(key);
User user = (User)JsonConvert.DeserializeObject(json, typeof(User));
Console.WriteLine(user.Name);
}
System.Console.WriteLine(string.Format("JSON Format {0} items takes {1} seconds", count, (DateTime.Now - start).TotalSeconds));
start = DateTime.Now;
//序列化DataSet为JSON。
//http://www.newtonsoft.com/json/help/html/SerializeDataSet.htm
DataSet dataSet = new DataSet("dataSet");
dataSet.Namespace = "NetFrameWork";
DataTable table = new DataTable();
DataColumn idColumn = new DataColumn("id", typeof(int));
idColumn.AutoIncrement = true;
DataColumn itemColumn = new DataColumn("item");
table.Columns.Add(idColumn);
table.Columns.Add(itemColumn);
dataSet.Tables.Add(table);
for (int i = 0; i < 2; i++)
{
DataRow newRow = table.NewRow();
newRow["item"] = "[测试] " + i;
table.Rows.Add(newRow);
}
dataSet.AcceptChanges();
string _json = JsonConvert.SerializeObject(dataSet, Formatting.Indented);
//设置dataset1值
db.StringSet("dataset1", _json);
DataSet ds = (DataSet)JsonConvert.DeserializeObject(_json, typeof(DataSet));
Console.WriteLine(ds.Tables.Rows["item"].ToString());
System.Console.ReadLine();
}
}
public class User
{
public long Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
[*]1
[*]2
[*]3
[*]4
[*]5
[*]6
[*]7
[*]8
[*]9
[*]10
[*]11
[*]12
[*]13
[*]14
[*]15
[*]16
[*]17
[*]18
[*]19
[*]20
[*]21
[*]22
[*]23
[*]24
[*]25
[*]26
[*]27
[*]28
[*]29
[*]30
[*]31
[*]32
[*]33
[*]34
[*]35
[*]36
[*]37
[*]38
[*]39
[*]40
[*]41
[*]42
[*]43
[*]44
[*]45
[*]46
[*]47
[*]48
[*]49
[*]50
[*]51
[*]52
[*]53
[*]54
[*]55
[*]56
[*]57
[*]58
[*]59
[*]60
[*]61
[*]62
[*]63
[*]64
[*]65
[*]66
[*]67
[*]68
[*]69
[*]70
[*]71
[*]72
[*]73
[*]74
[*]75
[*]76
[*]77
[*]78
[*]79
[*]80
[*]81
[*]82
[*]83
[*]84
[*]85
[*]86
[*]87
[*]88
[*]89
[*]90
[*]91
[*]92
[*]93
[*]94
[*]95
[*]96
[*]97
[*]98
[*]99
[*]100
[*]101
[*]102
[*]103
[*]104
[*]105
[*]106
[*]107
[*]108
[*]109
[*]110
[*]111
[*]112
[*]113
[*]114
[*]115
[*]116
[*]117
[*]118
[*]119
[*]120
[*]121
[*]122
[*]123
[*]124
[*]125
[*]126
[*]127
[*]128
[*]129
[*]130
[*]131
[*]132
[*]1
[*]2
[*]3
[*]4
[*]5
[*]6
[*]7
[*]8
[*]9
[*]10
[*]11
[*]12
[*]13
[*]14
[*]15
[*]16
[*]17
[*]18
[*]19
[*]20
[*]21
[*]22
[*]23
[*]24
[*]25
[*]26
[*]27
[*]28
[*]29
[*]30
[*]31
[*]32
[*]33
[*]34
[*]35
[*]36
[*]37
[*]38
[*]39
[*]40
[*]41
[*]42
[*]43
[*]44
[*]45
[*]46
[*]47
[*]48
[*]49
[*]50
[*]51
[*]52
[*]53
[*]54
[*]55
[*]56
[*]57
[*]58
[*]59
[*]60
[*]61
[*]62
[*]63
[*]64
[*]65
[*]66
[*]67
[*]68
[*]69
[*]70
[*]71
[*]72
[*]73
[*]74
[*]75
[*]76
[*]77
[*]78
[*]79
[*]80
[*]81
[*]82
[*]83
[*]84
[*]85
[*]86
[*]87
[*]88
[*]89
[*]90
[*]91
[*]92
[*]93
[*]94
[*]95
[*]96
[*]97
[*]98
[*]99
[*]100
[*]101
[*]102
[*]103
[*]104
[*]105
[*]106
[*]107
[*]108
[*]109
[*]110
[*]111
[*]112
[*]113
[*]114
[*]115
[*]116
[*]117
[*]118
[*]119
[*]120
[*]121
[*]122
[*]123
[*]124
[*]125
[*]126
[*]127
[*]128
[*]129
[*]130
[*]131
[*]132
运行结果如图:
文档来源:51CTO技术博客https://blog.51cto.com/u_11990719/3101721
页:
[1]