diff --git a/7.RedisSortedSets/README b/7.RedisSortedSets/README new file mode 100644 index 0000000..940ea2a --- /dev/null +++ b/7.RedisSortedSets/README @@ -0,0 +1,4 @@ +Source Code for Blog Post on Redis for .NET Developers +- Describes Set datatype in Redis with C# using a console app. +- To read the content please visit +http://taswar.zeytinsoft.com/2016/12/29/redis-sorted-sets-datatype/ diff --git a/7.RedisSortedSets/RedisSortedSets.sln b/7.RedisSortedSets/RedisSortedSets.sln new file mode 100644 index 0000000..985ff22 --- /dev/null +++ b/7.RedisSortedSets/RedisSortedSets.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RedisSortedSets", "RedisSortedSets\RedisSortedSets.csproj", "{5CE3A981-5449-4B8A-9B60-65CFA987084A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5CE3A981-5449-4B8A-9B60-65CFA987084A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5CE3A981-5449-4B8A-9B60-65CFA987084A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5CE3A981-5449-4B8A-9B60-65CFA987084A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5CE3A981-5449-4B8A-9B60-65CFA987084A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/7.RedisSortedSets/RedisSortedSets/App.config b/7.RedisSortedSets/RedisSortedSets/App.config new file mode 100644 index 0000000..7dcb8b9 --- /dev/null +++ b/7.RedisSortedSets/RedisSortedSets/App.config @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/7.RedisSortedSets/RedisSortedSets/Program.cs b/7.RedisSortedSets/RedisSortedSets/Program.cs new file mode 100644 index 0000000..4ed56e8 --- /dev/null +++ b/7.RedisSortedSets/RedisSortedSets/Program.cs @@ -0,0 +1,189 @@ +using StackExchange.Redis; +using System; + +namespace RedisSortedSets +{ + class Program + { + static void Main(string[] args) + { + var redis = RedisStore.RedisCache; + + RedisKey topHackerKeys = "hackers"; + RedisKey alphaKey = "alphaKey"; + RedisKey destinationKey = "destKey"; + RedisKey intersectKey = "intersectKey"; + + + redis.KeyDelete(topHackerKeys, CommandFlags.FireAndForget); + redis.KeyDelete(alphaKey, CommandFlags.FireAndForget); + redis.KeyDelete(intersectKey, CommandFlags.FireAndForget); + redis.KeyDelete(destinationKey, CommandFlags.FireAndForget); + + //According to http://www.arkhitech.com/12-greatest-programmers-of-all-time/ + var topProgrammers = new[] { + "Dennis Ritchie", + "Linus Torvalds", + "Bjarne Stroustrup", + "Tim Berners-Lee", + "Brian Kernighan", + "Donald Knuth", + "Ken Thompson", + "Guido van Rossum", + "James Gosling", + "Bill Gates", + "Niklaus Wirth", + "Ada Lovelace" + }; + + //add 12 items to the sorted set + for (int i = 0, j = 1; i < topProgrammers.Length; i++, j++) + redis.SortedSetAdd(topHackerKeys, topProgrammers[i], j); + + var members = redis.SortedSetScan(topHackerKeys); + + Console.WriteLine(string.Join(",\n", members)); + /* output + * Dennis Ritchie: 1, + * Linus Torvalds: 2, + * Bjarne Stroustrup: 3, + * Tim Berners-Lee: 4, + * Brian Kernighan: 5, + * Donald Knuth: 6, + * Ken Thompson: 7, + * Guido van Rossum: 8, + * James Gosling: 9, + * Bill Gates: 10, + * Niklaus Wirth: 11, + * Ada Lovelace: 12 + */ + + + Console.WriteLine(redis.SortedSetLength(topHackerKeys)); //output 12 + + var byRanks = redis.SortedSetRangeByRank(topHackerKeys); + Console.WriteLine(string.Join(",\n", byRanks)); + /* output + * Dennis Ritchie, + * Linus Torvalds, + * Bjarne Stroustrup, + * Tim Berners-Lee, + * Brian Kernighan, + * Donald Knuth, + * Ken Thompson, + * Guido van Rossum, + * James Gosling, + * Bill Gates, + * Niklaus Wirth, + * Ada Lovelace + */ + + + Console.WriteLine(redis.SortedSetRank(topHackerKeys, "Linus Torvalds")); //output 1 + + + var byScore = redis.SortedSetRangeByScore(topHackerKeys, 1, topProgrammers.Length,Exclude.None, Order.Descending); + Console.WriteLine(string.Join(",\n", byScore)); + /*output + * Ada Lovelace, + * Niklaus Wirth, + * Bill Gates, + * James Gosling, + * Guido van Rossum, + * Ken Thompson, + * Donald Knuth, + * Brian Kernighan, + * Tim Berners-Lee, + * Bjarne Stroustrup, + * Linus Torvalds, + * Dennis Ritchie + */ + + redis.SortedSetIncrement(topHackerKeys, "Linus Torvalds", 100); + + Console.WriteLine(redis.SortedSetScore(topHackerKeys, "Linus Torvalds")); //output 102, since it was 2 to being with + + redis.SortedSetDecrement(topHackerKeys, "Linus Torvalds", 100); + + Console.WriteLine(redis.SortedSetScore(topHackerKeys, "Linus Torvalds")); //output 2 back to original value + + redis.SortedSetAdd(alphaKey, "a", 1); + redis.SortedSetAdd(alphaKey, "b", 1); + redis.SortedSetAdd(alphaKey, "c", 1); + + redis.SortedSetCombineAndStore(SetOperation.Union, destinationKey, topHackerKeys, alphaKey); + + members = redis.SortedSetScan(destinationKey); + Console.WriteLine("**********UNION**************"); + Console.WriteLine(string.Join(",\n", members)); + /* output + * Dennis Ritchie: 1, + * a: 1, + * b: 1, + * c: 1, + * Linus Torvalds: 2, + * Bjarne Stroustrup: 3, + * Tim Berners-Lee: 4, + * Brian Kernighan: 5, + * Donald Knuth: 6, + * Ken Thompson: 7, + * Guido van Rossum: 8, + * James Gosling: 9, + * Bill Gates: 10, + * Niklaus Wirth: 11, + * Ada Lovelace: 12 + */ + + redis.SortedSetCombineAndStore(SetOperation.Intersect, intersectKey, topHackerKeys, destinationKey); + members = redis.SortedSetScan(intersectKey); + + //note it double the key scores + Console.WriteLine("**********INTERSECT**************"); + Console.WriteLine(string.Join(",\n", members)); + /*output + * Dennis Ritchie: 2, + * Linus Torvalds: 4, + * Bjarne Stroustrup: 6, + * Tim Berners-Lee: 8, + * Brian Kernighan: 10, + * Donald Knuth: 12, + * Ken Thompson: 14, + * Guido van Rossum: 16, + * James Gosling: 18, + * Bill Gates: 20, + * Niklaus Wirth: 22, + * Ada Lovelace: 24 + */ + + members = redis.SortedSetRangeByScoreWithScores(topHackerKeys, 2, 4); + Console.WriteLine("**********RANGE BY SCORE WITH SCORES**************"); + Console.WriteLine(string.Join(",\n", members)); + /*output + * Linus Torvalds: 2, + * Bjarne Stroustrup: 3, + * Tim Berners-Lee: 4 + */ + + + redis.SortedSetRemove(alphaKey, "a"); + members = redis.SortedSetScan(alphaKey); + Console.WriteLine("**********REMOVE**************"); + Console.WriteLine(string.Join(",\n", members)); + /*output + * b: 1, + * c: 1 + */ + + redis.SortedSetRemoveRangeByScore(destinationKey, 0, 11); + members = redis.SortedSetScan(destinationKey); + Console.WriteLine("**********REMOVE BY SCORE**************"); + Console.WriteLine(string.Join(",\n", members)); + /* output + * Ada Lovelace: 12 + */ + + + Console.ReadKey(); + } + } +} diff --git a/7.RedisSortedSets/RedisSortedSets/Properties/AssemblyInfo.cs b/7.RedisSortedSets/RedisSortedSets/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..2984417 --- /dev/null +++ b/7.RedisSortedSets/RedisSortedSets/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("RedisSortedSets")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("RedisSortedSets")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("5ce3a981-5449-4b8a-9b60-65cfa987084a")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/7.RedisSortedSets/RedisSortedSets/RedisSortedSets.csproj b/7.RedisSortedSets/RedisSortedSets/RedisSortedSets.csproj new file mode 100644 index 0000000..f555202 --- /dev/null +++ b/7.RedisSortedSets/RedisSortedSets/RedisSortedSets.csproj @@ -0,0 +1,68 @@ + + + + + Debug + AnyCPU + {5CE3A981-5449-4B8A-9B60-65CFA987084A} + Exe + Properties + RedisSortedSets + RedisSortedSets + v4.6.1 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\StackExchange.Redis.1.1.608\lib\net46\StackExchange.Redis.dll + True + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/7.RedisSortedSets/RedisSortedSets/RedisStore.cs b/7.RedisSortedSets/RedisSortedSets/RedisStore.cs new file mode 100644 index 0000000..9ac0981 --- /dev/null +++ b/7.RedisSortedSets/RedisSortedSets/RedisStore.cs @@ -0,0 +1,25 @@ +using StackExchange.Redis; +using System; +using System.Configuration; + +namespace RedisSortedSets +{ + public class RedisStore + { + private static readonly Lazy LazyConnection; + + static RedisStore() + { + var configurationOptions = new ConfigurationOptions + { + EndPoints = { ConfigurationManager.AppSettings["redis.connection"] } + }; + + LazyConnection = new Lazy(() => ConnectionMultiplexer.Connect(configurationOptions)); + } + + public static ConnectionMultiplexer Connection => LazyConnection.Value; + + public static IDatabase RedisCache => Connection.GetDatabase(); + } +} diff --git a/7.RedisSortedSets/RedisSortedSets/packages.config b/7.RedisSortedSets/RedisSortedSets/packages.config new file mode 100644 index 0000000..d4dad6b --- /dev/null +++ b/7.RedisSortedSets/RedisSortedSets/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file