Radis DB with .NET Core Code Example

The StackExchange.Redis NuGet package must first be added to your project. You may accomplish this by either adding the package to your project’s dependencies in the.csproj file or using the Visual Studio Package Manager Interface.

To connect to your Redis instance, construct a ConnectionMultiplexer object next:

using StackExchange.Redis;

var redis = ConnectionMultiplexer.Connect("localhost:6379");

After you have a ConnectionMultiplexer, you can utilise the GetDatabase function to access your Redis database:

var database = redis.GetDatabase();

Create Operation:

Use the StringSet function in Redis to generate a fresh key-value pair:

var key = "mykey";
var value = "myvalue";
database.StringSet(key, value);

Read Operation:

The StringGet function may be used to get a value from Redis:

var key = "mykey";
var value = database.StringGet(key);

Update Operation:

You may use the StringSet function once more to change a value in Redis:

var key = "mykey";
var newValue = "newvalue";
database.StringSet(key, newValue);

Delete Operation:

Redis’ KeyDelete method may be used to remove a key-value pair:

var key = "mykey";
database.KeyDelete(key);

 

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories