1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-21 21:55:38 +01:00
GiantsTools/Giants.Services/Store/CosmosDbVersioningStore.cs
2022-09-05 14:33:14 -07:00

64 lines
2.1 KiB
C#

namespace Giants.Services.Store
{
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
public class CosmosDbVersioningStore : IVersioningStore
{
private readonly ILogger<CosmosDbServerRegistryStore> logger;
private readonly IMemoryCache memoryCache;
private readonly IConfiguration configuration;
private CosmosDbClient client;
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;
}
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}";
}
}
}