Class StateBase<TKey, TValue>
An abstract base class implementing IState<TKey, TValue> with a method to query and retrieve an object.
Inherited Members
Namespace: Microsoft.XboxStudios.GameStateTracker
Assembly: GameStateTracker.Core.dll
Syntax
public abstract class StateBase<TKey, TValue> : IState<TKey, TValue>, IReadOnlyCollection<TValue>, IEnumerable<TValue>, IEnumerable, IHasUpdateTime, IDisposable where TValue : class
Type Parameters
Name | Description |
---|---|
TKey | The key type to query for an object. |
TValue | The object type. |
Constructors
View SourceStateBase(IObjectCache<TKey, TValue>, ILogger<StateBase<TKey, TValue>>?)
Initializes a new instance of the StateBase<TKey, TValue> class.
Declaration
protected StateBase(IObjectCache<TKey, TValue> cache, ILogger<StateBase<TKey, TValue>>? logger = null)
Parameters
Type | Name | Description |
---|---|---|
IObjectCache<TKey, TValue> | cache | The cache to use to store objects. |
ILogger<StateBase<TKey, TValue>> | logger | The logger to use. |
Properties
View SourceCache
Gets the cache of objects.
Declaration
protected IObjectCache<TKey, TValue> Cache { get; }
Property Value
Type | Description |
---|---|
IObjectCache<TKey, TValue> |
Count
Gets the count of objects in the cache.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
int |
IsUpdateInProgress
Gets a value indicating whether or not the state is being updated.
Declaration
public bool IsUpdateInProgress { get; }
Property Value
Type | Description |
---|---|
bool |
Logger
Gets or sets the logger to use.
Declaration
public ILogger<StateBase<TKey, TValue>> Logger { get; set; }
Property Value
Type | Description |
---|---|
ILogger<StateBase<TKey, TValue>> |
Semaphore
Gets a semaphore to prevent concurrent attempts at retrieving objects and updating the cache.
Declaration
protected SemaphoreSlim Semaphore { get; }
Property Value
Type | Description |
---|---|
SemaphoreSlim |
UpdateProcessor
Gets a task queue for delaying state updates.
Declaration
protected TaskQueue UpdateProcessor { get; }
Property Value
Type | Description |
---|---|
TaskQueue |
UpdateTime
Gets or sets the current time to compare to when querying the cache.
Declaration
public DateTime? UpdateTime { get; set; }
Property Value
Type | Description |
---|---|
DateTime? |
Methods
View SourceAgeObject(TKey)
Gets the age of an object in the cache.
Declaration
protected virtual TimeSpan? AgeObject(TKey info)
Parameters
Type | Name | Description |
---|---|---|
TKey | info | The object info. |
Returns
Type | Description |
---|---|
TimeSpan? | The time since the object was last refreshed. |
Clear(TKey)
Removes an object from the cache.
Declaration
public bool Clear(TKey info)
Parameters
Type | Name | Description |
---|---|---|
TKey | info | The object info. |
Returns
Type | Description |
---|---|
bool | True if the object was removed. |
Dispose()
Dispose the state.
Declaration
public void Dispose()
Existing(TKey)
Gets a cached object with the given info.
Declaration
public virtual TValue? Existing(TKey info)
Parameters
Type | Name | Description |
---|---|---|
TKey | info | The object info. |
Returns
Type | Description |
---|---|
TValue | The object. |
Get(TKey, TimeSpan, CancellationToken)
Retrieves an object.
Declaration
public virtual Task<TValue?> Get(TKey query, TimeSpan maxAge, CancellationToken token = default)
Parameters
Type | Name | Description |
---|---|---|
TKey | query | The object to look for. |
TimeSpan | maxAge | The maximum age of the object, if it exists in the cache. |
CancellationToken | token | The cancellation token. |
Returns
Type | Description |
---|---|
Task<TValue> | The object. |
GetEnumerator()
Returns an IEnumerator<T> for objects in the cache.
Declaration
public IEnumerator<TValue> GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator<TValue> | An IEnumerator<T> object. |
GetObject(TKey, CancellationToken)
Retrieves an object matching the given query.
Declaration
protected abstract Task<TValue?> GetObject(TKey query, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
TKey | query | The objects to look for. |
CancellationToken | token | The cancellation token. |
Returns
Type | Description |
---|---|
Task<TValue> | An object. |
Set(in TKey, TValue?)
Stores an object and set its age in the cache.
Declaration
protected virtual void Set(in TKey info, TValue? value)
Parameters
Type | Name | Description |
---|---|---|
TKey | info | The object information. |
TValue | value | The object to store. If null, the object is removed, but the cache keeps the object's age. |
WaitForUpdateFinished()
Waits for the state to finish being updated.
Declaration
public Task WaitForUpdateFinished()
Returns
Type | Description |
---|---|
Task | A task that completes when the state is no longer being updated. |