使用 StackExchange.Redis 訪問 Redisjavascript
static void Main(string[] args) { using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost:6379")) { IDatabase db = redis.GetDatabase(); db.StringSet("name", "Michal Jackson"); string name = db.StringGet("name"); Console.WriteLine(name); //結果:Michal Jackson db.StringSet("age", "11"); //incr 自增 db.StringIncrement("age"); RedisValue age = db.StringGet("age"); Console.WriteLine(age);//結果:12 //incrby 指定增量 age = db.StringIncrement("age", 5); Console.WriteLine(age);//結果:17 //decr 自減 age = db.StringDecrement("age"); Console.WriteLine(age);//結果:16 //decrby 指定減量 age = db.StringDecrement("age", 5); Console.WriteLine(age);//結果:11 //mset 設置多個值 db.StringSet(new KeyValuePair<RedisKey, RedisValue>[] { new KeyValuePair<RedisKey, RedisValue>("aa", "aa"), new KeyValuePair<RedisKey, RedisValue>("bb", "bb"), new KeyValuePair<RedisKey, RedisValue>("cc", "5"), }); //mget 取多個值 var values = db.StringGet(new RedisKey[] { "aa", "bb", "cc" }); foreach (RedisValue redisValue in values) { Console.Write(redisValue + ","); } //結果:aa,bb,5 //exists 是否存在 db.StringSet("name1", "Dave1"); bool existsResult = db.KeyExists("name1"); Console.WriteLine(existsResult); //結果:true //del 刪除 bool delResult = db.KeyDelete("name1"); Console.WriteLine(delResult); //結果:true existsResult = db.KeyExists("name1"); Console.WriteLine(existsResult); //結果:false //type 判斷類型 db.StringSet("name2", "Dave2"); var typeOfValue = db.KeyType("name2"); Console.WriteLine(typeOfValue); //String //expire 過時時間 db.StringSet("name3", "Dave3"); db.KeyExpire("name3", TimeSpan.FromSeconds(5)); RedisValue value = db.StringGet("name3"); Console.WriteLine(value); //Dave3 Console.WriteLine("此處等待6秒..."); Thread.Sleep(6 * 1000); value = db.StringGet("name3"); //啥也沒有.. Console.WriteLine(value); //ex 設置key直接設置有效期 db.StringSet("name4","Dave4", TimeSpan.FromSeconds(5)); RedisValue value4 = db.StringGet("name4"); Console.WriteLine(value4); //Dave4 Console.WriteLine("此處等待6秒..."); Thread.Sleep(6 * 1000); value4 = db.StringGet("name4"); //啥也沒有.. Console.WriteLine(value4); //ttl 查看過時時間 db.StringSet("name6","Dave6", TimeSpan.FromSeconds(5)); for (int i = 1; i < 7; i++) { Thread.Sleep( 1000); RedisValue valueTTL = db.StringGet("name6"); var ttl = db.KeyTimeToLive("name6"); if (ttl==null) { Console.WriteLine($"{i}秒事後:Dave6已過時"); } else { Console.WriteLine($"{i}秒事後:{valueTTL}還能存活{ttl}秒"); } } // 1秒事後:Dave6還能存活00:00:03.9970000秒 // 2秒事後:Dave6還能存活00:00:02.9040000秒 // 3秒事後:Daue6還能存活00:00:01.9040000秒 // 4秒事後:Dave6還能存活00:00:00.9030000秒 // 5秒事後:Dave6已過時 // 6秒事後:Daue6已過時 } Console.ReadKey(); }
先 Startup.cs
注入 IConnectionMultiplexer
html
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddSingleton<IConnectionMultiplexer>(ConnectionMultiplexer.Connect("localhost:6379"));
在 HomeController.cs
java
public class HomeController : Controller { private readonly IConnectionMultiplexer _redis; private readonly IDatabase _db; public HomeController(IConnectionMultiplexer redis) { _redis = redis; _db = _redis.GetDatabase(); } public IActionResult Index() { _db.StringSet("fullname", "Michael Jackson"); var name = _db.StringGet("fullname"); return View("Index", name); } }
視圖 Index
redis
@model StackExchange.Redis.RedisValue <div class="text-center"> <h1 class="display-4"> Welcome:@Model </h1> <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p> </div>
建立 CounterViewComponent 類app
using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; using StackExchange.Redis; namespace RedisToEsay.ViewComponents { public class CounterViewComponent : ViewComponent { private readonly IDatabase _db; public CounterViewComponent(IConnectionMultiplexer redis) { _db = redis.GetDatabase(); } public async Task<IViewComponentResult> InvokeAsync() { string controller = RouteData.Values["controller"].ToString(); string action = RouteData.Values["action"].ToString(); if (!string.IsNullOrWhiteSpace(controller) && !string.IsNullOrWhiteSpace(action)) { var pageId = $"{controller}-{action}"; await _db.StringIncrementAsync(pageId); var count = _db.StringGet(pageId); return View("Default", pageId + ":" + count); } throw new Exception("can't get pageId"); } } }
建立 Default 視圖async
@model string <h4>@Model</h4>
而後在Shared_Layout.cshtml 調用ide
<div> @await Component.InvokeAsync("Counter") </div>
報錯:Defualt 建立的位置不對ui
An unhandled exception occurred while processing the request. InvalidOperationException: The view 'Components/Counter/Default' was not found. The following locations were searched: /Views/Home/Components/Counter/Default.cshtml /Views/Shared/Components/Counter/Default.cshtml /Pages/Shared/Components/Counter/Default.cshtml
因此建立的位置在
/Views/Shared/Components/Counter/Default.cshtml
spa
草根專欄,Redis in .NET Core 入門:(2) String
楊旭(Video),Redis in ASP.NET Core 1. 計數器.net