Azure Cache for Redis 开发的常见问题解答Azure Cache for Redis development FAQs

本文提供有关如何开发 Azure Redis 缓存的常见问题的解答。This article provides answers to common questions about how to develop for Azure Cache for Redis.

常见问题和解答Common questions and answers

本部分包含以下常见问题解答:This section covers the following FAQs:

如何开始使用 Azure Redis 缓存?How can I get started with Azure Cache for Redis?

有几种开始使用 Azure Redis 缓存的方法。There are several ways you can get started with Azure Cache for Redis.

如果还没有 Azure 帐户,可以:If you don't already have an Azure account, you can:

  • 建立 Azure 试用帐户Open an Azure trial account. 获取可用来尝试付费版 Azure 服务的信用额度。You get credits that can be used to try out paid Azure services. 即使在信用额度用完之后,也可以保留帐户并使用免费的 Azure 服务和功能。Even after the credits are used up, you can keep the account and use free Azure services and features.

StackExchange.Redis 配置选项有什么作用?What do the StackExchange.Redis configuration options do?

StackExchange.Redis 有很多选项。StackExchange.Redis has many options. 本部分介绍一些常用设置。This section talks about some of the common settings. 有关 StackExchange.Redis 选项的详细详细,请参阅 StackExchange.Redis 配置For more detailed information about StackExchange.Redis options, see StackExchange.Redis configuration.

配置选项ConfigurationOptions 说明Description 建议Recommendation
AbortOnConnectFailAbortOnConnectFail 如果设置为 true,则发生网络故障后不会重新建立连接。When set to true, the connection will not reconnect after a network failure. 设置为 false,让 StackExchange.Redis 自动重新连接。Set to false and let StackExchange.Redis reconnect automatically.
ConnectRetryConnectRetry 初始连接期间重试连接的次数。The number of times to repeat connection attempts during initial connect. 请参阅下面的注释寻求指导。See the following notes for guidance.
ConnectTimeoutConnectTimeout 连接操作的超时,以毫秒为单位。Timeout in ms for connect operations. 请参阅下面的注释寻求指导。See the following notes for guidance.

通常情况下,使用客户端的默认值便已足够。Usually the default values of the client are sufficient. 可以根据工作负荷微调选项。You can fine-tune the options based on your workload.

  • 重试Retries

    • 对于 ConnectRetry 和 ConnectTimeout,一般指导原则是快速失败并重试。For ConnectRetry and ConnectTimeout, the general guidance is to fail fast and retry again. 该指导原则取决于工作负荷,以及客户端发出 Redis 命令和接收响应平均花费的时间。This guidance is based on your workload and how much time on average it takes for your client to issue a Redis command and receive a response.
    • 让 StackExchange.Redis 自动重新连接,而不是检查连接状态,并由用户自己重新连接。Let StackExchange.Redis automatically reconnect instead of checking connection status and reconnecting yourself. 避免使用 ConnectionMultiplexer.IsConnected 属性Avoid using the ConnectionMultiplexer.IsConnected property.
    • 雪球效应 - 有时可能会遇到这样的问题:不断地重试,但问题不断累积,导致永远无法恢复。Snowballing - sometimes you may run into an issue where you are retrying and the retries snowball and never recovers.
  • 超时值Timeout values

    • 根据工作负荷相应地设置值。Consider your workload and set the values accordingly. 如果要存储较大值,应将超时设置为较大值。If you are storing large values, set the timeout to a higher value.
    • AbortOnConnectFail 设置为 false,让 StackExchange.Redis 重新连接。Set AbortOnConnectFail to false and let StackExchange.Redis reconnect for you.
    • 使用应用程序的单个 ConnectionMultiplexer 实例。Use a single ConnectionMultiplexer instance for the application. 可以使用 LazyConnection 创建 Connection 属性返回的单个实例,如使用 ConnectionMultiplexer 类连接到缓存中所示。You can use a LazyConnection to create a single instance that is returned by a Connection property, as shown in Connect to the cache using the ConnectionMultiplexer class.
    • ConnectionMultiplexer.ClientName 属性设置为应用程序实例的唯一名称以进行诊断。Set the ConnectionMultiplexer.ClientName property to an app instance unique name for diagnostic purposes.
    • 对自定义工作负载使用多个 ConnectionMultiplexer 实例。Use multiple ConnectionMultiplexer instances for custom workloads.
      • 如果应用程序中的负载不同,可以遵循此模型。You can follow this model if you have varying load in your application. 例如:For example:
      • 可以使用一个多路复用器来处理大键。You can have one multiplexer for dealing with large keys.
      • 可以使用一个多路复用器来处理小键。You can have one multiplexer for dealing with small keys.
      • 可为连接超时设置不同的值,并为使用的每个 ConnectionMultiplexer 设置重试逻辑。You can set different values for connection timeouts and retry logic for each ConnectionMultiplexer that you use.
      • 在每个多路复用器上设置 ClientName 属性以帮助进行诊断。Set the ClientName property on each multiplexer to help with diagnostics.
      • 该指导原则可以更好地改进每个 ConnectionMultiplexer的延迟。This guidance may lead to more streamlined latency per ConnectionMultiplexer.

可以使用哪些 Azure Redis 缓存客户端?What Azure Cache for Redis clients can I use?

Redis 的一大优势是有许多客户端,支持许多不同的开发语言。One of the great things about Redis is that there are many clients supporting many different development languages. 如需最新的客户端列表,请参阅 Redis 客户端For a current list of clients, see Redis clients. 有关介绍多种不同语言和客户端的教程,请参阅如何使用 Azure Redis 缓存以及它在内容列表中的同级文章。For tutorials that cover several different languages and clients, see How to use Azure Cache for Redis and it's sibling articles in the table of contents.

从 Azure 门户检索主机名、端口和访问密钥Retrieve host name, ports, and access keys from the Azure portal

若要连接到某个 Azure Cache for Redis 实例,缓存客户端需要该缓存的主机名、端口和密钥。To connect to an Azure Cache for Redis instance, cache clients need the host name, ports, and a key for the cache. 在某些客户端中,这些项的名称可能略有不同。Some clients might refer to these items by slightly different names. 可以从 Azure 门户检索主机名、端口和访问密钥。You can get the host name, ports, and keys from the Azure portal.

  • 若要获取访问密钥,请在缓存的左侧导航中选择“访问密钥”。 To get the access keys, from your cache left navigation, select Access keys.

    Azure Redis 缓存密钥

  • 若要获取主机名和端口,请在缓存的左侧导航栏中选择“属性”。 To get the host name and ports, from your cache left navigation, select Properties. 主机名的格式为 <DNS 名称> 。The host name is of the form <DNS name>

    Azure Redis 缓存属性

Azure Redis 缓存是否有本地模拟器?Is there a local emulator for Azure Cache for Redis?

Azure Redis 缓存没有本地模拟器,但可以在本地计算机上从 Redis 命令行工具运行 MSOpenTech 版本的 redis-server.exe 并连接到它,以获得与本地缓存模拟器相似的体验,如以下示例所示:There is no local emulator for Azure Cache for Redis, but you can run the MSOpenTech version of redis-server.exe from the Redis command-line tools on your local machine and connect to it to get a similar experience to a local cache emulator, as shown in the following example:

private static Lazy<ConnectionMultiplexer>
    lazyConnection = new Lazy<ConnectionMultiplexer> (() =>
        // Connect to a locally running instance of Redis to simulate
        // a local cache emulator experience.
        return ConnectionMultiplexer.Connect("");

public static ConnectionMultiplexer Connection
        return lazyConnection.Value;

如果需要,可以选择配置 redis.conf 文件,以更好地匹配联机 Azure Redis 缓存的默认缓存设置You can optionally configure a redis.conf file to more closely match the default cache settings for your online Azure Cache for Redis if desired.

如何运行 Redis 命令?How can I run Redis commands?

可以使用 Redis 命令中列出的任何命令,但 Azure Redis 缓存中不支持的 Redis 命令所列的命令除外。You can use any of the commands listed at Redis commands except for the commands listed at Redis commands not supported in Azure Cache for Redis. 可以使用多个选项来运行 Redis 命令。You have several options to run Redis commands.

  • 如果有标准或高级缓存,可以使用 Redis 控制台运行 Redis 命令。If you have a Standard or Premium cache, you can run Redis commands using the Redis Console. Redis 控制台提供了一种在 Azure 门户中安全地运行 Redis 命令的方式。The Redis console provides a secure way to run Redis commands in the Azure portal.
  • 还可以使用 Redis 命令行工具。You can also use the Redis command-line tools. 若要使用这些工具,请执行以下步骤:To use them, perform the following steps:
  • 下载 Redis 命令行工具Download the Redis command-line tools.
  • 使用 redis-cli.exe连接到缓存。Connect to the cache using redis-cli.exe. 使用 -h 开关传入缓存终结点,使用 -a 传入密钥,如以下示例中所示:Pass in the cache endpoint using the -h switch and the key using -a as shown in the following example:
  • redis-cli -h <Azure Cache for Redis name> -a <key>


Redis 命令行工具不适用于 TLS 端口,但你可以按照如何将 Redis 命令行工具用于 Azure Cache for Redis 一文中的说明,使用 stunnel 等实用程序将工具安全地连接到 TLS 端口。The Redis command-line tools do not work with the TLS port, but you can use a utility such as stunnel to securely connect the tools to the TLS port by following the directions in the How to use the Redis command-line tool with Azure Cache for Redis article.

Azure Redis 缓存为什么没有 MSDN 类库参考?Why doesn't Azure Cache for Redis have an MSDN class library reference?

Azure Cache for Redis 基于热门开源内存中数据存储 Redis。Azure Cache for Redis is based on the popular open-source in-memory data store, Redis. 它可以通过各种 Redis 客户端 进行访问,这些客户端适用于许多编程语言。It can be accessed by a wide variety of Redis clients for many programming languages. 每个客户端有自身的 API,用于通过 Redis 命令调用 Azure Redis 缓存实例。Each client has its own API that makes calls to the Azure Cache for Redis instance using Redis commands.

由于客户端各不相同,因此 MSDN 上未提供统一的类引用,每个客户端都有自己的参考文档。Because each client is different, there is not one centralized class reference on MSDN, and each client maintains its own reference documentation. 除了参考文档以外,还可以参阅多个教程,这些教程介绍了如何通过不同的语言和缓存客户端来开始使用 Azure Redis 缓存。In addition to the reference documentation, there are several tutorials showing how to get started with Azure Cache for Redis using different languages and cache clients. 若要访问这些教程,请参阅如何使用 Azure Redis 缓存以及它在内容列表中的同级文章。To access these tutorials, see How to use Azure Cache for Redis and it's sibling articles in the table of contents.

是否可将 Azure Redis 缓存用作 PHP 会话缓存?Can I use Azure Cache for Redis as a PHP session cache?

可以。若要使用 Azure Redis 缓存作为 PHP 会话缓存,请在 session.save_path 中指定 Azure Redis 缓存实例的连接字符串。Yes, to use Azure Cache for Redis as a PHP session cache, specify the connection string to your Azure Cache for Redis instance in session.save_path.


使用 Azure Redis 缓存作为 PHP 会话缓存时,必须对用于连接到缓存的安全密钥进行 URL 编码,如以下示例所示:When using Azure Cache for Redis as a PHP session cache, you must URL encode the security key used to connect to the cache, as shown in the following example:

session.save_path = "tcp://<url encoded primary or secondary key here>";

如果未对密钥进行 URL 编码,可能会收到类似于以下消息的异常: Failed to parse session.save_pathIf the key is not URL encoded, you may receive an exception with a message like: Failed to parse session.save_path

有关在 PhpRedis 客户端中使用 Azure Redis 缓存作为 PHP 会话缓存的详细信息,请参阅 PHP Session handler(PHP 会话处理程序)。For more information about using Azure Cache for Redis as a PHP session cache with the PhpRedis client, see PHP Session handler.

什么是 Redis 数据库?What are Redis databases?

Redis 数据库就是同一 Redis 实例中的数据的逻辑隔离。Redis Databases are just a logical separation of data within the same Redis instance. 缓存内存在所有数据库之间共享,给定数据库的实际内存消耗取决于该数据库中存储的键/值。The cache memory is shared between all the databases and actual memory consumption of a given database depends on the keys/values stored in that database. 例如,C6 缓存的内存为 53 GB,P5 的内存为 120 GB。For example, a C6 cache has 53 GB of memory, and a P5 has 120 GB. 可以选择将全部 53 GB/120 GB 放置在一个数据库中,也可以将其分配到多个数据库中。You can choose to put all 53 GB / 120 GB into one database or you can split it up between multiple databases.


使用启用了群集功能的高级 Azure Redis 缓存时,仅数据库 0 可用。When using a Premium Azure Cache for Redis with clustering enabled, only database 0 is available. 此限制是固有的 Redis 限制,并不特定于 Azure Redis 缓存。This limitation is an intrinsic Redis limitation and is not specific to Azure Cache for Redis. 有关详细信息,请参阅使用群集功能时,是否需要对客户端应用程序进行更改?For more information, see Do I need to make any changes to my client application to use clustering?.

后续步骤Next steps

了解其他 Azure Cache for Redis 常见问题解答Learn about other Azure Cache for Redis FAQs.