ASP.NET Core caching implemented with Azure Cosmos DB
Перейти к файлу
Matias Quaranta 7c53dccbe0
Fix race conditions on SlidingExpiration + AbsoluteExpiration (#80)
* Handling race conditions

* tests

* release prep
2024-03-26 06:52:53 -07:00
.github/ISSUE_TEMPLATE Update issue templates 2021-04-22 13:36:28 -07:00
pipelines Enabling CodeQL on nightly (#76) 2023-12-14 14:31:33 -08:00
sample Updating sample dependencies and closing Security alerts (#73) 2023-11-01 13:53:20 -07:00
src Fix race conditions on SlidingExpiration + AbsoluteExpiration (#80) 2024-03-26 06:52:53 -07:00
tests Fix race conditions on SlidingExpiration + AbsoluteExpiration (#80) 2024-03-26 06:52:53 -07:00
.gitignore Adding test project (#2) 2019-08-06 17:04:59 -07:00
35MSSharedLib1024.snk Adding StrongName signing (#19) 2020-01-27 11:37:07 -08:00
CONTRIBUTING.md Create CONTRIBUTING.md 2021-04-22 13:34:49 -07:00
LICENSE Initial commit 2019-03-08 10:31:35 -08:00
README.md Update README.md 2022-10-11 11:18:58 -07:00
SECURITY.md Adding Microsoft SECURITY.MD (#65) 2022-08-15 08:45:55 -07:00
changelog.md Fix race conditions on SlidingExpiration + AbsoluteExpiration (#80) 2024-03-26 06:52:53 -07:00

README.md

Microsoft Caching Extension using Azure Cosmos DB

Build Status

This repository contains an implementation of IDistributedCache using Azure Cosmos DB that can be leveraged in ASP.NET Core as a Session State Provider.

There is also a sample on how to instantiate the provider as part of ASP.NET Core's pipeline.

Installation

This project is available on Nuget to install through any of the available package management tools.

CosmosClient initialization

The implementation provides two distinct options:

Use an existing instance of a CosmosClient

This option will make the provider re-use an existing CosmosClient instance, which won't be disposed when the provider is disposed.

services.AddCosmosCache((CosmosCacheOptions cacheOptions) =>
{
    cacheOptions.ContainerName = Configuration["CosmosCacheContainer"];
    cacheOptions.DatabaseName = Configuration["CosmosCacheDatabase"];
    cacheOptions.CosmosClient = existingCosmosClient;
    cacheOptions.CreateIfNotExists = true;
});

Use a defined CosmosBuilder

This option will make the provider maintain an internal instance of CosmosClient that will get disposed when the provider is disposed. The CosmosClient will be created using the provided CosmosBuilder.

services.AddCosmosCache((CosmosCacheOptions cacheOptions) =>
{
    cacheOptions.ContainerName = Configuration["CosmosCacheContainer"];
    cacheOptions.DatabaseName = Configuration["CosmosCacheDatabase"];
    cacheOptions.ClientBuilder = new CosmosClientBuilder(Configuration["CosmosConnectionString"]);
    cacheOptions.CreateIfNotExists = true;
});

State storage

The provider stores the state in a container within a database, both parameters are required within the CosmosCacheOptions initialization. An optional parameter, CreateIfNotExists will make sure to create the container if it does not exist with an optimized configuration for key-value storage. ContainerThroughput can be used to specify a particular Throughput on the container.

When providing an existing container and not using CreateIfNotExists, make sure the container has time to live enabled.

Cosmos account consistency

Consistency in a distributed cache is important, especially when applications are deployed across multiple instances and user requests can be distributed across all of them. Azure Cosmos DB has multiple options when it comes to configuring consistency. When using it as a distributed cache provider, the following consistency levels are recommended:

  • Session: Recommended only if the ASP.NET application will be using Application Request Routing Affinity. This is enabled by default when hosting your ASP.NET application in Azure App Service.
  • Bounded staleness
  • Strong

Using other consistency levels is not recommended as read operations could be getting a stale version of the session.

Diagnosing and troubleshooting

The provider executes operations on Cosmos DB based on the calls to the IDistributedCache APIs. There might be scenarios when the user wants to troubleshoot or diagnose these interactions (for example, on high latency scenarios).

The CosmosCacheOptions expose a delegate that can be configured to capture the Diagnostics from the SDK calls that are happening internally and decide to log them.

void captureDiagnostics(CosmosDiagnostics diagnostics)
{
    if (diagnostics.GetClientElapsedTime() > SomePredefinedThresholdTime)
    {
        Console.WriteLine(diagnostics.ToString());
    }
}

services.AddCosmosCache((CosmosCacheOptions cacheOptions) =>
{
    cacheOptions.DiagnosticsHandler = captureDiagnostics;
    /* other options */
});

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.