https://github.com/2881099/csredishtml
csredis-in-asp.net core理論實戰-主從配置、哨兵模式git
https://github.com/luoyunchong/dotnetcore-examples/tree/master/dotnet-core-redisgithub
建立一個. NET Core WebAPI項目redis
想執行 . NET Core CLI命令行,要cd到csproj同級目錄中json
dotnet add package CSRedisCore #mvc分佈式緩存注入 dotnet add package Caching.CSRedis
或
程序包管理控制檯(Package Manager)中運行,選擇你的項目api
Install-Package CSRedisCore Install-Package Caching.CSRedis
{ "CsRedisConfig": { "DefaultConnectString": "127.0.0.1:6379,password=,defaultDatabase=0,prefix=csredis-default-" } }
public void ConfigureServices(IServiceCollection services) { // eg 1.單個redis實現 普通模式 //CSRedisClient csredis = new CSRedisClient("127.0.0.1:6379,password=,defaultDatabase=csredis,prefix=csredis-example"); //eg 2.單個redis,使用appsettings.json中的配置項 IConfigurationSection configurationSection = Configuration.GetSection("CsRedisConfig:DefaultConnectString"); CSRedisClient csredis = new CSRedisClient(configurationSection.Value); //初始化 RedisHelper RedisHelper.Initialization(csredis); //註冊mvc分佈式緩存 services.AddSingleton<IDistributedCache>(new CSRedisCache(RedisHelper.Instance)); ...其餘代碼 }
經過靜態方法調用,鍵爲test1 ,value爲前臺傳來的值,緩存60s緩存
獲取值Get方法, test1做爲鍵,返回值給前臺。60s後再獲取,將沒法獲得值。數據結構
// 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"); }
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); Console.ReadKey(); } }
前提mvc
{ "CsRedisConfig": { "SentinelConnectString": "mymaster,password=,prefix=csredis-example-", "Sentinel": [ "127.0.0.1:26379", "127.0.0.1:26380", "127.0.0.1:26381" ] } }
public void ConfigureServices(IServiceCollection services) { //eg.3 使用appsettings.json,哨兵模式 IConfigurationSection configurationSection = Configuration.GetSection("CsRedisConfig:SentinelConnectString"); string[] sentinelValues = Configuration.GetSection("CsRedisConfig:Sentinel").Get<string[]>(); CSRedisClient csredis = new CSRedisClient(configurationSection.Value, sentinelValues); //初始化 RedisHelper RedisHelper.Initialization(csredis); //註冊mvc分佈式緩存 services.AddSingleton<IDistributedCache>(new CSRedisCache(RedisHelper.Instance)); ...其餘代碼 }
RedisHelper 與redis-cli命令行保持一致的api,會使用redis相關命令,即會使用RedisHelper方法app
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(); } static void Test() { //1.set key value [ex seconds] [px milliseconds] [nx|xx] //setex key seconds value #設定鍵的值,並指定此鍵值對應的 有效時間。 //setnx key value #鍵必須 不存在,才能夠設置成功。若是鍵已經存在,返回 0。 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. 獲取值 //get key //若是要獲取的 鍵不存在,則返回 nil(空)。 string redisValue = RedisHelper.Get("redis-key"); Console.WriteLine($"setex redis-key 50 just a string value ,RedisHelper.Get()獲得值以下:{redisValue}"); DateTime now = RedisHelper.Get<DateTime>("redis-key-class"); Console.WriteLine($"setex redis-key-class DateTime.Now,RedisHelper.Get()值以下{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. 批量獲取值 //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. 計數 //incr key //incr 命令用於對值作 自增操做 //自增指定數字 long incr = RedisHelper.IncrBy("key"); Console.WriteLine($"incr key, incr獲得的值是{incr}"); //設置自增數字的增量值 incr = RedisHelper.IncrBy("key",2); Console.WriteLine($"再次incrby key 2, incr獲得的值是{incr}"); incr = RedisHelper.IncrBy("key", -2); Console.WriteLine($"再次decrby key -2, incr獲得的值是{incr}"); //exists key bool isExistsKey = RedisHelper.Exists("new-key"); Console.WriteLine($"exists key ,value:{isExistsKey}"); double incrByFloat=RedisHelper.IncrByFloat("key-float", 0.1); Console.WriteLine($"incrbyfloat key-float 0.1,value:{incrByFloat}"); }