1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-24 07:05:37 +01:00
GiantsTools/Giants.Services/Store/CosmosDbVersioningStore.cs

64 lines
2.1 KiB
C#
Raw Normal View History

namespace Giants.Services.Store
{
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
2022-09-05 22:42:41 +02:00
public class CosmosDbVersioningStore : IVersioningStore
{
private readonly ILogger<CosmosDbServerRegistryStore> logger;
private readonly IMemoryCache memoryCache;
private readonly IConfiguration configuration;
private CosmosDbClient client;
2022-09-05 22:42:41 +02:00
public CosmosDbVersioningStore(
ILogger<CosmosDbServerRegistryStore> logger,
IMemoryCache memoryCache,
IConfiguration configuration)
{
this.logger = logger;
this.memoryCache = memoryCache;
this.configuration = configuration;
}
public async Task<VersionInfo> GetVersionInfo(string appName)
{
VersionInfo versionInfo = await this.memoryCache.GetOrCreateAsync<VersionInfo>(
key: GetCacheKey(appName),
factory: async (entry) =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5);
return await this.client.GetItemById<VersionInfo>(
VersionInfo.GenerateId(appName),
nameof(VersionInfo));
});
return versionInfo;
}
2022-09-05 22:42:41 +02:00
public async Task UpdateVersionInfo(VersionInfo versionInfo)
{
await this.client.UpsertItem(versionInfo);
}
public async Task Initialize()
{
this.client = new CosmosDbClient(
connectionString: this.configuration["CosmosDbEndpoint"],
authKeyOrResourceToken: this.configuration["CosmosDbKey"],
databaseId: this.configuration["DatabaseId"],
containerId: this.configuration["ContainerId"]);
await this.client.Initialize();
}
private static string GetCacheKey(string gameName)
{
return $"{CacheKeys.VersionInfo}-{gameName}";
}
}
}