Skip to content

Commit 91be2c8

Browse files
authored
Merge pull request #35908 from dotnet/main
Merge to Live
2 parents b903df9 + 7db6835 commit 91be2c8

File tree

11 files changed

+240
-15
lines changed

11 files changed

+240
-15
lines changed

aspnetcore/client-side/dotnet-interop.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,6 @@ In the preceding example, the `{TARGET FRAMEWORK}` placeholder is the [target fr
468468
* [.NET WebAssembly runtime](https://github.com/dotnet/runtime/tree/main/src/mono/wasm)
469469
* [`dotnet.d.ts` file (.NET WebAssembly runtime configuration)](https://github.com/dotnet/runtime/blob/main/src/mono/browser/runtime/dotnet.d.ts)
470470
* [Use .NET from any JavaScript app in .NET 7](https://devblogs.microsoft.com/dotnet/use-net-7-from-any-javascript-app-in-net-7/)
471+
* Including static assets from a Razor class library
472+
* <xref:blazor/components/class-libraries> (Blazor documentation)
473+
* <xref:razor-pages/ui-class#create-an-rcl-with-static-assets> (Razor Pages documentation)

aspnetcore/fundamentals/app-state.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Session state exhibits the following behaviors:
8585
8686
The in-memory cache provider stores session data in the memory of the server where the app resides. In a server farm scenario:
8787

88-
* Use *sticky sessions* to tie each session to a specific app instance on an individual server. [Azure App Service](https://azure.microsoft.com/services/app-service/) uses [Application Request Routing (ARR)](/iis/extensions/planning-for-arr/using-the-application-request-routing-module) to enforce sticky sessions by default. However, sticky sessions can affect scalability and complicate web app updates. A better approach is to use a Redis or SQL Server distributed cache, which doesn't require sticky sessions. For more information, see <xref:performance/caching/distributed>.
88+
* Use *sticky sessions* to tie each session to a specific app instance on an individual server. [Azure App Service](https://azure.microsoft.com/services/app-service/) uses [Application Request Routing (ARR)](/iis/extensions/planning-for-arr/using-the-application-request-routing-module) to enforce sticky sessions by default. However, sticky sessions can affect scalability and complicate web app updates. A better approach is to use a Redis, SQL Server, or Azure Postgres distributed cache, which doesn't require sticky sessions. For more information, see <xref:performance/caching/distributed>.
8989
* The session cookie is encrypted via <xref:Microsoft.AspNetCore.DataProtection.IDataProtector>. Data Protection must be properly configured to read session cookies on each machine. For more information, see <xref:security/data-protection/introduction> and [Key storage providers](xref:security/data-protection/implementation/key-storage-providers).
9090

9191
### Configure session state
@@ -372,7 +372,7 @@ Session state exhibits the following behaviors:
372372
373373
The in-memory cache provider stores session data in the memory of the server where the app resides. In a server farm scenario:
374374

375-
* Use *sticky sessions* to tie each session to a specific app instance on an individual server. [Azure App Service](https://azure.microsoft.com/services/app-service/) uses [Application Request Routing (ARR)](/iis/extensions/planning-for-arr/using-the-application-request-routing-module) to enforce sticky sessions by default. However, sticky sessions can affect scalability and complicate web app updates. A better approach is to use a Redis or SQL Server distributed cache, which doesn't require sticky sessions. For more information, see <xref:performance/caching/distributed>.
375+
* Use *sticky sessions* to tie each session to a specific app instance on an individual server. [Azure App Service](https://azure.microsoft.com/services/app-service/) uses [Application Request Routing (ARR)](/iis/extensions/planning-for-arr/using-the-application-request-routing-module) to enforce sticky sessions by default. However, sticky sessions can affect scalability and complicate web app updates. A better approach is to use a Redis, SQL Server, or Azure Postgres distributed cache, which doesn't require sticky sessions. For more information, see <xref:performance/caching/distributed>.
376376
* The session cookie is encrypted via <xref:Microsoft.AspNetCore.DataProtection.IDataProtector>. Data Protection must be properly configured to read session cookies on each machine. For more information, see <xref:security/data-protection/introduction> and [Key storage providers](xref:security/data-protection/implementation/key-storage-providers).
377377

378378
### Configure session state

aspnetcore/performance/caching/distributed.md

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ When cached data is distributed, the data:
2727
* Survives server restarts and app deployments.
2828
* Doesn't use local memory.
2929

30-
Distributed cache configuration is implementation specific. This article describes how to configure SQL Server and Redis distributed caches. Third party implementations are also available, such as [NCache](http://www.alachisoft.com/ncache/aspnet-core-idistributedcache-ncache.html) ([NCache on GitHub](https://github.com/Alachisoft/NCache)). Regardless of which implementation is selected, the app interacts with the cache using the <xref:Microsoft.Extensions.Caching.Distributed.IDistributedCache> interface.
30+
Distributed cache configuration is implementation specific. This article describes how to configure SQL Server, Redis, or Postgres distributed caches. Third party implementations are also available, such as [NCache](http://www.alachisoft.com/ncache/aspnet-core-idistributedcache-ncache.html) ([NCache on GitHub](https://github.com/Alachisoft/NCache)), Cosmos DB, and Postgres. Regardless of which implementation is selected, the app interacts with the cache using the <xref:Microsoft.Extensions.Caching.Distributed.IDistributedCache> interface.
3131

3232
[View or download sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/performance/caching/distributed/samples/) ([how to download](xref:index#how-to-download-a-sample))
3333

@@ -39,6 +39,8 @@ Add a package reference for the distributed cache provider used:
3939

4040
* For a Redis distributed cache, [Microsoft.Extensions.Caching.StackExchangeRedis](https://www.nuget.org/packages/Microsoft.Extensions.Caching.StackExchangeRedis).
4141
* For SQL Server, [Microsoft.Extensions.Caching.SqlServer](https://www.nuget.org/packages/Microsoft.Extensions.Caching.SqlServer).
42+
* For Postgres, [Microsoft.Extensions.Caching.Postgres](https://www.nuget.org/packages/Microsoft.Extensions.Caching.Postgres).
43+
* For Cosmos DB, [Microsoft.Extensions.Caching.Cosmos](https://www.nuget.org/packages/Microsoft.Extensions.Caching.Cosmos).
4244
* For the NCache distributed cache, [NCache.Microsoft.Extensions.Caching.OpenSource](https://www.nuget.org/packages/NCache.Microsoft.Extensions.Caching.OpenSource).
4345

4446
## IDistributedCache interface
@@ -57,8 +59,9 @@ Register an implementation of <xref:Microsoft.Extensions.Caching.Distributed.IDi
5759
* [Distributed Redis cache](#distributed-redis-cache)
5860
* [Distributed Memory Cache](#distributed-memory-cache)
5961
* [Distributed SQL Server cache](#distributed-sql-server-cache)
62+
* [Distributed Postgres cache](#distributed-postgres-cache)
6063
* [Distributed NCache cache](#distributed-ncache-cache)
61-
* [Distributed Azure CosmosDB cache](#distributed-azure-cosmosdb-cache)
64+
* [Distributed Azure Cosmos DB cache](#distributed-azure-cosmos-db-cache)
6265

6366
### Distributed Redis Cache
6467

@@ -126,6 +129,75 @@ The sample app implements <xref:Microsoft.Extensions.Caching.SqlServer.SqlServer
126129
> [!NOTE]
127130
> A <xref:Microsoft.Extensions.Caching.SqlServer.SqlServerCacheOptions.ConnectionString*> (and optionally, <xref:Microsoft.Extensions.Caching.SqlServer.SqlServerCacheOptions.SchemaName*> and <xref:Microsoft.Extensions.Caching.SqlServer.SqlServerCacheOptions.TableName*>) are typically stored outside of source control (for example, stored by the [Secret Manager](xref:security/app-secrets) or in `appsettings.json`/`appsettings.{Environment}.json` files). The connection string may contain credentials that should be kept out of source control systems.
128131
132+
### Distributed Postgres Cache
133+
134+
[Azure Database for PostgreSQL](/azure/postgresql) can be used as a distributed cache backing store via the `IDistributedCache` interface. Azure Database for PostgreSQL is a fully managed, AI-ready Database-as-a-Service (DBaaS) offering built on the open-source PostgreSQL engine, designed to support mission-critical workloads with predictable performance, robust security, high availability, and seamless scalability.
135+
136+
After installing the [Microsoft.Extensions.Caching.Postgres](https://www.nuget.org/packages/Microsoft.Extensions.Caching.Postgres) NuGet package, configure your distributed cache as follows:
137+
138+
1. Register the Service
139+
140+
```csharp
141+
using Microsoft.Extensions.DependencyInjection;
142+
143+
var builder = WebApplication.CreateBuilder(args);
144+
145+
// Register Postgres distributed cache
146+
builder.Services.AddDistributedPostgresCache(options => {
147+
options.ConnectionString = builder.Configuration.GetConnectionString("PostgresCache");
148+
options.SchemaName = builder.Configuration.GetValue<string>("PostgresCache:SchemaName", "public");
149+
options.TableName = builder.Configuration.GetValue<string>("PostgresCache:TableName", "cache");
150+
options.CreateIfNotExists = builder.Configuration.GetValue<bool>("PostgresCache:CreateIfNotExists", true);
151+
options.UseWAL = builder.Configuration.GetValue<bool>("PostgresCache:UseWAL", false);
152+
153+
// Optional: Configure expiration settings
154+
155+
var expirationInterval = builder.Configuration.GetValue<string>("PostgresCache:ExpiredItemsDeletionInterval");
156+
if (!string.IsNullOrEmpty(expirationInterval) && TimeSpan.TryParse(expirationInterval, out var interval)) {
157+
options.ExpiredItemsDeletionInterval = interval;
158+
}
159+
160+
var slidingExpiration = builder.Configuration.GetValue<string>("PostgresCache:DefaultSlidingExpiration");
161+
if (!string.IsNullOrEmpty(slidingExpiration) && TimeSpan.TryParse(slidingExpiration, out var sliding)) {
162+
options.DefaultSlidingExpiration = sliding;
163+
}
164+
});
165+
166+
var app = builder.Build();
167+
```
168+
169+
2. Use the Cache
170+
171+
```csharp
172+
public class MyService {
173+
private readonly IDistributedCache _cache;
174+
175+
public MyService(IDistributedCache cache) {
176+
_cache = cache;
177+
}
178+
179+
public async Task<string> GetDataAsync(string key) {
180+
var cachedData = await _cache.GetStringAsync(key);
181+
182+
if (cachedData == null) {
183+
// Fetch data from source
184+
var data = await FetchDataFromSource();
185+
186+
// Cache the data with options
187+
var options = new DistributedCacheEntryOptions {
188+
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30),
189+
SlidingExpiration = TimeSpan.FromMinutes(5)
190+
};
191+
192+
await _cache.SetStringAsync(key, data, options);
193+
return data;
194+
}
195+
196+
return cachedData;
197+
}
198+
}
199+
```
200+
129201
### Distributed NCache Cache
130202

131203
[NCache](https://github.com/Alachisoft/NCache) is an open source in-memory distributed cache developed natively in .NET. NCache works both locally and configured as a distributed cache cluster for an ASP.NET Core app running in Azure or on other hosting platforms.
@@ -140,7 +212,7 @@ To configure NCache:
140212

141213
[!code-csharp[](~/performance/caching/distributed/samples/6.x/DistCacheSample/Program.cs?name=snippet_AddNCache_Cache)]
142214

143-
### Distributed Azure CosmosDB Cache
215+
### Distributed Azure Cosmos DB Cache
144216

145217
[Azure Cosmos DB](/azure/cosmos-db/introduction) can be used in ASP.NET Core as a session state provider by using the `IDistributedCache` interface. Azure Cosmos DB is a fully managed NoSQL and relational database for modern app development that offers high availability, scalability, and low-latency access to data for mission-critical applications.
146218

@@ -213,6 +285,7 @@ When SQL Server is used as a distributed cache backing store, use of the same da
213285

214286
* [Redis Cache on Azure](/azure/azure-cache-for-redis/)
215287
* [SQL Database on Azure](/azure/sql-database/)
288+
* [Azure Database for PostgreSQL](/azure/postgresql/)
216289
* [ASP.NET Core IDistributedCache Provider for NCache in Web Farms](http://www.alachisoft.com/ncache/aspnet-core-idistributedcache-ncache.html) ([NCache on GitHub](https://github.com/Alachisoft/NCache))
217290
* [Repository README file for Microsoft.Extensions.Caching.Cosmos](https://github.com/Azure/Microsoft.Extensions.Caching.Cosmos/blob/master/README.md)
218291
* <xref:performance/caching/memory>

aspnetcore/performance/caching/distributed/includes/distributed5.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ When cached data is distributed, the data:
1111
* Survives server restarts and app deployments.
1212
* Doesn't use local memory.
1313

14-
Distributed cache configuration is implementation specific. This article describes how to configure SQL Server and Redis distributed caches. Third party implementations are also available, such as [NCache](http://www.alachisoft.com/ncache/aspnet-core-idistributedcache-ncache.html) ([NCache on GitHub](https://github.com/Alachisoft/NCache)). Regardless of which implementation is selected, the app interacts with the cache using the <xref:Microsoft.Extensions.Caching.Distributed.IDistributedCache> interface.
14+
Distributed cache configuration is implementation specific. This article describes how to configure SQL Server, Redis, and Postgres distributed caches. Third party implementations are also available, such as [NCache](http://www.alachisoft.com/ncache/aspnet-core-idistributedcache-ncache.html) ([NCache on GitHub](https://github.com/Alachisoft/NCache)). Regardless of which implementation is selected, the app interacts with the cache using the <xref:Microsoft.Extensions.Caching.Distributed.IDistributedCache> interface.
1515

1616
[View or download sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/performance/caching/distributed/samples/) ([how to download](xref:index#how-to-download-a-sample))
1717

@@ -21,6 +21,8 @@ To use a SQL Server distributed cache, add a package reference to the [Microsoft
2121

2222
To use a Redis distributed cache, add a package reference to the [Microsoft.Extensions.Caching.StackExchangeRedis](https://www.nuget.org/packages/Microsoft.Extensions.Caching.StackExchangeRedis) package.
2323

24+
To use a Postgres distributed cache, add a package reference to the [Microsoft.Extensions.Caching.Postgres](https://www.nuget.org/packages/Microsoft.Extensions.Caching.Postgres) package.
25+
2426
To use NCache distributed cache, add a package reference to the [NCache.Microsoft.Extensions.Caching.OpenSource](https://www.nuget.org/packages/NCache.Microsoft.Extensions.Caching.OpenSource) package.
2527

2628
## IDistributedCache interface
@@ -39,6 +41,7 @@ Register an implementation of <xref:Microsoft.Extensions.Caching.Distributed.IDi
3941
* [Distributed Memory Cache](#distributed-memory-cache)
4042
* [Distributed SQL Server cache](#distributed-sql-server-cache)
4143
* [Distributed Redis cache](#distributed-redis-cache)
44+
* [Distributed Postgres cache](#distributed-postgres-cache)
4245
* [Distributed NCache cache](#distributed-ncache-cache)
4346

4447
### Distributed Memory Cache
@@ -105,6 +108,75 @@ For more information, see [Azure Cache for Redis](/azure/azure-cache-for-redis/c
105108

106109
See [this GitHub issue](https://github.com/dotnet/AspNetCore.Docs/issues/19542) for a discussion on alternative approaches to a local Redis cache.
107110

111+
### Distributed Postgres Cache
112+
113+
[Azure Database for PostgreSQL](/azure/postgresql) can be used as a distributed cache backing store via the `IDistributedCache` interface. Azure Database for PostgreSQL is a fully managed, AI-ready Database-as-a-Service (DBaaS) offering built on the open-source PostgreSQL engine, designed to support mission-critical workloads with predictable performance, robust security, high availability, and seamless scalability.
114+
115+
After installing the [Microsoft.Extensions.Caching.Postgres](https://www.nuget.org/packages/Microsoft.Extensions.Caching.Postgres) NuGet package, configure your distributed cache as follows:
116+
117+
1. Register the Service
118+
119+
```csharp
120+
using Microsoft.Extensions.DependencyInjection;
121+
122+
var builder = WebApplication.CreateBuilder(args);
123+
124+
// Register Postgres distributed cache
125+
builder.Services.AddDistributedPostgresCache(options => {
126+
options.ConnectionString = builder.Configuration.GetConnectionString("PostgresCache");
127+
options.SchemaName = builder.Configuration.GetValue<string>("PostgresCache:SchemaName", "public");
128+
options.TableName = builder.Configuration.GetValue<string>("PostgresCache:TableName", "cache");
129+
options.CreateIfNotExists = builder.Configuration.GetValue<bool>("PostgresCache:CreateIfNotExists", true);
130+
options.UseWAL = builder.Configuration.GetValue<bool>("PostgresCache:UseWAL", false);
131+
132+
// Optional: Configure expiration settings
133+
134+
var expirationInterval = builder.Configuration.GetValue<string>("PostgresCache:ExpiredItemsDeletionInterval");
135+
if (!string.IsNullOrEmpty(expirationInterval) && TimeSpan.TryParse(expirationInterval, out var interval)) {
136+
options.ExpiredItemsDeletionInterval = interval;
137+
}
138+
139+
var slidingExpiration = builder.Configuration.GetValue<string>("PostgresCache:DefaultSlidingExpiration");
140+
if (!string.IsNullOrEmpty(slidingExpiration) && TimeSpan.TryParse(slidingExpiration, out var sliding)) {
141+
options.DefaultSlidingExpiration = sliding;
142+
}
143+
});
144+
145+
var app = builder.Build();
146+
```
147+
148+
2. Use the Cache
149+
150+
```csharp
151+
public class MyService {
152+
private readonly IDistributedCache _cache;
153+
154+
public MyService(IDistributedCache cache) {
155+
_cache = cache;
156+
}
157+
158+
public async Task<string> GetDataAsync(string key) {
159+
var cachedData = await _cache.GetStringAsync(key);
160+
161+
if (cachedData == null) {
162+
// Fetch data from source
163+
var data = await FetchDataFromSource();
164+
165+
// Cache the data with options
166+
var options = new DistributedCacheEntryOptions {
167+
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30),
168+
SlidingExpiration = TimeSpan.FromMinutes(5)
169+
};
170+
171+
await _cache.SetStringAsync(key, data, options);
172+
return data;
173+
}
174+
175+
return cachedData;
176+
}
177+
}
178+
```
179+
108180
### Distributed NCache Cache
109181

110182
[NCache](https://github.com/Alachisoft/NCache) is an open source in-memory distributed cache developed natively in .NET and .NET Core. NCache works both locally and configured as a distributed cache cluster for an ASP.NET Core app running in Azure or on other hosting platforms.
@@ -166,6 +238,7 @@ When SQL Server is used as a distributed cache backing store, use of the same da
166238

167239
* [Redis Cache on Azure](/azure/azure-cache-for-redis/)
168240
* [SQL Database on Azure](/azure/sql-database/)
241+
* [Azure Database for PostgreSQL](/azure/postgresql/)
169242
* [ASP.NET Core IDistributedCache Provider for NCache in Web Farms](http://www.alachisoft.com/ncache/aspnet-core-idistributedcache-ncache.html) ([NCache on GitHub](https://github.com/Alachisoft/NCache))
170243
* <xref:performance/caching/memory>
171244
* <xref:fundamentals/change-tokens>

0 commit comments

Comments
 (0)