Table of Contents

Class InMemoryMessageStore<TModel>

Namespace
Fdyay.IdentityServer.Services
Assembly
Fdyay.IdentityServer.AspNetCore.dll

The default in-process message store.

public sealed class InMemoryMessageStore<TModel> : IMessageStore<TModel>

Type Parameters

TModel
Inheritance
InMemoryMessageStore<TModel>
Implements
Inherited Members

Remarks

Suitable for a single instance. A multi-instance host should register its own distributed implementation of IMessageStore<TModel> — messages held here are neither shared nor survive a restart.

Bounded since 0.177.0. The error store is written on behalf of anonymous requests, so what it holds is decided by whoever sends them. It holds at most DefaultCapacity messages (or the capacity it was built with), the oldest giving way to the newest, and a write sweeps only the messages that have expired, oldest first — constant work per write, where until then every write walked every message held, so the cost of a write grew with the number of requests sent in the last five minutes.

Constructors

InMemoryMessageStore(TimeProvider, TimeSpan)

Creates the store, holding at most DefaultCapacity messages.

public InMemoryMessageStore(TimeProvider time, TimeSpan lifetime)

Parameters

time TimeProvider

The clock a message's expiry is read against.

lifetime TimeSpan

How long a message can be read after it is written.

Exceptions

ArgumentNullException

time is null.

InMemoryMessageStore(TimeProvider, TimeSpan, int)

Creates the store, holding at most capacity messages.

public InMemoryMessageStore(TimeProvider time, TimeSpan lifetime, int capacity)

Parameters

time TimeProvider

The clock a message's expiry is read against.

lifetime TimeSpan

How long a message can be read after it is written.

capacity int

The most messages held at once; a write past it drops the oldest.

Exceptions

ArgumentNullException

time is null.

ArgumentOutOfRangeException

capacity is zero or negative.

Fields

DefaultCapacity

How many messages a store holds when it is not told: 10,000.

public const int DefaultCapacity = 10000

Field Value

int

Remarks

Comfortably more than the messages a single instance writes within a message's lifetime under ordinary load, and few enough that a flood of refused requests costs a bounded amount of memory. Past it the oldest message is dropped, which for an error message means the error page shows its generic text, and for a consent decision means the user is asked again.

Properties

Count

The number of messages held, counting any that have expired and not yet been swept.

public int Count { get; }

Property Value

int

Methods

ReadAsync(string?, CancellationToken)

Reads the message with the given id, or null when the id is unknown or has expired.

public Task<TModel?> ReadAsync(string? id, CancellationToken cancellationToken = default)

Parameters

id string
cancellationToken CancellationToken

Returns

Task<TModel>

WriteAsync(TModel, CancellationToken)

Stores the message and returns the opaque id it can be read back with.

public Task<string> WriteAsync(TModel message, CancellationToken cancellationToken = default)

Parameters

message TModel
cancellationToken CancellationToken

Returns

Task<string>

Remarks

The id is 256 random bits, Base64Url-encoded. Expired messages are removed as each new one is written, and the oldest one when the store is full.

Exceptions

ArgumentNullException

message is null.