CodePudding user response:
The.net how to store and query redis can easy to many data, only it particularly simple,Redis is very small, it is only slightly a little bit more simple than SQL Server, can not say what the object, the data record and is simple in structure, need to write a lot of extra code programmers,
.net application cache, we use the key to whether the local cache or network distributed cache, the first is "a stateful object oriented", the second is "automatic clustering and routing management", the cache is object oriented, with a state, c # business logic code automatically deployed to the object, when faced with high concurrency requirements and system complexity to a certain level, can know the real necessity of distributed system, and not to find a simplistic data with expensive memory the host to "cache",
CodePudding user response:
The basic semantic is, for example, I wrotevar. Obj=Common Program. Conn. GetGrainthis means that the inventory of the code is deployed in many programs on the server, network I can respectively 10000 stock, concurrent, asynchronously with 5000 inventory records for processing, each kind of program has its own physical count, respectively by the different developers (or programmer) development, the development of the incremental deployment to a web server at any time, and my "client" just naturally through the distributed cache object to invoke the distributed computing capacity on the network, rather than blocking network bandwidth too simple repetition "to add and delete",(code);
Obj. Inventory (record);
CodePudding user response:
1, Visual Studio 2019 to create a new asp.net core project, select the Api template, the project should include a sample WeatherForecastController,2, the editor WeatherForecastController. Cs, add a IDistributedCache members, and introduce the namespace,
.
using Microsoft. Extensions. Caching. Distributed;
[ApiController]
[the Route (" [controller] ")]
Public class WeatherForecastController: ControllerBase
{
Summaries=new private static readonly string [] []
{
"Freezing", "Bracing" and "Chilly", "Cool", "Mild", "Warm" and "Balmy", "Hot", "Sweltering", "Scorching"
};
Private readonly ILogger
private readonly IDistributedCache _cache;
Public WeatherForecastController (ILogger
{
_logger=logger;
_cache=cache;
}
3, install nuget package Microsoft. Extensions. Caching. StackExchangeRedis, the current version is 3.1.5,
4, the nuget package provides the StackExchangeRedis customer, and can provide the IDistributedCache service,
5, the editor Startup. Cs, injection IDistributedCache services, including redis - server: "6379" to you can use redis server address,
Public void ConfigureServices (IServiceCollection services)
{
Services. AddControllers ();
services. AddStackExchangeRedisCache (options=& gt; The options. The Configuration=redis - server: "6379");
}
6, so that you can use Redis provide caching, for example, you can rewrite WeatherForecastController IEnumerable
[HttpGet]
Public IEnumerable
{
const string cacheKey="my - weather forecast -";
(1) {var cacheOneMinute=new DistributedCacheEntryOptions AbsoluteExpirationRelativeToNow=TimeSpan. FromMinutes (1)};
Var json=_cache. Get string (cacheKey);
If (json==null)
{
Var RNG=new Random ();
Var list=Enumerable. Range (1, 5), Select (index=& gt; New WeatherForecast
{
The Date=DateTime. Now. AddDays (index),
TemperatureC=RNG. Next (- 20, 55),
The Summary=Summaries [RNG. Next (Summaries. Length)]
})
The ToArray ();
Json=JsonConvert. SerializeObject (list);
_cache. SetString (cacheKey, json, cacheOneMinute);
}
Return JsonConvert. DeserializeObject
}
7, to run the program, refresh the page, a cache can be observed as a result, the case for a minute, a minute refresh, will not change as a result,