找传奇、传世资源到传世资源站!

C# Redis存取数据(入门级示例)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

需修改配置后才能使用,否则会报错

using StackExchange.Redis;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;using RedisDemo6.model;using System.Diagnostics;namespace RedisDemo6{ class Program { static IDatabase database; static ConnectionMultiplexer conn; static void Main(string[] args) { RedisClient redis = new RedisClient(); Stopwatch sw = new Stopwatch(); var rand = new Random(); string key = "testhash"; var list = new List<TOrder>() ; ConfigurationOptions option = new ConfigurationOptions(); option.EndPoints.Add("127.0.0.1", 7004); conn = ConnectionMultiplexer.Connect(option); database = conn.GetDatabase(); GC.Collect(); for (var i = 0; i < 50000; i ) { var order = new TOrder() { OrderCode = "CGD" rand.Next(1, 999999999), OrderName = "123", CreateTime = DateTime.Now }; list.Add(order); //TestSetHashAsync(key, order); //redis.HashSet<TOrder>(key, order.OrderCode, order); } sw.Start(); //redis.HashSet(key,"TOrder",list); var item = redis.StringGet<string>("testkey"); Console.WriteLine(item); //redis.StringSet<List<TOrder>>(key, list); //List<TOrder> list2 = redis.StringGet<List<TOrder>>(key); //Console.WriteLine(list2.FirstOrDefault()); //Parallel.For(0, 300000, (i) => //{ // var order = new TOrder() // { // OrderCode = "CGD" DateTime.Now.ToString("yyMMdd") rand.Next(1, 999999999), // OrderName = "123", // CreateTime = DateTime.Now // }; // redis.HashSet(key, order.OrderCode, order); //}); //List<TOrder> order = redis.HashGetAllValues<TOrder>(key); //List<TOrder> torder = redis.HashScan<TOrder>(key, "*6666", 10000); //for(var i = 0; i < torder.Count;i ) //{ // Console.WriteLine(torder[i].OrderCode); //} sw.Stop(); GC.Collect(); //sw.Restart(); //Parallel.For(0, 500000, async (i) => { // var order = new TOrder() // { // OrderCode = "CGD" DateTime.Now.ToString("yyMMdd") rand.Next(1, 999999999), // OrderName = "123", // CreateTime = DateTime.Now // }; // await redis.HashSetAsync(key, order.OrderCode, order); //}); //sw.Stop(); Console.WriteLine("存入完成" sw.ElapsedMilliseconds); Console.ReadKey(); //TestGetHashAsync(key); //List<TOrder> hget = redis.HashGetAllValues<TOrder>(key); //hget.Where(a => a.OrderCode.Contains("66666")).ToList().ForEach(a => //{ // Console.WriteLine(a.OrderCode " " a.OrderName " " a.CreateTime); //}); //string[] order = redis.HashGetAllKeys(key); //Console.WriteLine("取出完成"); //Console.ReadKey(); //try //{ // TestGetHashAsync(key); //} //catch (Exception ex) //{ // Console.WriteLine(ex); // Console.ReadKey(); //} //hget.Take(10).Skip(1).ToList().ForEach(a => //{ // Console.WriteLine(a); //}); //while (true) //{ // string val = "gavin_" rand.Next(1, 999999).ToString(); // //TestWriteRead(val); // Thread.Sleep(1000); //} } private static async Task TestSetHashAsync(string key, TOrder order) { var redis = new RedisClient(); await redis.HashSetAsync(key,order.OrderCode,order); } private static async Task TestGetHashAsync(string key) { var redis = new RedisClient(); List<TOrder> hget = redis.HashGetAllValues<TOrder>(key); hget.Where(a => a.OrderCode.Contains("66666")).ToList().ForEach(a => { Console.WriteLine(a.OrderCode " " a.OrderName " " a.CreateTime); }); } //private static void TestWriteRead(string value) //{ // string key = "testkey"; // try // { // database.StringSet(key, value); // Console.WriteLine($"写入{key}={value}成功"); // } // catch // { // Thread.Sleep(3000); // Console.WriteLine($"读取{key}={database.StringGet(key)}成功"); // } // var a = database.StringGet(key); // Console.WriteLine($"读取{key}={a}成功"); //} }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复