Created
May 5, 2017 18:03
-
-
Save hamsteven/cc0082aeb6ddfa2915a29259302ddf73 to your computer and use it in GitHub Desktop.
C# ServiceStack Redis Pipelined MGET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using ServiceStack; | |
| using ServiceStack.Text; | |
| using ServiceStack.Redis; | |
| using ServiceStack.DataAnnotations; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| var redisManager = new RedisManagerPool("localhost:6379"); | |
| var redis = redisManager.GetClient(); | |
| const int REDIS_SIZE = 5000; | |
| const int BATCH_SIZE = 1000; | |
| /*public static class ListExtensions | |
| { | |
| public static List<List<T>> ChunkBy<T>(this List<T> source, int chunkSize) | |
| { | |
| return source | |
| .Select((x, i) => new { Index = i, Value = x }) | |
| .GroupBy(x => x.Index / chunkSize) | |
| .Select(x => x.Select(v => v.Value).ToList()) | |
| .ToList(); | |
| } | |
| }*/ | |
| void repopulateRedis(int num) | |
| { | |
| redis.FlushDb(); | |
| for (int i = 0; i<num; i++) | |
| { | |
| redis.SetEntry("pHwolAYzEY1DUFDatapMblvR4Ve8IuSHYx9xdoUN/QJE9Ngwv+uCplcrQTVIYv2k2KwzTIMd"+i, "114:10, 116:20, 117:10, 119:30"); | |
| } | |
| } | |
| var myList = redis.GetAllKeys(); | |
| var chunkedList = new List<List<string>>(); | |
| while(myList.Any()) | |
| { | |
| chunkedList.Add(myList.Take(BATCH_SIZE).ToList()); | |
| myList= myList.Skip(BATCH_SIZE).ToList(); | |
| } | |
| repopulateRedis(REDIS_SIZE); | |
| Console.WriteLine(redis.DbSize); | |
| var resList = new List<string>(); | |
| using (var pipe = redis.CreatePipeline()) | |
| { | |
| foreach (var l in chunkedList) { | |
| pipe.QueueCommand(r => r.GetValues(l), x => resList.AddRange(x)); | |
| } | |
| Console.WriteLine("got here"); | |
| pipe.Flush(); | |
| } | |
| Console.WriteLine(String.Join("; ", resList)); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <packages> | |
| <package id="ServiceStack.Text" version="4.5.8" targetFramework="net45" /> | |
| <package id="ServiceStack.Interfaces" version="4.5.8" targetFramework="net45" /> | |
| <package id="ServiceStack.Common" version="4.5.8" targetFramework="net45" /> | |
| <package id="ServiceStack.Redis" version="4.5.8" targetFramework="net45" /> | |
| </packages> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment