SessionTracker.Redis
1.0.0
See the version list below for details.
dotnet add package SessionTracker.Redis --version 1.0.0
NuGet\Install-Package SessionTracker.Redis -Version 1.0.0
<PackageReference Include="SessionTracker.Redis" Version="1.0.0" />
paket add SessionTracker.Redis --version 1.0.0
#r "nuget: SessionTracker.Redis, 1.0.0"
// Install SessionTracker.Redis as a Cake Addin #addin nuget:?package=SessionTracker.Redis&version=1.0.0 // Install SessionTracker.Redis as a Cake Tool #tool nuget:?package=SessionTracker.Redis&version=1.0.0
SessionTracker
Library that allows working with distributed "sessions".
Features
- Session tracking via starting, updating, finishing, fetching and fetching finished
Session
objects. - Configurable caching settings per
Session
type. - Redis implementations provided out of the box.
Description
A session is encapsulated data that has to be passed around a distributed cache or any other backing-store custom implementation. It is helpful when dealing with for example Discord API interactions that may need some data passed between them without relying on Discords custom ID.
Installation
To register the session tracker service use the following extension methods:
For Redis implementation:
builder.AddRedisSessionTracker(options, redisOptions);
If you wish to configure the JsonSerializerOptions
used for serializing/deserializing session instances within Redis provider use:
services.Configure<JsonSerializerOptions>(SessionSettings.JsonSerializerName, yourOptions);
You can implement your own backing store provider by implementing ISessionsBackingStoreProvider
interface and registering your new service with the container like so:
services.AddSingleton<ISessionsBackingStoreProvider, YourImplementationType>;
Example usage
Create your own Session
type:
public CustomSession : Session
{
public bool IsThisASuperSession { get; set; }
public CustomSession(bool isSuper = true)
=> IsThisASuperSession = isSuper;
}
Inject ISessionTracker
to your handlers/services/controllers/whatnot:
Start session inside one handler:
public FirstSimpleInteractionHandler
{
private readonly ISessionTracker _tracker;
public FirstSimpleInteractionHandler(ISessionTracker tracker)
=> _tracker = tracker;
void Handle()
{
string key = "superKeyForThisSession";
var session = new CustomSession(false);
var lockResult = await _tracker.StartSessionAsync<CustomSession>(key, session);
if (!lockResult.IsSuccess)
return;
await using var @lock = lockResult.Entity;
}
}
Update from another:
public SecondSimpleInteractionHandler
{
private readonly ISessionTracker _tracker;
public SecondSimpleInteractionHandler(ISessionTracker tracker)
=> _tracker = tracker;
void Handle()
{
string key = "superKeyForThisSession"
var lockedResult = await _tracker.GetSessionAsync<CustomSession>(key);
if (!lockedResult.IsDefined(out var lockedSession))
return;
await using var @lock = lockedSession.Lock;
lockedSession.Session.IsThisASuperSession = false;
await _tracker.UpdateSessionAsync<CustomSession>(key, lockedSession.Session);
}
}
Use in third:
public ThirdSimpleInteractionHandler
{
private readonly ISessionTracker _tracker;
public ThirdSimpleInteractionHandler(ISessionTracker tracker)
=> _tracker = tracker;
void Handle()
{
string key = "superKeyForThisSession"
var result = await _tracker.GetBareSessionAsync<CustomSession>(key);
if (!result.IsDefined(out var session))
return;
var check = result.IsThisASuperSession // returns false
}
}
Finalize in fourth:
public FourthSimpleInteractionHandler
{
private readonly ISessionTracker _tracker;
public FourthSimpleInteractionHandler(ISessionTracker tracker)
=> _tracker = tracker;
void Handle()
{
string key = "superKeyForThisSession"
var lockResult = await _tracker.LockAsync<CustomSession>(key);
if (!lockResult.IsSuccess)
return;
await using var @lock = lockResult.Entity;
await _tracker.FinishSessionAsync<CustomSession>(key);
}
}
Get from finalized cache in fifth:
public FifthSimpleInteractionHandler
{
private readonly ISessionTracker _tracker;
public FifthSimpleInteractionHandler(ISessionTracker tracker)
=> _tracker = tracker;
void Handle()
{
string key = "superKeyForThisSession"
// note that using GetSessionAsync here would result in a NotFoundError
var result = await _tracker.GetFinishedSessionAsync<CustomSession>(key);
if (!result.IsDefined(out var session))
return;
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- RedLock.net (>= 2.3.2)
- SessionTracker (>= 2.0.1)
- StackExchange.Redis (>= 2.6.48)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.14 | 330 | 1/9/2023 |
1.0.13 | 281 | 1/9/2023 |
1.0.12 | 322 | 11/19/2022 |
1.0.11 | 318 | 11/19/2022 |
1.0.10 | 405 | 9/6/2022 |
1.0.8 | 379 | 9/4/2022 |
1.0.7 | 383 | 9/4/2022 |
1.0.6 | 380 | 9/4/2022 |
1.0.5 | 388 | 9/4/2022 |
1.0.4 | 369 | 9/4/2022 |
1.0.3 | 393 | 9/4/2022 |
1.0.2 | 389 | 9/4/2022 |
1.0.1 | 392 | 9/4/2022 |
1.0.0 | 391 | 9/3/2022 |