Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the design of the .NET cache module?

Shulou Source: shulou.com Published: 2022-06-03 12:49:56 10月24日 Update

Net cache module design, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Basic cache module design

The most basic cache module must have a unified CacheHelper, as follows:

Public interface ICacheHelper {T Get (string key); void Set (string key, T value); void Remove (string key);}

Then the business layer is called like this

Public User Get (int id) {if (id c is PrimaryKeyAttribute)); / / get value var keyValue = primaryKey.GetValue (obj, null) of ID by fetching PrimaryKeyAttribute here; var key = string.Format ("urn: {0}: {1}", type.Name, keyValue); var dt = DateTime.UtcNow.AddDays (1); / / assume 1-day default cache var offset = new DateTimeOffset (dt); Cache.Set (key, obj, offset);}

At this point, the final version of ICache that I think of is complete. It is also important to note that PrimaryKey can actually be more flexible. In many cases, the PrimaryKey of an Object is very complicated. When designing a Cache entity, you can modify it as follows:

Public class UserCacheEntity {[PrimaryKey] public int ID {get {return string.Format ("{0}: {1}", UserId, UserName);} public int UserId {get; set;} public string UserName {get; set;} public string Cellphone {get; set;}}

The above method can almost automatically manage the common data Cache, the only trouble is the need to customize a CacheObject, which brings the trouble of entity transformation, it depends on how to choose.

Explain again the ICache design I want:

1. Always only Cache hot data, which means that every Key has to have an expiration time

2. ICache automatically manages Get/Set, preferably Key.

3. ICache is streamlined while not losing flexibility.

For detailed code Demo, please refer to: Git

More flexible implementation

Before I wrote this summary, I was also thinking about which layer Cache should be put on, and where to put it on the third floor. Where do you put it when DDD is layered like that? Google and see some references.

Cache should be globally arbitrary, and of course it should be implemented as interface+IOC, so it can be referenced more independently.

After reading the above, have you mastered the method of .NET cache module design? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Caching design time modules code management data methods methods faceted reference obvious important content again entity manual efficiency more syntax Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MariaDB Linux Shulou Information OPPO Reno