Add project files.
This commit is contained in:
		
							
								
								
									
										30
									
								
								Core.Blueprint.DAL.Redis/Helpers/CacheHelper.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								Core.Blueprint.DAL.Redis/Helpers/CacheHelper.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| using Core.Blueprint.DAL.Redis.Configuration; | ||||
|  | ||||
| namespace Core.Blueprint.DAL.Redis.Helpers | ||||
| { | ||||
|     public static class CacheHelper | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// Determines the cache duration based on specific duration, settings, or a default value. | ||||
|         /// </summary> | ||||
|         /// <param name="specificCacheDuration">Specific cache duration in minutes, if provided.</param> | ||||
|         /// <param name="cacheSettings">General cache settings containing default duration values.</param> | ||||
|         /// <returns>The cache duration as a TimeSpan.</returns> | ||||
|         public static TimeSpan GetCacheDuration(CacheSettings cacheSettings, int? specificCacheDuration = 0) | ||||
|         { | ||||
|             var defaultCacheDuration = TimeSpan.FromMinutes(.5); | ||||
|  | ||||
|             if (specificCacheDuration.HasValue && specificCacheDuration.Value > 0) | ||||
|             { | ||||
|                 return TimeSpan.FromMinutes(specificCacheDuration.Value); | ||||
|             } | ||||
|  | ||||
|             if (cacheSettings.DefaultCacheDurationInMinutes > 0) | ||||
|             { | ||||
|                 return TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes); | ||||
|             } | ||||
|  | ||||
|             return defaultCacheDuration; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										46
									
								
								Core.Blueprint.DAL.Redis/Helpers/CacheKeyHelper.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								Core.Blueprint.DAL.Redis/Helpers/CacheKeyHelper.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| using System.Text; | ||||
| using System.Text.RegularExpressions; | ||||
|  | ||||
| namespace Core.Blueprint.DAL.Redis.Helpers | ||||
| { | ||||
|     public static class CacheKeyHelper | ||||
|     { | ||||
|         public static string GenerateCacheKey(object instance, string methodName, params object[] parameters) | ||||
|         { | ||||
|             var className = instance.GetType().Name; | ||||
|             var keyBuilder = new StringBuilder($"{className}.{methodName}"); | ||||
|  | ||||
|             foreach (var param in parameters) | ||||
|             { | ||||
|                 string normalizedParam = NormalizeParameter(param); | ||||
|                 keyBuilder.Append($".{normalizedParam}"); | ||||
|             } | ||||
|  | ||||
|             return keyBuilder.ToString(); | ||||
|         } | ||||
|  | ||||
|         private static string NormalizeParameter(object param) | ||||
|         { | ||||
|             if (param == null) | ||||
|             { | ||||
|                 return "null"; | ||||
|             } | ||||
|  | ||||
|             string paramString; | ||||
|  | ||||
|             if (param is DateTime dateTime) | ||||
|             { | ||||
|                 paramString = dateTime.ToString("yyyyMMdd"); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 paramString = param.ToString(); | ||||
|             } | ||||
|  | ||||
|             // Replace special characters with an underscore | ||||
|             string normalizedParam = Regex.Replace(paramString, @"[^a-zA-Z0-9]", "_"); | ||||
|  | ||||
|             return normalizedParam; | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Sergio Matias Urquin
					Sergio Matias Urquin