remove old references and remove unnecessary code
This commit is contained in:
		| @@ -1,134 +0,0 @@ | ||||
| using Azure.Identity; | ||||
| using Core.Blueprint.DAL.Redis.Contracts; | ||||
| using Microsoft.Extensions.Logging; | ||||
| using StackExchange.Redis; | ||||
| using System.Text.Json; | ||||
|  | ||||
|  | ||||
| namespace Core.Blueprint.DAL.Redis | ||||
| { | ||||
|     public class CacheService : ICacheService | ||||
|     { | ||||
|         private IDatabase _cacheDatabase = null!; | ||||
|         private readonly ILogger<CacheService> _logger; | ||||
|  | ||||
|  | ||||
|         public CacheService(string connectionString, ILogger<CacheService> logger) | ||||
|         { | ||||
|             _logger = logger; | ||||
|             Task.Run(async () => | ||||
|             { | ||||
|                 _cacheDatabase = await GetRedisDatabase(connectionString); | ||||
|             }).Wait(); | ||||
|         } | ||||
|  | ||||
|         private async Task<IDatabase> GetRedisDatabase(string connectionString) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var configurationOptions = await ConfigurationOptions.Parse($"{connectionString}") | ||||
|                     .ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential()); | ||||
|  | ||||
|                 configurationOptions.AbortOnConnectFail = false; | ||||
|                 var connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(configurationOptions); | ||||
|  | ||||
|                 _logger.LogInformation("Successfully connected to Redis."); | ||||
|  | ||||
|                 return connectionMultiplexer.GetDatabase(); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 _logger.LogError(ex, "Error establishing Redis connection."); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|  | ||||
|         public async Task<T> GetAsync<T>(string key) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var value = await _cacheDatabase.StringGetAsync(key); | ||||
|                 if (value.IsNullOrEmpty) | ||||
|                 { | ||||
|                     _logger.LogInformation($"Cache miss for key: {key}"); | ||||
|                     return default; | ||||
|                 } | ||||
|  | ||||
|                 _logger.LogInformation($"Cache hit for key: {key}"); | ||||
|                 return JsonSerializer.Deserialize<T>(value); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 _logger.LogError(ex, $"Error getting cache item with key {key}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task SetAsync<T>(string key, T value, TimeSpan? expiry = null) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var json = JsonSerializer.Serialize(value); | ||||
|                 await _cacheDatabase.StringSetAsync(key, json, expiry); | ||||
|                 _logger.LogInformation($"Cache item set with key: {key}"); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 _logger.LogError(ex, $"Error setting cache item with key {key}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task RemoveAsync(string key) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 await _cacheDatabase.KeyDeleteAsync(key); | ||||
|                 _logger.LogInformation($"Cache item removed with key: {key}"); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 _logger.LogError(ex, $"Error removing cache item with key {key}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task<bool> ExistsAsync(string key) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var exists = await _cacheDatabase.KeyExistsAsync(key); | ||||
|                 _logger.LogInformation($"Cache item exists check for key: {key} - {exists}"); | ||||
|                 return exists; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 _logger.LogError(ex, $"Error checking existence of cache item with key {key}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task RefreshAsync(string key, TimeSpan? expiry = null) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var value = await _cacheDatabase.StringGetAsync(key); | ||||
|                 if (!value.IsNullOrEmpty) | ||||
|                 { | ||||
|                     await _cacheDatabase.StringSetAsync(key, value, expiry); | ||||
|                     _logger.LogInformation($"Cache item refreshed with key: {key}"); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     _logger.LogWarning($"Cache item with key: {key} does not exist, cannot refresh"); | ||||
|                 } | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 _logger.LogError(ex, $"Error refreshing cache item with key {key}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -1,7 +0,0 @@ | ||||
| namespace Core.Blueprint.DAL.Redis.Configuration | ||||
| { | ||||
|     public class CacheSettings | ||||
|     { | ||||
|         public int DefaultCacheDurationInMinutes { get; set; } | ||||
|     } | ||||
| } | ||||
| @@ -1,27 +0,0 @@ | ||||
| using Core.Blueprint.DAL.Redis.Contracts; | ||||
| using Microsoft.Extensions.Configuration; | ||||
| using Microsoft.Extensions.DependencyInjection; | ||||
| using Microsoft.Extensions.Logging; | ||||
|  | ||||
| namespace Core.Blueprint.DAL.Redis.Configuration | ||||
| { | ||||
|     public static class RedisExtension | ||||
|     { | ||||
|         public static IServiceCollection AddRedisLayer(this IServiceCollection services, IConfiguration configuration) | ||||
|         { | ||||
|             var source = configuration.GetSection("ConnectionStrings"); | ||||
|  | ||||
|             var redisConnectionString = source["Redis"]?.ToString(); | ||||
|  | ||||
|             if (string.IsNullOrEmpty(redisConnectionString)) | ||||
|             { | ||||
|                 throw new InvalidOperationException("Redis connection string is not configured."); | ||||
|             } | ||||
|  | ||||
|             services.AddSingleton<ICacheService>(provider => | ||||
|                 new CacheService(redisConnectionString, provider.GetRequiredService<ILogger<CacheService>>())); | ||||
|  | ||||
|             return services; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -1,11 +0,0 @@ | ||||
| namespace Core.Blueprint.DAL.Redis.Contracts | ||||
| { | ||||
|     public interface ICacheService | ||||
|     { | ||||
|         Task<T> GetAsync<T>(string key); | ||||
|         Task SetAsync<T>(string key, T value, TimeSpan? expiry = null); | ||||
|         Task RemoveAsync(string key); | ||||
|         Task<bool> ExistsAsync(string key); | ||||
|         Task RefreshAsync(string key, TimeSpan? expiry = null); | ||||
|     } | ||||
| } | ||||
| @@ -7,11 +7,7 @@ | ||||
|   </PropertyGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <PackageReference Include="Azure.Identity" Version="1.13.1" /> | ||||
|     <PackageReference Include="Microsoft.Azure.StackExchangeRedis" Version="3.2.0" /> | ||||
|     <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" /> | ||||
|     <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" /> | ||||
|     <PackageReference Include="StackExchange.Redis" Version="2.8.24" /> | ||||
|     <PackageReference Include="Blueprint.Redis" Version="0.0.1" /> | ||||
|   </ItemGroup> | ||||
|  | ||||
| </Project> | ||||
|   | ||||
| @@ -1,30 +0,0 @@ | ||||
| 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; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -1,46 +0,0 @@ | ||||
| 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