Skip to main content
In this guide, you’ll learn how to connect to Azure Cache for Redis using .NET. We will demonstrate how to create a secure connection using the StackExchange.Redis library, access the database, execute basic key-value operations, and explore advanced Redis commands for more complex caching scenarios.

Establishing a Secure Connection

Begin by creating a connection to your Azure Cache for Redis instance. Your connection string includes the Redis Cache name, password, and SSL settings to ensure a secure connection—similar to configuring a connection in the Redis CLI. Once connected, you can access a specific database and perform operations. For instance, the code below shows how to store and retrieve a key-value pair using the StringSet and StringGet methods:
The StackExchange.Redis library is widely used in .NET applications for seamless integration with Redis caches. Ensure your connection string is secure and properly stored.

Advanced Commands and Operations

Beyond basic operations, Redis offers several advanced commands that are beneficial for complex caching scenarios:
  • Create Batch: Groups multiple operations and sends them as a single unit to the Redis server. (Note that these operations are not executed as a transaction.)
  • Create Transaction: Ensures that grouped operations are executed atomically—all commands succeed or none are applied.
  • Key Delete: Removes a key-value pair from the cache, which is useful for cleaning outdated or unnecessary data.
  • Key Exists: Checks if a specific key exists, returning a Boolean result to verify data presence before further operations.
  • Key Expire: Sets a Time-To-Live (TTL) for a key. Once the TTL expires, the key is automatically removed—ideal for managing data lifecycles.
  • Key Rename: Renames an existing key without affecting the stored data.
  • Key Time To Live: Retrieves the remaining TTL for a key. A return value of -1 indicates the key will persist indefinitely.
  • Key Type: Identifies the data type stored at a specified key, assisting in managing various data structures such as strings, lists, and sets.
The image is a table describing methods for interacting with Azure Cache for Redis using .NET, including method names and their descriptions.
These commands provide robust control over your Redis cache, from handling key expirations to managing operations within batches or transactions.

A Complete Example

Below is a complete example that walks you through:
  1. Connecting to Azure Cache for Redis using a connection string.
  2. Generating a unique key with a GUID.
  3. Prompting the user to enter a value.
  4. Storing the value in the cache with a TTL of 30 seconds.
  5. Retrieving and displaying the stored value.
  6. Waiting for the key to expire and checking its status.
The expected console output during execution might look like this:
Ensure proper error handling and secure management of connection strings in production environments. Avoid exposing sensitive information in your source code.

Verifying Data with Redis CLI

You can verify data storage and expiration using the Redis CLI. For example, to list keys and retrieve a specific key’s value:
After the TTL expires, running the get command for the same key will return nil, indicating the key has expired. This concludes our walkthrough on interacting with Azure Cache for Redis using .NET. Next, we’ll explore another popular caching solution in Azure: Azure CDN. For more information, you may find these resources useful:

Watch Video