Redis入门篇
1.简单介绍
Redis是一个开源,高级的键值存储和一个适用的解决方案,用于构建高性能,可扩展的Web应用程序。
Redis有三个主要特点,使它优越于其它键值数据存储系统 -
- Redis将其数据库完全保存在内存中,仅使用磁盘进行持久化。
- 与其它键值数据存储相比,Redis有一组相对丰富的数据类型。
- Redis可以将数据复制到任意数量的从机中。
2.安装redis服务
下载文件Redis-x64-3.2.100.msi
3.常用redis命令
卸载服务:redis-server --service-uninstall
开启服务:redis-server --service-start
停止服务:redis-server --service-stop
4.安装redis可视化工具
redis-desktop-manager-0.8.8.384.exe
5.存取数据
using Newtonsoft.Json; using Qxun.Framework.Contract; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; using System.Xml; namespace Test { [Serializable] public class User { public long Id { get; set; } public string Name { get; set; } public int Age { get; set; } } public class Program { static void Main(string[] args) { ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost:6379,password=6665508a"); 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,Newtonsoft.Json.Formatting.Indented); ////设置dataset1值 //db.StringSet("dataset1", _json); //DataSet ds = (DataSet)JsonConvert.DeserializeObject(_json, typeof(DataSet)); //Console.WriteLine(ds.Tables[0].Rows[0]["item"].ToString()); System.Console.ReadLine(); } } }