This article is a mirror article of machine translation, please click here to jump to the original article.

View: 44102|Reply: 1

[.NET Core] ASP.NET Core (1) uses Redis caching

[Copy link]
Posted on 2020-9-22 18:47:34 | | | |
I've written a lot about ASP.NET Core before, as follows:

ASP.NET Core gets the relative path to the current URL
https://www.itsvse.com/thread-9389-1-1.html

ASP.NET Core injects NullReferenceException with the Autofac attribute
https://www.itsvse.com/thread-9384-1-1.html

ASP.NET Core 3.1 Calling .NET 4.0 WCF Services tutorial
https://www.itsvse.com/thread-9313-1-1.html

Ocelot Series (1) ASP.NET Core Gateway in practice
https://www.itsvse.com/thread-9284-1-1.html

ASP.NET Core uses the MediatR intermediary model
https://www.itsvse.com/thread-9272-1-1.html

ASP.NET Core view uses Blazor's image captcha
https://www.itsvse.com/thread-9143-1-1.html

Downloading the exe file in asp.net core returns a 404 error solution
https://www.itsvse.com/thread-9026-1-1.html

asp.net core registers for global Authorize authentication
https://www.itsvse.com/thread-9022-1-1.html

Asp.net Core Model Data Validation
https://www.itsvse.com/thread-9016-1-1.html

ASP.NET Core 3.0 Web modification Razor view does not refresh
https://www.itsvse.com/thread-8996-1-1.html

ASP.NET Core MVC memory cache usage tutorial
https://www.itsvse.com/thread-8861-1-1.html

ASP.NET Core 3.0 Nuget restore failed
https://www.itsvse.com/thread-8858-1-1.html

Practical combat: ASP.NET Core uses gRPC communication
https://www.itsvse.com/thread-8834-1-1.html

ASP.NET Detailed explanation of Core Middleware
https://www.itsvse.com/thread-8126-1-1.html

asp.net core writes a middleware to record the interface time
https://www.itsvse.com/thread-8079-1-1.html

Deploy ASP.NET Core to IIS server
https://www.itsvse.com/thread-7838-1-1.html

ASP.NET Core uses Autofac to implement AOP interception
https://www.itsvse.com/thread-7566-1-1.html

ASP.NET Core ILogger Log Usage Tutorial
https://www.itsvse.com/thread-7565-1-1.html

ASP.NET Core uses Autofac to implement IOC injection
https://www.itsvse.com/thread-7563-1-1.html

ASP.NET Core dependency injection uses the IOC container that comes with it
https://www.itsvse.com/thread-7562-1-1.html

【Practical Combat】ASP.NET Core's Entity Framework Core introductory tutorial
https://www.itsvse.com/thread-7553-1-1.html

ASP.NET Core – Razor View Import Usage Description
https://www.itsvse.com/thread-7550-1-1.html

ASP.NET Implementation of custom routing constraints in Core
https://www.itsvse.com/thread-7549-1-1.html

Create an MVC project ASP.NET the Core command line
https://www.itsvse.com/thread-4868-1-1.html
ASP.NET Core provides a variety of IDistributedCache implementations (Redis, SQL Server, In-memory).

Tutorial on memory cache:

ASP.NET Core MVC memory cache usage tutorial
https://www.itsvse.com/thread-8861-1-1.html


For information on how to install redis in Windows, see:

Redis has three ways to start
https://www.itsvse.com/thread-4008-1-1.html


First of allMicrosoft.Extensions.Caching.RedisandMicrosoft.Extensions.Caching.StackExchangeRedisare implementations of the Microsoft.Extensions.Caching.Distributed.IDistributedCache interface.

This article uses Microsoft.Extensions.Caching.StackExchangeRedis to implement caching operations, as Microsoft.Extensions.Caching.Redis was last updated in 2018.


In a ASP.NET Core project, add a nuget package using the command as follows:

Register the caching service in the ConfigureServices method, and by decompiling the code, you can see that the registered isSingleton mode, that method must be the sameThread safetyof. For more settings, configure the ConfigurationOptions property.



By reading the configuration file, the redis configuration is set up, the port number and connection password are customized, and the appsettings.json configuration is as follows:



Try adding cache data to redis in the controller, the controller code is as follows:

Cache configuration:

  • AbsoluteExpiration absolute expiration time, if null is null, the condition is invalid
  • AbsoluteExpirationRelativeToNow is the absolute expiration time relative to the current time (using TimeSpan), and the null condition is invalid
  • SlidingExpiration sliding expiration time
  • ExpirationTokens are provided to customize cache expiration
  • PostEvictionCallbacks caches invalidation callbacks
  • Priority cache item priority (the order in which the cache is absolutely cleared when the cache is fully loaded)
  • Size represents the size of the cached data, which is generally null in memory caches



Refresh the controller code multiple times and query the cache through the redis-cli tool, as shown in the figure below:



Calling the SetString method to store the value, using hash type storage, I don't know why.

(End)




Previous:EF Core Series 2 encapsulates OnModelCreating, creates indexes using reflections, and more
Next:asp.net Core source code address
Posted on 2021-9-22 20:02:31 |
Learn to learn...
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com