在部署期间使用 PowerShell 清除 Azure Redis 缓存
Clearing Azure Redis Cache using PowerShell during deployment
在将我们的 Web 应用程序的新版本部署到 Azure 应用服务时,我需要清除关联的 Azure Redis 缓存中的数据。这是为了确保我们不会 return 旧版本的项目在新版本中有架构更改。
我们正在使用 Octopus Deploy 进行部署,我之前曾尝试执行以下 PowerShell 命令来重置缓存:
Reset-AzureRmRedisCache -ResourceGroupName "$ResourceGroup" -Name "$PrimaryCacheName" -RebootType "AllNodes" -Force
这很成功,但有点笨手笨脚,我们遇到间歇性连接问题,我怀疑这是由于我们正在重新启动 Redis 并删除现有连接。
理想情况下,我只想通过 PowerShell 执行 FLUSHALL
命令。这是更好的方法吗,是否可以使用 StackExchange.Redis 库在 PowerShell 中执行?
Reset-AzureRmRedisCache cmdlet 重新启动 Azure Redis 缓存实例的节点,我同意这对您的要求来说有点矫枉过正。
Yes, it is possible to execute a Redis FLUSHALL command in PowerShell.
作为先决条件,您应该安装 Redis CLI 并设置一个环境变量以指向您环境中的 Redis CLI executable/binary 路径。
然后,您可以使用 Redis-CLI 命令在 PowerShell 中执行,如下所示。
Invoke-Command -ScriptBlock { redis-cli -h <hostname>.redis.cache.windows.net -p <redisPort> -a <password> }
Invoke-Command -ScriptBlock { redis-cli flushall }
上面代码示例的执行结果如下图:
我已经使用 netcat 的 Windows 端口从我的 Windows 机器远程清除 Redis 缓存,如下所示:
$redisCommands = "SELECT $redisDBIndex`r`nFLUSHDB`r`nQUIT`r`n"
$redisCommands | .\nc $redisServer 6379
其中$redisDBIndex
是你要清除的Redis缓存索引。或者,如果您想清除所有内容,只需使用命令 FLAUSHALL
。 $redisServer
是您的 Redis 服务器。并简单地通过管道传输到 nc.
我也记录在这里:https://jaeyow.github.io/fullstack-developer/automate-redis-cache-flush-in-powershell/#
我最终实现它的方法是通过 PowerShell 调用 StackExchange.Redis 库,因此您需要在方便的地方拥有此 DLL 的副本。在我的部署过程中,我可以访问连接字符串,因此此函数会剥离主机和端口以连接到服务器。这无需打开 non-SSL 端口即可工作,并且连接字符串允许管理员访问缓存:
function FlushCache($RedisConnString)
{
# Extract the Host/Port from the start of the connection string (ignore the remainder)
# e.g. MyUrl.net:6380,password=abc123,ssl=True,abortConnect=False
$hostAndPort = $RedisConnString.Substring(0, $RedisConnString.IndexOf(","))
# Split the Host and Port e.g. "MyUrl.net:6380" --> ["MyUrl.net", "6380"]
$RedisCacheHost, $RedisCachePort = $hostAndPort.split(':')
Write-Host "Flushing cache on host - $RedisCacheHost - Port $RedisCachePort" -ForegroundColor Yellow
# Add the Redis type from the assembly
$asm = [System.Reflection.Assembly]::LoadFile("StackExchange.Redis.dll")
# Open a connection
[object]$redis_cache = [StackExchange.Redis.ConnectionMultiplexer]::Connect("$RedisConnString,allowAdmin=true",$null)
# Flush the cache
$redisServer = $redis_cache.GetServer($RedisCacheHost, $RedisCachePort,$null)
$redisServer.FlushAllDatabases()
# Dispose connection
$redis_cache.Dispose()
Write-Host "Cache flush done" -ForegroundColor Yellow
}
在将我们的 Web 应用程序的新版本部署到 Azure 应用服务时,我需要清除关联的 Azure Redis 缓存中的数据。这是为了确保我们不会 return 旧版本的项目在新版本中有架构更改。
我们正在使用 Octopus Deploy 进行部署,我之前曾尝试执行以下 PowerShell 命令来重置缓存:
Reset-AzureRmRedisCache -ResourceGroupName "$ResourceGroup" -Name "$PrimaryCacheName" -RebootType "AllNodes" -Force
这很成功,但有点笨手笨脚,我们遇到间歇性连接问题,我怀疑这是由于我们正在重新启动 Redis 并删除现有连接。
理想情况下,我只想通过 PowerShell 执行 FLUSHALL
命令。这是更好的方法吗,是否可以使用 StackExchange.Redis 库在 PowerShell 中执行?
Reset-AzureRmRedisCache cmdlet 重新启动 Azure Redis 缓存实例的节点,我同意这对您的要求来说有点矫枉过正。
Yes, it is possible to execute a Redis FLUSHALL command in PowerShell.
作为先决条件,您应该安装 Redis CLI 并设置一个环境变量以指向您环境中的 Redis CLI executable/binary 路径。
然后,您可以使用 Redis-CLI 命令在 PowerShell 中执行,如下所示。
Invoke-Command -ScriptBlock { redis-cli -h <hostname>.redis.cache.windows.net -p <redisPort> -a <password> }
Invoke-Command -ScriptBlock { redis-cli flushall }
上面代码示例的执行结果如下图:
我已经使用 netcat 的 Windows 端口从我的 Windows 机器远程清除 Redis 缓存,如下所示:
$redisCommands = "SELECT $redisDBIndex`r`nFLUSHDB`r`nQUIT`r`n"
$redisCommands | .\nc $redisServer 6379
其中$redisDBIndex
是你要清除的Redis缓存索引。或者,如果您想清除所有内容,只需使用命令 FLAUSHALL
。 $redisServer
是您的 Redis 服务器。并简单地通过管道传输到 nc.
我也记录在这里:https://jaeyow.github.io/fullstack-developer/automate-redis-cache-flush-in-powershell/#
我最终实现它的方法是通过 PowerShell 调用 StackExchange.Redis 库,因此您需要在方便的地方拥有此 DLL 的副本。在我的部署过程中,我可以访问连接字符串,因此此函数会剥离主机和端口以连接到服务器。这无需打开 non-SSL 端口即可工作,并且连接字符串允许管理员访问缓存:
function FlushCache($RedisConnString)
{
# Extract the Host/Port from the start of the connection string (ignore the remainder)
# e.g. MyUrl.net:6380,password=abc123,ssl=True,abortConnect=False
$hostAndPort = $RedisConnString.Substring(0, $RedisConnString.IndexOf(","))
# Split the Host and Port e.g. "MyUrl.net:6380" --> ["MyUrl.net", "6380"]
$RedisCacheHost, $RedisCachePort = $hostAndPort.split(':')
Write-Host "Flushing cache on host - $RedisCacheHost - Port $RedisCachePort" -ForegroundColor Yellow
# Add the Redis type from the assembly
$asm = [System.Reflection.Assembly]::LoadFile("StackExchange.Redis.dll")
# Open a connection
[object]$redis_cache = [StackExchange.Redis.ConnectionMultiplexer]::Connect("$RedisConnString,allowAdmin=true",$null)
# Flush the cache
$redisServer = $redis_cache.GetServer($RedisCacheHost, $RedisCachePort,$null)
$redisServer.FlushAllDatabases()
# Dispose connection
$redis_cache.Dispose()
Write-Host "Cache flush done" -ForegroundColor Yellow
}