Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upsert #4

Merged
merged 2 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageVersion>2.0.0</PackageVersion>
<PackageVersion>2.1.0</PackageVersion>
<Description>Service types for interacting with CosmosDb</Description>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.37.1" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.41.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
</ItemGroup>

Expand Down
12 changes: 12 additions & 0 deletions BibleTalkAI.CosmosDatabase.Services/CosmosDbCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ public async ValueTask Replace(TDocument item, string id)
cache.Set(CacheKey(id), item);
}

public async ValueTask Upsert(TDocument item, Guid id)
{
await db.Upsert(item, id);
cache.Set(CacheKey(id), item);
}

public async ValueTask Upsert(TDocument item, string id)
{
await db.Upsert(item, id);
cache.Set(CacheKey(id), item);
}

public void SetCache(Guid id, TDocument item) => cache.Set(CacheKey(id), item);
public void SetCache(string id, TDocument item) => cache.Set(CacheKey(id), item);

Expand Down
7 changes: 7 additions & 0 deletions BibleTalkAI.CosmosDatabase.Services/CosmosDbService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,11 @@ public ValueTask Replace(TDocument item, Guid id)
public async ValueTask Replace(TDocument item, string id)
=> await _container.ReplaceItemAsync(item, id, new PartitionKey(id),
requestOptions: CosmosOptions.NoContentOnWrite);

public ValueTask Upsert(TDocument item, Guid id)
=> Upsert(item, id.ToString());

public async ValueTask Upsert(TDocument item, string id)
=> await _container.UpsertItemAsync(item, new PartitionKey(id),
requestOptions: CosmosOptions.NoContentOnWrite);
}
2 changes: 2 additions & 0 deletions BibleTalkAI.CosmosDatabase.Services/IDbCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface IDbCache<TDocument>
ValueTask Remove(string id);
ValueTask Replace(TDocument item, Guid id);
ValueTask Replace(TDocument item, string id);
ValueTask Upsert(TDocument item, Guid id);
ValueTask Upsert(TDocument item, string id);
void SetCache(Guid id, TDocument item);
void SetCache(string id, TDocument item);
void RemoveCached(Guid id);
Expand Down
2 changes: 2 additions & 0 deletions BibleTalkAI.CosmosDatabase.Services/IDbService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface IDbService<TDocument>
ValueTask Remove(string id);
ValueTask Replace(TDocument item, Guid id);
ValueTask Replace(TDocument item, string id);
ValueTask Upsert(TDocument item, Guid id);
ValueTask Upsert(TDocument item, string id);
}
Loading