|
|
Posted on 2020-9-22 18:47:34
|
|
|
|

I've written a lot about ASP.NET Core before, as follows:
ASP.NET Core provides a variety of IDistributedCache implementations (Redis, SQL Server, In-memory).
Tutorial on memory cache:
For information on how to install redis in Windows, see:
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 moreNext:asp.net Core source code address
|