在部署期间使用PowerShell清除Azure Redis缓存

在部署期间使用PowerShell清除Azure Redis缓存,azure,redis,azure-powershell,stackexchange.redis,redis-cli,Azure,Redis,Azure Powershell,Stackexchange.redis,Redis Cli,在将新版本的web应用程序部署到Azure应用程序服务时,我需要清除相关Azure Redis缓存中的数据。这是为了确保我们不会返回在新版本中有架构更改的项目的旧版本 我们正在使用Octopus Deploy进行部署,我以前尝试过执行以下PowerShell命令来重置缓存: Reset-AzureRmRedisCache -ResourceGroupName "$ResourceGroup" -Name "$PrimaryCacheName" -RebootType "AllNodes" -Fo

在将新版本的web应用程序部署到Azure应用程序服务时,我需要清除相关Azure Redis缓存中的数据。这是为了确保我们不会返回在新版本中有架构更改的项目的旧版本

我们正在使用Octopus Deploy进行部署,我以前尝试过执行以下PowerShell命令来重置缓存:

Reset-AzureRmRedisCache -ResourceGroupName "$ResourceGroup" -Name "$PrimaryCacheName" -RebootType "AllNodes" -Force
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
}
这是成功的,但有点笨手笨脚,我们有间歇性的连接问题,我怀疑这是由于我们重新启动Redis并删除现有连接造成的


理想情况下,我只想通过PowerShell执行
FLUSHALL
命令。这是一种更好的方法吗?是否可以使用StackExchange.Redis库在PowerShell中执行

Reset-AzureRmRedisCachecmdlet重新启动Azure Redis缓存实例的节点,我同意这对于您的要求来说有点过分

可以,可以在PowerShell中执行Redis FLUSHALL命令

作为先决条件,您应该安装并设置环境变量,以指向环境中的Redis CLI可执行文件/二进制路径

然后,您可以使用Redis CLI命令在PowerShell中执行,如下所示

Invoke-Command -ScriptBlock { redis-cli -h <hostname>.redis.cache.windows.net -p <redisPort> -a <password> }
Invoke-Command -ScriptBlock { redis-cli flushall }
Invoke命令-ScriptBlock{redis cli-h.redis.cache.windows.net-p-a}
调用命令-ScriptBlock{redis cli flushall}
上面代码示例的执行结果如下所示:

我使用netcat的Windows端口从我的Windows机器上远程清除Redis缓存,如下所示:

$redisCommands = "SELECT $redisDBIndex`r`nFLUSHDB`r`nQUIT`r`n"
$redisCommands | .\nc $redisServer 6379
其中,
$redidbIndex
是要清除的Redis缓存索引。或者,如果要清除所有内容,只需使用命令
FLAUSHALL
$rediserver
是您的Redis服务器。只需将管道连接到nc即可


我也在这里记录了它:

我最终实现它的方式是通过PowerShell调用StackExchange.Redis库,因此您需要在方便的地方拥有此DLL的副本。在部署过程中,我可以访问连接字符串,因此此函数将剥离主机和端口以连接到服务器。无需打开非SSL端口,连接字符串允许管理员访问缓存:

Reset-AzureRmRedisCache -ResourceGroupName "$ResourceGroup" -Name "$PrimaryCacheName" -RebootType "AllNodes" -Force
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
}

我也需要安装一些库吗?powershell中的命令返回OK作为响应,但当我检查Azure portal中的Redis控制台时,缓存仍有值。这仅在启用非SSL端口时才有效。如果您只有SSL端口,它将不起作用。StackExtange.Redis库位于此处: