.NET and Redis
Getting Started
The .NET Community has built many client libraries to help handle requests to Redis Server. In this guide, we'll mostly be concerned with using the StackExchange.Redis client library. As the name implies the StackExchange client is developed by StackExchange for use on popular websites like StackOverflow.
Step 1. Install the Package
There are a few ways to Install the Package:
- .NET CLI
- PM Console
- Package Reference
- NuGet GUI
Run the following in the directory of the csproj
file you want to add the package too.
dotnet add package StackExchange.Redis
Run the following command from the Package Manager Console
Install-Package StackExchange.Redis
You can also add the package directly to you csproj
file with the following XML:
<ItemGroup>
<PackageReference Include="StackExchange.Redis" Version="2.2.4" />
</ItemGroup>
If you're using Visual Studio, and you want to use the NuGet GUI just follow the steps outlined by Microsoft, and make sure to search for StackExchange.Redis
Step 2. Import the Required Namespace
using StackExchange.Redis;
Step 3. Initialize the ConnectionMultiplexer
The ConnectionMultiplexer is the main arbiter of the connection to Redis inside the CLR, your application should maintain a single instance of the ConnectionMultiplexer throughout its runtime. You can initialize the Multiplexer with either a connection string, or with a ConfigurationOptions
object. A typical connection string is of the form: HOST_NAME:PORT_NUMBER,password=PASSWORD
where HOST_NAME
is the host name of your server (e.g. localhost
), PORT_NUMBER
is the port number Redis is listening on (e.g. 6379
) and PASSWORD
is your redis server's password (e.g. secret_password
).
static readonly ConnectionMultiplexer _redis = ConnectionMultiplexer.Connect($"{HOST_NAME}:{PORT_NUMBER},password={PASSWORD}");
Step 4. Grab Database Connection
Once we have a handle for the Multiplexer, we need get a connection to the database.
var db = _redis.GetDatabase();
Step 5. Use the connection
Now that you've retreived the connection to the database, all that's left is to use it. Here are some simple operations:
- Ping the Database
- Set and Get String
- List Operations
- Set Operations
- Hash Operations
db.Ping();
Insert Into List
db.ListLeftPush("simple-list", 1);
Pop Out of List
var result = db.ListRightPop("simple-list");
Console.WriteLine(result);
Insert Into Set
db.SetAdd("sample-set-1", new RedisValue[]{"apple", "banana", "tangerine", "kiwi"});
db.SetAdd("sample-set-2", new RedisValue[]{"apple", "banana", "orange", "blueberries"});
Set Union
var union = db.SetCombine(SetOperation.Union, "sample-set-1", "sample-set-2");
Console.WriteLine(String.Join(", ", union));
Set Intersection
var intersection = db.SetCombine(SetOperation.Intersect, "sample-set-1", "sample-set-2");
Console.WriteLine(String.Join(", ", intersection));
Set Difference
var difference = db.SetCombine(SetOperation.Difference, "sample-set-1", "sample-set-2");
Console.WriteLine(String.Join(", ",difference));
Add to Hash
db.HashSet("person:1", new HashEntry[]{new HashEntry("first-name","John"), new HashEntry("last-name","Smith")});
Get Field From Hash
var firstName = db.HashGet("person:1", "first-name");
Console.WriteLine(firstName);
Get All Fields From Hash
var allFields = db.HashGetAll("person:1");
Console.WriteLine(string.Join(", ",allFields));
Redis Launchpad
Redis Launchpad is like an “App Store” for Redis sample apps. You can easily find apps for your preferred frameworks and languages. Check out a few of these apps below, or click here to access the complete list.