【Azure Redis 缓存】Azure Cache for Redis 如何迁移

Azure Cache for Redis 如何迁移

【Azure Redis 缓存】Azure Cache for Redis有默认备份可以用于恢复么?一文中,介绍了使用RDB文件的方式来迁移Redis中的数据或者事备份恢复。与此同时,在Azure Cache for Redis的官方文档描述中,也列举出了有一下三种方式:

  1. 将数据导出到 RDB 文件并将该文件导入 Azure Cache for Redis: 此方法需要在Redis高级版中完成。https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-migration-guide#export-data-to-an-rdb-file-and-import-it-into-azure-cache-for-redis
  2. 在迁移期间同时写入两个 Redis 缓存: 可以使用应用程序将数据写入现有缓存和要设置的新缓存,而不是直接在缓存之间移动数据。 应用程序一开始仍将从现有缓存中读取数据。 当新缓存拥有必要的数据后,就可以将应用程序切换到该缓存,然后停用旧缓存。https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-migration-guide#write-to-two-redis-caches-simultaneously-during-migration-period
  3. 以编程方式迁移可以通过编程方式从现有缓存中读取数据并将其写入 Azure Cache for Redis,从而创建自定义迁移过程。https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-migration-guide#migrate-programmatically

开源工具下载地址:https://github.com/deepakverma/redis-copy 最核心的代码为:

    await sourcedb.KeyDumpAsync(key).ContinueWith(dump =>
       {
         if (dump.IsFaulted || dump.IsCanceled)
          {
             throw new AggregateException(dump.Exception);
          }
          else
          {
          //Redis > 3.0, if key already exists it won't overwrite
             destdb.KeyRestoreAsync(key, dump.Result, ttl.Result).ContinueWith(restore =>
             {
               Interlocked.Increment(ref totalKeysCopied);
                percent = ((double)totalKeysCopied / totalKeysSource) * 100;
                TasksInProgress[source] = percent;
             });
          }
       });

其最核心的思想使用的是Redis DUMPRESTORE命令。 

DUMP  key:

Serialize the value stored at key in a Redis-specific format and return it to the user. The returned value can be synthesized back into a Redis key using the RESTORE command.

Redis DUMP 命令用于序列化给定 key ,并返回被序列化的值。

RESTORE key

 Create a key associated with a value that is obtained by deserializing the provided serialized value (obtained via DUMP).

参考资料

迁移到 Azure Cache for Redis:https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-migration-guide#migrate-programmatically

Redis 数据备份与恢复: https://www.runoob.com/redis/redis-backup.html

原文地址:https://www.cnblogs.com/lulight/p/14332237.html