您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

用于 .NET 的 Azure Redis 缓存库Azure Redis Cache libraries for .NET

概述Overview

Azure Redis 缓存是一个安全的数据缓存和消息传送中转站,可让应用程序以较高的吞吐量、较低的延迟访问数据。Azure Redis Cache is a secure data cache and messaging broker that provides high throughput and low-latency access to data for applications. 有关详细信息,请参阅如何使用 Redis 缓存For more information, see How to Use Redis Cache.

客户端库Client library

Azure Redis 缓存与任何 Redis 客户端 API(包括 StackExchange.Redis)兼容。Azure Redis Cache is compatible with any Redis client API, including StackExchange.Redis.

直接从 Visual Studio 包管理器控制台或使用 .NET Core CLI 安装 NuGet 包Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

Visual Studio 包管理器Visual Studio Package Manager

Install-Package StackExchange.Redis
dotnet add package StackExchange.Redis

示例Example

此示例连接到 Redis 缓存数据库实例,根据名称将一些字符串添加到缓存,然后再次检索这些字符串。This example connects to a Redis Cache database instance, adds some strings to the cache by name, and then retrieves them again.

/* Include this "using" directive.
using StackExchange.Redis;
*/

ConnectionMultiplexer connection = 
    ConnectionMultiplexer.Connect("contoso.redis.cache.windows.net,abortConnect=false,ssl=true,password=...");
    IDatabase cache = connection.GetDatabase();

// Perform cache operations using the cache object...
// Simple put of integral data types into the cache
cache.StringSet("key1", "value");
cache.StringSet("key2", 25);

// Simple get of data types from the cache
string key1 = cache.StringGet("key1");
int key2 = (int)cache.StringGet("key2");

管理库Management library

使用 Redis 缓存管理库可以管理 Redis 缓存资源和访问密钥。The Redis Cache management library allows you to manage Redis Cache resources and access keys.

直接从 Visual Studio 包管理器控制台或使用 .NET Core CLI 安装 NuGet 包Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

Visual Studio 包管理器Visual Studio Package Manager

Install-Package Microsoft.Azure.Management.Redis.Fluent
dotnet add package Microsoft.Azure.Management.Redis.Fluent

示例Example

此示例创建新的 Redis 缓存。This example creates a new Redis Cache.

/* Include these "using" directives...
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.Redis.Fluent;
*/

IRedisCache redisCache1 = azure.RedisCaches.Define("RedisCacheName")
    .WithRegion(Region.USCentral)
    .WithNewResourceGroup("ResourceGroupName")
    .WithBasicSku()
    .Create();

示例Samples