2using System.Collections.Generic;
4using System.Threading.Tasks;
6using Microsoft.Extensions.Logging;
25 readonly ILogger<IdentityCache>
logger;
40 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
48 logger.LogTrace(
"Disposing...");
55 ArgumentNullException.ThrowIfNull(user);
56 ArgumentNullException.ThrowIfNull(systemIdentity);
58 var uid = user.Require(x => x.Id);
59 var sysId = systemIdentity.
Uid;
61 ValueTask oldIdentityDisposal = ValueTask.CompletedTask;
66 logger.LogDebug(
"Caching system identity {sysId} of user {uid}", sysId, uid);
70 logger.LogTrace(
"Expiring previously cached identity...");
71 oldIdentityDisposal = identCache.DisposeAsync();
75 systemIdentity.
Clone(),
79 logger.LogDebug(
"Expiring system identity cache for user {uid}", uid);
80 lock (cachedIdentities)
81 cachedIdentities.Remove(uid);
89 await oldIdentityDisposal;
96 ArgumentNullException.ThrowIfNull(user);
97 var uid = user.Require(x => x.Id);
100 return identity.SystemIdentity.Clone();
102 throw new InvalidOperationException(
"Cached system identity has expired!");
Extension methods for the ValueTask and ValueTask<TResult> classes.
static async ValueTask WhenAll(IEnumerable< ValueTask > tasks)
Fully await a given list of tasks .
ISystemIdentity LoadCachedIdentity(User user)
Attempt to load a cached ISystemIdentity.The cached ISystemIdentity.
readonly ILogger< IdentityCache > logger
The ILogger for the IdentityCache.
async ValueTask CacheSystemIdentity(User user, ISystemIdentity systemIdentity, DateTimeOffset expiry)
Keep a user 's systemIdentity alive until an expiry time.A ValueTask representing the running opera...
readonly IAsyncDelayer asyncDelayer
The IAsyncDelayer for the IdentityCache.
IdentityCache(IAsyncDelayer asyncDelayer, ILogger< IdentityCache > logger)
Initializes a new instance of the IdentityCache class.
readonly Dictionary< long, IdentityCacheObject > cachedIdentities
The map of Api.Models.EntityId.Ids to IdentityCacheObjects.
For keeping a specific ISystemIdentity alive for a period of time.
For caching ISystemIdentitys.
Represents a user on the current global::System.Runtime.InteropServices.OSPlatform.
ISystemIdentity Clone()
Clone the ISystemIdentity creating another copy that must have IDisposable.Dispose called on it.
string Uid
A unique identifier for the user.
For waiting asynchronously.