csredis-in-asp.net core theories combat - Example of use

GitHub csredis
https://github.com/2881099/csredis
sample source
https://github.com/luoyunchong/dotnetcore-examples/tree/master/dotnet-core-redis

Premise
installed and configured redis service is available.
vs2017 or vs2019 or VSCode
the .NET Core 2.2+ sdk
create a. NET Core WebAPI project

I want to perform. NET Core CLI command line, cd to the directory at the same level csproj

the Add Package Penalty for CSRedisCore DOTNET
#mvc distributed cache injection
DOTNET the Add Package Penalty for Caching.CSRedis
1
2
3
or
running Package Manager Console (Package Manager), select your project

The Package-CSRedisCore the Install
the Install the Package-Caching.CSRedis
. 1
2
normal mode
appsettings.json CI
{
"CsRedisConfig": {
"DefaultConnectString": "127.0.0.1:6379,password=,defaultDatabase=0,prefix=csredis-default-"
}
}
. 1
2
. 3
. 4
. 5
Startup.cs, set the
public void ConfigureServices (IServiceCollection Services)
{
// EG 1. redis achieve a single normal mode
// CSRedisClient csredis = new CSRedisClient ( " 127.0.0.1:6379,password=,defaultDatabase csredis =, = csredis-prefix Example ");
// EG 2. CI single redis, in use appsettings.json
IConfigurationSection configurationSection = Configuration.GetSection("CsRedisConfig:DefaultConnectString");
CSRedisClient csredis = new CSRedisClient(configurationSection.Value);

// Initialize RedisHelper
RedisHelper.Initialization (csredis);
// Register mvc distributed cache
services.AddSingleton <IDistributedCache> (new CSRedisCache ( RedisHelper.Instance));

... other code

}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
ValuesController.cs
call the static method, the key for the test1, value transmitted foreground value, cache 60s

Get value acquisition method, test1 as the key, the return value to the reception. 60s after the acquisition, will not get the value.

// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
RedisHelper.Set("test1", value, 60);
}

// GET api/values
[HttpGet]
public ActionResult<string> Get()
{
return RedisHelper.Get("test1");
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
普通模式-控制台
class Program
{
static void Main(string[] args)
{
var csredis = new CSRedis.CSRedisClient("127.0.0.1:6379,password=,defaultDatabase=CsRedis,prefix=CsRedis_ConSole_Example");
RedisHelper.Initialization(csredis);

RedisHelper.Set("test1", "123123", 60);
string result = RedisHelper.Get("test1");
Console.WriteLine("key:test1,value:" + result);

The Console.ReadKey ();
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
Sentinel Model
premise

Understand the role of sentry mode
and have an available primary (master) redis service, two from (slaver) services, three sentinel surveillance.
appsettings.json CI
{
"CsRedisConfig": {
"SentinelConnectString": "mymaster, password =, = csredis-example- prefix",
"the Sentinel": [
"127.0.0.1:26379",
"127.0.0.1:26380",
"127.0.0.1:26381"
]
}
}

. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
Startup.cs, set the
public void ConfigureServices (IServiceCollection Services)
{
//eg.3 use appsettings.json, Sentinel Model
IConfigurationSection configurationSection = Configuration.GetSection ( "CsRedisConfig: SentinelConnectString") ;

string[] sentinelValues = Configuration.GetSection("CsRedisConfig:Sentinel").Get<string[]>();

CSRedisClient csredis = new CSRedisClient(configurationSection.Value, sentinelValues);

// Initialize RedisHelper
RedisHelper.Initialization (csredis);
// Register mvc distributed cache
services.AddSingleton <IDistributedCache> (new CSRedisCache ( RedisHelper.Instance));

... other code
}

. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
using the same buffer in the normal mode, but a redis close a service, the service is still available, but if the cluster redis in handover procedure, there will be a short failure , after a while it will not recover.
Related Articles
Fun Redis gospel of .NET Core Developers another fool artifact recommend https://www.cnblogs.com/yilezhu/p/9947905.html
ways [from shallow to deep] redis achieve release subscription https://www.cnblogs.com/kellynic/p/9952386.html
depth analysis Redis series (four) - Redis data structures and global command overview https://juejin.im/post/5bb01064e51d453eb93d8028
RedisHelper and redis-cli command line consistent api, uses redis related commands, that method will be used RedisHelper

配合redis-cli命令行
static void Main()
{
CSRedisClient csredis = new CSRedisClient("127.0.0.1:6379,password=,defaultDatabase=CsRedis,prefix=CsRedis_ConSole_Example");
RedisHelper.Initialization(csredis);

Test();
Console.ReadKey();
}

void the Test static ()
{
//1.set Key value [seconds The EX] [PX milliseconds] [NX | XX]
// setex key seconds value # value setting key, and specifies the effective time corresponding to this key.
// setnx key value # key must not exist before you can successfully set. If the key already exists, 0 is returned.
RedisHelper.Set ( "redis-key", "just a string value", 50); // setex "redis-key" 50 "just a string value"

RedisHelper.Set("redis-key-class",DateTime.Now, 30);

//1.1.2. Gets the value
// GET Key
// If you want to get the key does not exist, nil is returned (empty).
RedisHelper.Get redisValue = String ( "Redis-Key");
Console.WriteLine ($ "Just SETEX Redis-50 A String Key value, RedisHelper.Get () values are obtained: redisValue {}");
the DateTime = RedisHelper now. GET <the DateTime> ( "Redis-Key-class");
Console.WriteLine ($ "SETEX the DateTime.Now Redis-Key-class, RedisHelper.Get () values are {now}");

//1.1.3. 批量设置值
//mset key value [key value ...]
RedisHelper.MSet("a", "1", "b", "2", "c", "3","d","4");//等价于mset a 1 b 2 c 3 d 4


//1.1.4. Batch to get the value
// mget key [key ...]

string[] mgetValues = RedisHelper.MGet<string>("a", "b", "c","d");
Console.WriteLine($"mset a 1 b 2 c 3 d 4, RedisHelper.MGet()得到的值是");
foreach (var mgetValue in mgetValues)
{
Console.Write($"{mgetValue}、");
}
Console.WriteLine();

//1.1.5 counting
// Key incr
// incr command value increment operator to make

// increment specified number
Long incr = RedisHelper.IncrBy ( "Key");
Console.WriteLine ($ "incr Key, obtain the value incr incr} is {");
// Set digital increment increment value
incr = RedisHelper.IncrBy ( "Key", 2);
Console.WriteLine ($ "incrby again key 2, the value is obtained incr {incr}");

= RedisHelper.IncrBy incr ( "Key", -2);
Console.WriteLine ($ "again decrby key -2, incr value obtained is {incr}");

//exists key
bool isExistsKey = RedisHelper.Exists("new-key");
Console.WriteLine($"exists key ,value:{isExistsKey}");

RedisHelper.IncrByFloat incrByFloat = Double ( "Key-a float", 0.1);
Console.WriteLine ($ "Key-incrbyfloat 0.1 a float, value: incrByFloat
{}");
} ------------- ---
Disclaimer: this article is the original article CSDN bloggers "iGeekFan", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/q710777720/article/details/95024485

Guess you like

Origin www.cnblogs.com/lhxsoft/p/12449218.html