For example: I have the following requirement. When I want to obtain user information, I will first look up from the local cache, then look up from the distributed cache, and finally I can't find it and then query from the database. But in some scenarios I don't need to query the database.
So I want to build a model like this.
public UserEntity GetUserInfo(List<DataSource> dataSources) { var xxxx = new UserEntity(); if(() { //Fetch from local cache return xxxx; } if(() { //Get from distributed cache //Update local cache return xxxx; } if(() { //Get from DB //Update distributed cache //Update local cache } return xxxx; }
However, it is more troublesome to build a List every time the caller will build a List. At this time, we can use the Flags feature in the enumeration and modify the program as follows:
First of all, the definition of enumeration, add the [Flags] feature tag, and the definition is generally to the n-power of 2, mainly to facilitate displacement operation
/// <summary> /// Where to get data/// </summary> [Flags] public enum DataSource { /// <summary> /// Local cache /// </summary> [Description("Local Cache")] LocalCache = 1, /// <summary> /// Distributed Cache /// </summary> [Description("Distributed Cache")] DistributeCache = 2, /// <summary> /// database /// </summary> [Description("database")] DB = 4, }
Modify the code:
public UserEntity GetUserInfo(DataSource dataSources) { var xxxx = new UserEntity(); if(() { //Fetch from local cache return xxxx; } if(() { //Get from distributed cache //Update local cache return xxxx; } if(() { //Get from DB //Update distributed cache //Update local cache } return xxxx; }
The place where the call can be specified by "|", for example, I just want to use distributed cache and database, then:
var userInfo = GetUserInfo( | );
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.