Home > Net >  How to use the cache in the ASP.NET Core static class
How to use the cache in the ASP.NET Core static class

Time:09-22

There is a demand, get the latest version of js file, similar to the following code, this method is extracted from a controller, originally the _cache, _env via constructor injection, instead of a static method cannot by constructing injected, why is everyone said DI is not a static global variable, feeling a lot of cases, global and static variables than singleton DI convenience?
 
Private static string GetFileUrl (string folder, string fileName)
{
String key=$" AppendVersion. {folder}. {fileName} ";
A string value.
if (! _cache. TryGetValue (key, value) out)
{
Value="https://bbs.csdn.net/topics/$/{folder}/{fileName}";
Var options=new MemoryCacheEntryOptions (). AddExpirationToken (_env. WebRootFileProvider. Watch (value));
Var the fileInfo=_env. WebRootFileProvider. GetFileInfo (value);

If (the fileInfo. The Exists)
{
Value +=the fileInfo. LastModified. ToLocalTime (), ToString ("? YyMMddHHmmss ");
}
_cache. Set (key, value, the options).
}
return value;
}

CodePudding user response:

Static methods referenced in the statement as static variables, obviously, the more some people around the pattern, the more abstract the tall,

Programming should be "only to do the necessary", don't too early to use redundant mode, the idea in different people have different understanding, but I am always stressed "do not make unnecessary abstraction, must avoid crosscutting and inversion of empty practices" ,

CodePudding user response:

Crosscutting, inversion, such as words, actually know as soon as you see, this is to emphasize life without of variables, and dynamically modify the code,

What do you think of everything as abstract don't need to fall to the ground, this way is really conducive to debug (especially a large number of high-strength stack code debugging), maintenance, expansion?

CodePudding user response:

reference 1st floor interacting in a professional developer response:
static methods referenced in the statement as static variables, obviously, the more some people around the pattern, the more abstract the tall,

Programming should be "only to do the necessary", don't too early to use redundant mode, the idea in different people have different understanding, but I am always stressed "do not make unnecessary abstraction, must avoid the crosscutting and inversion of hollow practices such as" ,
are you talking about Microsoft? I always feel the new framework of Microsoft, is in force we DI sometimes actually we don't want to do it, such as when we have a demand to do DI ourselves to configure some third-party DI framework rather than it began to force on the bottom
  • Related