View cache-diagnostics.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void captureDiagnostics(CosmosDiagnostics diagnostics) | |
{ | |
if (diagnostics.GetClientElapsedTime() > SomePredefinedThresholdTime) | |
{ | |
Console.WriteLine(diagnostics.ToString()); | |
} | |
} | |
services.AddCosmosCache((CosmosCacheOptions cacheOptions) => | |
{ |
View cache-use.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyBusinessClass | |
{ | |
private readonly IDistributedCache cache; | |
public MyBusinessClass(IDistributedCache cache) | |
{ | |
this.cache = cache; | |
} | |
public async Task SomeOperationAsync() |
View caching-create-inject.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.AddCosmosCache((CosmosCacheOptions cacheOptions) => | |
{ | |
cacheOptions.ContainerName = "myContainer"; | |
cacheOptions.DatabaseName = "myDatabase"; | |
cacheOptions.CosmosClient = preExistingClient; | |
/* Creates the container if it does not exist */ | |
cacheOptions.CreateIfNotExists = true; | |
}); |
View caching-create.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
/* Other service configurations */ | |
services.AddCosmosCache((CosmosCacheOptions cacheOptions) => | |
{ | |
CosmosClientBuilder clientBuilder = new CosmosClientBuilder("myConnectionString") | |
.WithApplicationRegion("West US"); | |
cacheOptions.ContainerName = "myContainer"; | |
cacheOptions.DatabaseName = "myDatabase"; | |
cacheOptions.ClientBuilder = clientBuilder; |
View enablecontentresponseonwrite-sample-clientoptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CosmosClientOptions clientOptions = new CosmosClientOptions(){ | |
// other client options | |
EnableContentResponseOnWrite = false | |
}; | |
CosmosClient client = new CosmosClient("<connection-string>", clientOptions); |
View clientinitializeasync.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<(string, string)> containers = new List<(string, string)> | |
{ | |
("DatabaseNameForContainer1", "ContainerName1"), | |
("DatabaseNameForContainer2", "ContainerName2") | |
}; | |
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions | |
{ | |
ApplicationName = "MyApplicationName", | |
// any other setting I want to customize |
View enablecontentresponseonwrite-sample-java.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CosmosAsyncClient client = new CosmosClientBuilder() | |
.endpoint("account endpoint") | |
.key("account key") | |
.consistencyLevel(ConsistencyLevel.EVENTUAL) | |
.contentResponseOnWriteEnabled(false) | |
.buildAsyncClient(); |
View enablecontentresponseonwrite-sample-write-disable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ToDoActivity item = new ToDoActivity() { ...}; | |
ItemResponse<ToDoActivity> itemResponse = await this.container.CreateItemAsync( | |
item, | |
requestOptions: new ItemRequestOptions() { EnableContentResponseOnWrite = false }); | |
ToDoActivity responseContent = itemResponse.Resource; // This is really the same as "item" | |
double consumedRUs = itemResponse.Headers.RequestCharge; | |
string etag = itemResponse.Headers.ETag; | |
string session = itemResponse.Headers.Session; |
View enablecontentresponseonwrite-sample-delete-disable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ItemResponse<ToDoActivity> readItemResponse = await this.container.ReadItemAsync( | |
id: "<id>", | |
new PartitionKey("<partitionKeyValue>")); | |
ToDoActivity someItem = readItemResponse.Resource; | |
// Apply some modification | |
someItem.value = 5; | |
ItemResponse<ToDoActivity> itemResponse = await this.container.ReplaceItemAsync<ToDoActivity>( | |
id: "<id>", |
View enablecontentresponseonwrite-sample-delete.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ItemResponse<ToDoActivity> readItemResponse = await this.container.ReadItemAsync( | |
id: "<id>", | |
new PartitionKey("<partitionKeyValue>")); | |
ToDoActivity someItem = readItemResponse.Resource; | |
// Apply some modification | |
someItem.value = 5; | |
ItemResponse<ToDoActivity> itemResponse = await this.container.ReplaceItemAsync<ToDoActivity>( | |
id: "<id>", |
NewerOlder