Note: The code examples mentioned in this article > https://code.msdn.microsoft.com/How-to-use-Redis-in-ASPNET-0d826418
Redis is an open-source, in-memory data structure storage system that can be used as a database, cache, and message middleware. It supports multiple types of data structures: strings, hash tables, lists, collections, ordered sets, and so on.
Redis officially does not roll out the Windows version, but Microsoft Open Tech provides support for Windows 64bit version.
How to install redis=> on a Windows machine download the installation file Redis-x64-3.2.100.msi, after installation, open Service Manager, locate the Redis service, and start it.
Pre-Preparation:
1. It is recommended to use visual Studio Update3 as your ide,:www.visualstudio.com
2. You need to install the. NET Core runtime environment and development tools, which are available in VS version: Www.microsoft.com/net/core
To create a project:
Open vs 2015, new Project->c# template->web->asp.net core Web application (. NET core)
Select a good path, the project name is Cscoreredis, select Web application, and the authentication option is none.
After the project is created, right-click on the Cscoreredis project to select Manage NuGet packages, search for Stackexchange.redis and install.
We will use the interface provided by this library to operate Redis.
Code:
The first thing to do is to add a Redis connection to HomeController.cs, and if you're not using a local Redis service, modify the connection string yourself.
Private Static New Lazy<connectionmultiplexer> (() ={ return connectionmultiplexer.connect (" localhost,abortconnect=false");}); Public Static connectionmultiplexer connection{ get { return Lazyconnection.value;} }
Add constructors to initialize database and list. Listleftpush is used here to get the latest data from left to right at the back of the ListRange.
Public Static stringListkeyname ="messagelist"; PublicHomeController () {db=connection.getdatabase (); if(Db. IsConnected (Listkeyname) && (!db. Keyexists (listkeyname) | | !db. KeyType (Listkeyname). Equals (redistype.list))) {//Add sample data.db. Keydelete (Listkeyname); //Push data from the leftDb. Listleftpush (Listkeyname,"TESTMSG1"); Db. Listleftpush (Listkeyname,"TESTMSG2"); Db. Listleftpush (Listkeyname,"TESTMSG3"); Db. Listleftpush (Listkeyname,"TESTMSG4"); }}
Modify the index.cshtml file, add input boxes and buttons
<formAction= "/home/sendmessage"Method= "POST"> <inputtype= "text"name= "message"style= "width:250px" /> <inputname= "Btnsend"value= "Send"type= "Submit"style= "margin-left:5px" /></form>
Adding the SendMessage method to the controller
[HttpPost] Public ActionResult SendMessage (string message) { if (db. IsConnected (listkeyname)) { db. Listleftpush (listkeyname, message); } return Redirecttoaction ("Index");}
Display a list of error messages or information
@if (@ViewData ["Error"] !=NULL){ "Error"]}Else{ <div id="messagelist"> @foreach (varMsginchModel) { <div> @Html. displayfor (ModelItem = msg) </div> } </div> }
Let's take a look at the running results
Enter a character in the input box, press the Send button, and the latest 5 messages will appear on the page.
to download the full code, please visit https://code.msdn.microsoft.com/How-to-use-Redis-in-ASPNET-0d826418
To learn more about Redis, please visit the Redis official website http://www.redis.io/
For detailed documentation on STACKEXCHANGE.REDIS, please visit the Https://github.com/StackExchange/StackExchange.Redis
To learn about Microsoft Azure Redis cache, please visit https://azure.microsoft.com/zh-cn/services/cache/
How to use Redis in ASP. NET Core