diff --git a/Extensions.Caching.PostgreSql/DatabaseOperations.cs b/Extensions.Caching.PostgreSql/DatabaseOperations.cs index d5166ca..e59bf21 100644 --- a/Extensions.Caching.PostgreSql/DatabaseOperations.cs +++ b/Extensions.Caching.PostgreSql/DatabaseOperations.cs @@ -19,7 +19,7 @@ internal sealed class DatabaseOperations : IDatabaseOperations public DatabaseOperations(IOptions options, ILogger logger) { - var cacheOptions = options.Value; + cacheOptions = options.Value; if (string.IsNullOrEmpty(cacheOptions.ConnectionString)) { @@ -37,7 +37,7 @@ public DatabaseOperations(IOptions options, ILogger options, ILogger + /// Modify current conection string + /// + /// the new connection string to replace + public void ChangeConnectionString(string newConnectionString) + { + string original = _connectionString; + try + { + using var newConnection = new NpgsqlConnection(newConnectionString); + newConnection.Open(); + bool connected = newConnection.ExecuteScalar("SELECT 1=1;"); + if (connected) + { + _connectionString = newConnectionString; + _logger.LogDebug("ChangeConnectionString: new connection is valid and is use"); + } + newConnection.Close(); + if (cacheOptions.CreateInfrastructure) + { + CreateSchemaAndTableIfNotExist(); + } + } + catch (System.Exception ex) + { + _connectionString = original; + throw new InvalidOperationException("ChangeConnectionString failed.\nPrevious connection restored.", ex); + } - private ISystemClock SystemClock { get; } + + } private void CreateSchemaAndTableIfNotExist() { - using (var connection = new NpgsqlConnection(ConnectionString)) + using (var connection = new NpgsqlConnection(_connectionString)) { connection.Open(); using (var transaction = connection.BeginTransaction()) @@ -76,7 +107,7 @@ private void CreateSchemaAndTableIfNotExist() public void DeleteCacheItem(string key) { - using var connection = new NpgsqlConnection(ConnectionString); + using var connection = new NpgsqlConnection(_connectionString); var deleteCacheItem = new CommandDefinition( SqlCommands.DeleteCacheItemSql, @@ -88,7 +119,7 @@ public void DeleteCacheItem(string key) public async Task DeleteCacheItemAsync(string key, CancellationToken cancellationToken) { - await using var connection = new NpgsqlConnection(ConnectionString); + await using var connection = new NpgsqlConnection(_connectionString); var deleteCacheItem = new CommandDefinition( SqlCommands.DeleteCacheItemSql, @@ -116,7 +147,7 @@ public async Task DeleteExpiredCacheItemsAsync(CancellationToken cancellationTok { var utcNow = SystemClock.UtcNow; - await using var connection = new NpgsqlConnection(ConnectionString); + await using var connection = new NpgsqlConnection(_connectionString); var deleteExpiredCache = new CommandDefinition( SqlCommands.DeleteExpiredCacheSql, @@ -132,7 +163,7 @@ public void SetCacheItem(string key, byte[] value, DistributedCacheEntryOptions var absoluteExpiration = GetAbsoluteExpiration(utcNow, options); ValidateOptions(options.SlidingExpiration, absoluteExpiration); - using var connection = new NpgsqlConnection(ConnectionString); + using var connection = new NpgsqlConnection(_connectionString); var expiresAtTime = options.SlidingExpiration == null ? absoluteExpiration!.Value @@ -159,7 +190,7 @@ public async Task SetCacheItemAsync(string key, byte[] value, DistributedCacheEn var absoluteExpiration = GetAbsoluteExpiration(utcNow, options); ValidateOptions(options.SlidingExpiration, absoluteExpiration); - await using var connection = new NpgsqlConnection(ConnectionString); + await using var connection = new NpgsqlConnection(_connectionString); var expiresAtTime = options.SlidingExpiration == null ? absoluteExpiration!.Value @@ -185,7 +216,7 @@ private byte[] GetCacheItem(string key, bool includeValue) var utcNow = SystemClock.UtcNow; byte[] value = null; - using var connection = new NpgsqlConnection(ConnectionString); + using var connection = new NpgsqlConnection(_connectionString); var updateCacheItem = new CommandDefinition( SqlCommands.UpdateCacheItemSql, @@ -208,7 +239,7 @@ private async Task GetCacheItemAsync(string key, bool includeValue, Canc var utcNow = SystemClock.UtcNow; byte[] value = null; - await using var connection = new NpgsqlConnection(ConnectionString); + await using var connection = new NpgsqlConnection(_connectionString); var updateCacheItem = new CommandDefinition( SqlCommands.UpdateCacheItemSql, diff --git a/Extensions.Caching.PostgreSql/IDatabaseOperations.cs b/Extensions.Caching.PostgreSql/IDatabaseOperations.cs index 68d529c..559ed97 100644 --- a/Extensions.Caching.PostgreSql/IDatabaseOperations.cs +++ b/Extensions.Caching.PostgreSql/IDatabaseOperations.cs @@ -26,5 +26,6 @@ public interface IDatabaseOperations Task SetCacheItemAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken cancellationToken); Task DeleteExpiredCacheItemsAsync(CancellationToken cancellationToken); + void ChangeConnectionString(string newConnectionString); } } \ No newline at end of file