static void Test1(RedisConnection redisConnection) { try { var db = redisConnection.GetDatabase(); db.PingAsync().Wait(); var list = new RedisList <string>(db, "test"); list.Clear(); list.Add("one"); list.Add("two"); list.Add("three"); list[1] = "Ross"; var bData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; db.StringSet("some_data", bData); //, TimeSpan.FromSeconds(20)); foreach (var str in list) { Console.WriteLine(str); } } catch (Exception ex) { Console.WriteLine(ex); } }
static void Test4(RedisConnection redisConnection) { var mpSerializer = new MsgPackRedisSerializer(); try { var db = redisConnection.GetDatabase(); db.PingAsync().Wait(); var list = new RedisList <string>(db, "test", mpSerializer); list.Clear(); list.Add("one"); list.Add("two"); list.Add("three"); list[1] = "Ross"; foreach (var str in list) { Console.WriteLine(str); } } catch (Exception ex) { Console.WriteLine(ex); } }
static void Main(string[] args) { // Create connectionMultiplexer. Creating connectionMultiplexer is costly so it is recommended to store and reuse it. var connectionMultiplexer = ConnectionMultiplexer.Connect("localhost,abortConnect=false"); // replace localhost with your redis db address // You can either create a RedisType by using RedisTypeFactory or by instantiating the desired type directly. var redisTypeFactory = new RedisTypeFactory(connectionMultiplexer); // Create a redis dictionary under the name of "Person". var redisDictionary = redisTypeFactory.GetDictionary <int, Person>("Person"); // Adding items to dictionary redisDictionary.Add(1, new Person { ID = 1, Name = "Steve", Age = 20 }); redisDictionary.Add(2, new Person { ID = 2, Name = "Mike", Age = 25 }); redisDictionary.Add(3, new Person { ID = 3, Name = "Lara", Age = 30 }); // Iterate through dictionary foreach (var person in redisDictionary) { Console.WriteLine("ID: {0}, Name: {1}, Age: {2}", person.Value.ID, person.Value.Name, person.Value.Age); } Console.WriteLine(); // Find a specific person var lara = redisDictionary.First(kvp => kvp.Value.Name == "Lara"); Console.WriteLine("Lara's Age: {0}", lara.Value.Age); // Delete a person redisDictionary.Remove(lara.Key); // Delete the Person dictionary from redis redisDictionary.Clear(); // Creating a redis list var redisList = new RedisList <int>(connectionMultiplexer.GetDatabase(), "Numbers"); // Adding some numbers to redis list for (int i = 0; i < 10; i++) { redisList.Add(i); } Console.WriteLine(); Console.WriteLine("List Members:"); // Iterating through list foreach (var number in redisList) { Console.WriteLine(number); } // Delete the Numbers list from redis redisList.Clear(); // Using a DI container... var container = new UnityContainer(); // Register connectionMultiplexer as a singleton instance container.RegisterInstance <IConnectionMultiplexer>(connectionMultiplexer); // Register RedisTypeFactory container.RegisterType <IRedisTypeFactory, RedisTypeFactory>(); // Resolve an IRedisTypeFacoty var factory = container.Resolve <IRedisTypeFactory>(); // Get a redis set from factory var redisSet = factory.GetSet <int>("NumbersSet"); redisSet.Add(1); redisSet.Add(1); redisSet.Add(2); Console.WriteLine(); Console.WriteLine("Set Members:"); // Iterating through set foreach (var item in redisSet) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine("Press any key to exit..."); Console.Read(); }
static void SerializerTests(RedisConnection redisConnection) { try { var json = File.ReadAllText($"{AppDomain.CurrentDomain.BaseDirectory}sample.json"); var myData = new MySerializeData { Data1 = json, Data2 = json, List = new List <string> { json, json, json, json } }; var myData2 = new MySerializeData2 { Data = json, List = new List <TestClass>(), }; for (var i = 0; i < 10; i++) { myData2.List.Add(new TestClass { Id = i, Name = $"Ross: {i}" }); } var jsonSerializer = new JsonRedisSerializer(); var mpSerializer = new MsgPackRedisSerializer(); //var r1 = jsonSerializer.Serialize(myData2); //var r2 = mpSerializer.Serialize(myData2); //var db = redisConnection.GetDatabase(); //var list = new XRedisList<MySerializeData>(db, "test", mpSerializer); //list.Clear(); //list.Add(myData); //list.Add(myData); //list.Add(myData); const int Iteration = 1000; while (true) { Console.WriteLine("Ready..."); var key = Console.ReadKey(); Console.WriteLine(); if (key.KeyChar == 'q') { break; } else if (key.KeyChar == 'c') { Console.Clear(); } else if (key.KeyChar == 'j') { var db = redisConnection.GetDatabase(); var list = new RedisList <MySerializeData>(db, "test", jsonSerializer); list.Clear(); var sw = new Stopwatch(); sw.Start(); for (var i = 0; i < Iteration; i++) { list.Add(myData); var d = list[0]; } sw.Stop(); Console.WriteLine($"{sw.ElapsedMilliseconds} msec for {Iteration} iterations using Json serializer"); } else if (key.KeyChar == 'm') { var db = redisConnection.GetDatabase(); var list = new RedisList <MySerializeData>(db, "test", mpSerializer); list.Clear(); var sw = new Stopwatch(); sw.Start(); for (var i = 0; i < Iteration; i++) { list.Add(myData); var d = list[0]; } sw.Stop(); Console.WriteLine($"{sw.ElapsedMilliseconds} msec for {Iteration} iterations using MsgPack serializer"); } } } catch (Exception ex) { Console.WriteLine(ex); } }
static void Main(string[] args) { // Create connectionMultiplexer. Creating connectionMultiplexer is costly so it is recommended to store and reuse it. var connectionMultiplexer = ConnectionMultiplexer.Connect("localhost,abortConnect=false"); // replace localhost with your redis db address // You can either create a RedisType by using RedisTypeFactory or by instantiating the desired type directly. var redisTypeFactory = new RedisTypeFactory(connectionMultiplexer); // Create a redis dictionary under the name of "Person". var redisDictionary = redisTypeFactory.GetDictionary<int, Person>("Person"); // Adding items to dictionary redisDictionary.Add(1, new Person { ID = 1, Name = "Steve", Age = 20 }); redisDictionary.Add(2, new Person { ID = 2, Name = "Mike", Age = 25 }); redisDictionary.Add(3, new Person { ID = 3, Name = "Lara", Age = 30 }); // Iterate through dictionary foreach (var person in redisDictionary) { Console.WriteLine("ID: {0}, Name: {1}, Age: {2}", person.Value.ID, person.Value.Name, person.Value.Age); } Console.WriteLine(); // Find a specific person var lara = redisDictionary.First(kvp => kvp.Value.Name == "Lara"); Console.WriteLine("Lara's Age: {0}", lara.Value.Age); // Delete a person redisDictionary.Remove(lara.Key); // Delete the Person dictionary from redis redisDictionary.Clear(); // Creating a redis list var redisList = new RedisList<int>(connectionMultiplexer.GetDatabase(), "Numbers"); // Adding some numbers to redis list for (int i = 0; i < 10; i++) { redisList.Add(i); } Console.WriteLine(); Console.WriteLine("List Members:"); // Iterating through list foreach (var number in redisList) { Console.WriteLine(number); } // Delete the Numbers list from redis redisList.Clear(); // Using a DI container... var container = new UnityContainer(); // Register connectionMultiplexer as a singleton instance container.RegisterInstance<IConnectionMultiplexer>(connectionMultiplexer); // Register RedisTypeFactory container.RegisterType<IRedisTypeFactory, RedisTypeFactory>(); // Resolve an IRedisTypeFacoty var factory = container.Resolve<IRedisTypeFactory>(); // Get a redis set from factory var redisSet = factory.GetSet<int>("NumbersSet"); redisSet.Add(1); redisSet.Add(1); redisSet.Add(2); Console.WriteLine(); Console.WriteLine("Set Members:"); // Iterating through set foreach (var item in redisSet) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine("Press any key to exit..."); Console.Read(); }