using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using static GB5Shared.GB5Constant.Constant; namespace GB5Shared.DTO.Framework.HybridCache { public class CacheDTO { private int serverid = -1; private int clientid = -1; private int serverconfigid = -1; private int userid = -1; private int roleid = -1; private int logineventlogid = -1; private int objectid = -1; private int objecttypeid = -1; private string sourcedata = string.Empty; public int ServerId { get { return serverid; } set { serverid = value; } } public int ClientId { get { return clientid; } set { clientid = value; } } public int ServerConfigId { get { return serverconfigid; } set { serverconfigid = value; } } public int UserId { get { return userid; } set { userid = value; } } public int RoleId { get { return roleid; } set { roleid = value; } } public int LoginEventLogId { get { return logineventlogid; } set { logineventlogid = value; } } public int ObjectId { get { return objectid; } set { objectid = value; } } public int ObjectTypeId { get { return objecttypeid; } set { objecttypeid = value; } } public string SourceData { get { return sourcedata; } set { sourcedata = value; } } } public static class CacheKeyGenerator { public static string KeyGeneration(int ObjectId, int ObjectTypeId, int CachingKeyLevel, string SourceData, LoginDTO LoginDTO, CriteriaDTO CriteriaDTO = null) { string DataToHash = null; string HashCachingKey = null; // ICommonFunctionFrameDAL Common = new CommonFunctionFrameDAL(); CacheDTO CacheDTO = new CacheDTO(); try { if (CachingKeyLevel == CacheKeyLevel.NOT_REQUIRED) { return "Null"; //---Not Required, Key "Null" } if (CachingKeyLevel == CacheKeyLevel.OVERALL) { CacheDTO.ServerId = -1; CacheDTO.ClientId = -1; CacheDTO.ServerConfigId = -1; CacheDTO.UserId = -1; CacheDTO.RoleId = -1; CacheDTO.LoginEventLogId = -1; } if (CachingKeyLevel == CacheKeyLevel.DB_SERVER) { CacheDTO.ServerId = LoginDTO.ServerId; } if (CachingKeyLevel == CacheKeyLevel.CLIENT_LEVEL) { CacheDTO.ServerId = LoginDTO.ServerId; CacheDTO.ServerConfigId = LoginDTO.ServerConfigId; CacheDTO.ClientId = LoginDTO.ClientId; } if (CachingKeyLevel == CacheKeyLevel.ROLE_LEVEL) { CacheDTO.ServerId = LoginDTO.ServerId; CacheDTO.ClientId = LoginDTO.ClientId; CacheDTO.ServerConfigId = LoginDTO.ServerConfigId; CacheDTO.RoleId = LoginDTO.RoleId; } if (CachingKeyLevel == CacheKeyLevel.USER_LEVEL) { CacheDTO.ServerId = LoginDTO.ServerId; CacheDTO.ClientId = LoginDTO.ClientId; CacheDTO.ServerConfigId = LoginDTO.ServerConfigId; CacheDTO.UserId = LoginDTO.UserId; CacheDTO.RoleId = LoginDTO.RoleId; } if (CachingKeyLevel == CacheKeyLevel.SESSION_LEVEL) { CacheDTO.LoginEventLogId = LoginDTO.LoginEventLogId; } CacheDTO.ObjectId = ObjectId; CacheDTO.ObjectTypeId = ObjectTypeId; CacheDTO.SourceData = SourceData; //string LoginDTOJsonString = JsonSerializer.Serialize(CacheDTO); //DataToHash = $"{ObjectId}:{ObjectTypeId}:{LoginDTOJsonString}"; //if (CriteriaDTO != null) //{ // string criteriaJsonString = JsonSerializer.Serialize(CriteriaDTO); // DataToHash = $"{DataToHash}:{criteriaJsonString}"; //} //using (Sha256 = SHA256.Create()) //SHA-256 hash of the data for Cachekey //{ // byte[] HashBytes = Sha256.ComputeHash(Encoding.UTF8.GetBytes(DataToHash)); // HashCachingKey = Convert.ToBase64String(HashBytes); //} return CacheKeyGenerator.GenerateKey(CacheDTO); } catch (Exception) { throw; } } private static string GenerateKey(CacheDTO dto) { return $"{dto.ObjectId}|{dto.ObjectTypeId}|{dto.ServerId}|{dto.ServerConfigId}|{dto.ClientId}|{dto.UserId}|{dto.RoleId}|{dto.LoginEventLogId}|{dto.SourceData}"; } } }