Skip to content

Instantly share code, notes, and snippets.

Avatar

Matias Quaranta ealsur

View GitHub Profile
View cache-diagnostics.cs
void captureDiagnostics(CosmosDiagnostics diagnostics)
{
if (diagnostics.GetClientElapsedTime() > SomePredefinedThresholdTime)
{
Console.WriteLine(diagnostics.ToString());
}
}
services.AddCosmosCache((CosmosCacheOptions cacheOptions) =>
{
View cache-use.cs
public class MyBusinessClass
{
private readonly IDistributedCache cache;
public MyBusinessClass(IDistributedCache cache)
{
this.cache = cache;
}
public async Task SomeOperationAsync()
View caching-create-inject.cs
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
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
CosmosClientOptions clientOptions = new CosmosClientOptions(){
// other client options
EnableContentResponseOnWrite = false
};
CosmosClient client = new CosmosClient("<connection-string>", clientOptions);
@ealsur
ealsur / clientinitializeasync.cs
Created Apr 16, 2021
Create and initialize CosmosClient
View clientinitializeasync.cs
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
CosmosAsyncClient client = new CosmosClientBuilder()
.endpoint("account endpoint")
.key("account key")
.consistencyLevel(ConsistencyLevel.EVENTUAL)
.contentResponseOnWriteEnabled(false)
.buildAsyncClient();
View enablecontentresponseonwrite-sample-write-disable.cs
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
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
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>",