In the second blog post series “Redis for .NET Developer” I will show how we will use C# to connect to Redis.
If you want to learn how to install Redis, visit my previous post on Intro to Redis for .NET Developers – Installing Redis on Windows.
We will be using StackExchange.Redis library to connect to Redis. One can download StackExchange. Redis through NuGet.
For .NET Framework
1. Let’s Download our Nuget package, you can use the command line like below or use Visual Studio (I am using VS2018).
|
1 |
PM> Install-Package StackExchange.Redis.StrongName |
Search for redis in your nuget window.
Once installed you will see in your output windows.
Now that the nuget package is installed, we can build a C# console app that will connect to your redis server.
Below is a sample code to connect to localhost of your redis.
|
1 2 3 4 5 6 7 8 9 10 |
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost"); IDatabase db = redis.GetDatabase(); if(db.StringSet("testKey", "testValue")) { var val = db.StringGet("testKey"); Console.WriteLine(val); } |
The above code will allow you to connect to Redis and store a string key “testKey” with a value of “testValue”.
For better modulation of Redis it is recommended to store ConnectionMultiplexer as a static singleton in your application.
Below is example of a RedisStore that stores the ConnectionMultiplexer as a static Lazy loaded singleton.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class RedisStore { private static readonly Lazy<ConnectionMultiplexer> LazyConnection; static RedisStore() { var configurationOptions = new ConfigurationOptions { EndPoints = { "localhost" } }; LazyConnection = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(configurationOptions)); } public static ConnectionMultiplexer Connection => LazyConnection.Value; public static IDatabase RedisCache => Connection.GetDatabase(); } |
Now our previous code would something like
|
1 2 3 4 5 6 7 8 |
var redis = RedisStore.RedisCache; if(redis.StringSet("testKey", "testValue")) { var val = redis.StringGet("testKey"); Console.WriteLine(val); } |
Dotnet Core Version for Connection
Lets go through the process of creating a dotnet core console application.
|
1 2 |
$mkdir RedisConnectionDotNetCore $dotnet new console |
We will now need to install all the packages that we require to run dotnetcore with Redis.
|
1 2 3 4 |
$dotnet add package Microsoft.Extensions.Configuration $dotnet add package Microsoft.Extensions.Configuration.FileExtensions $dotnet add package Microsoft.Extensions.Configuration.Json $dotnet add package StackExchange.Redis --version 2.0.519 |
We will create an appsettings.json file to store our connection information, that is one of the reason we have installed all those packages into our project.
This is how my appsettings.json looks like, with only one key and value.
|
1 2 3 |
{ "redis.connection" :"localhost:32768" } |
We also need to tell our csproj about the output of the json file by modifying our RedisConnectionDotNetCore.csproj, we will add into our ItemGroup
|
1 2 3 4 5 6 7 8 9 |
<ItemGroup> <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" /> <PackageReference Include="StackExchange.Redis" Version="2.0.519" /> <None Update="appsettings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> </ItemGroup> |
Lets modify our code in RedisStore.cs such that it can read from the json file, as you can see most of the code remains the same except for where we pick our configuration from.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class RedisStore { private static readonly Lazy<ConnectionMultiplexer> LazyConnection; static RedisStore() { var builder = new ConfigurationBuilder() .SetBasePath(System.IO.Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); IConfiguration config = new ConfigurationBuilder() .AddJsonFile("appsettings.json", true, true) .Build(); var configurationOptions = new ConfigurationOptions { EndPoints = { config["redis.connection"] } }; LazyConnection = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(configurationOptions)); } public static ConnectionMultiplexer Connection => LazyConnection.Value; public static IDatabase RedisCache => Connection.GetDatabase(); } |
Our Program.cs remains the same
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Program { static void Main(string[] args) { var redis = RedisStore.RedisCache; if (redis.StringSet("testKey", "testValue")) { var val = redis.StringGet("testKey"); Console.WriteLine(val); } Console.ReadKey(); } } |
Now we can run with dotnet command line
|
1 2 3 |
$dotnet restore $dotnet run testValue //output vaule |
In my next blog post I will cover the data structures that Redis provides.
For code please visit https://github.com/taswar/RedisForNetDevelopers
For previous Redis topics
































