-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
协程bug修复, session分类为内存session和储存session, cmd Waiter新增多个构造器
- Loading branch information
Showing
14 changed files
with
607 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace SaladimQBot.Extensions; | ||
|
||
public interface ISession | ||
{ | ||
SessionId SessionId { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
|
||
namespace SaladimQBot.Extensions; | ||
|
||
public class MemorySessionService | ||
{ | ||
protected Dictionary<Type, Dictionary<SessionId, ISession>> allSessions; | ||
|
||
public MemorySessionService() | ||
{ | ||
allSessions = new(); | ||
} | ||
|
||
public TSession GetSession<TSession>(SessionId sessionId) where TSession : class, ISession, new() | ||
{ | ||
Type typeSession = typeof(TSession); | ||
if (allSessions.TryGetValue(typeSession, out var sessions)) | ||
{ | ||
if (sessions.TryGetValue(sessionId, out var session)) | ||
{ | ||
return (TSession)session; | ||
} | ||
else | ||
{ | ||
TSession newSession = new() | ||
{ | ||
SessionId = sessionId | ||
}; | ||
sessions.Add(sessionId, newSession); | ||
return newSession; | ||
} | ||
} | ||
else | ||
{ | ||
allSessions.Add(typeSession, new()); | ||
return GetSession<TSession>(sessionId); | ||
} | ||
} | ||
|
||
public bool TryRemoveSession<TSession>(SessionId sessionId) where TSession : class, ISession, new() | ||
{ | ||
Type typeSession = typeof(TSession); | ||
if (allSessions.TryGetValue(typeSession, out var sessions)) | ||
{ | ||
if (sessions.TryGetValue(sessionId, out var session)) | ||
{ | ||
sessions.Remove(session.SessionId); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public TSession GetUserSession<TSession>(long userId) where TSession : class, ISession, new() | ||
=> GetSession<TSession>(new SessionId(userId)); | ||
|
||
public TSession GetGroupSession<TSession>(long groupId) where TSession : class, ISession, new() | ||
=> GetSession<TSession>(new SessionId(0, groupId)); | ||
|
||
public TSession GetGroupUserSession<TSession>(long groupId, long userId) where TSession : class, ISession, new() | ||
=> GetSession<TSession>(new SessionId(userId, groupId)); | ||
|
||
public bool TryRemoveUserSession<TSession>(long userId) where TSession : class, ISession, new() | ||
=> TryRemoveSession<TSession>(new SessionId(userId)); | ||
|
||
public bool TryRemoveGroupSession<TSession>(long groupId) where TSession : class, ISession, new() | ||
=> TryRemoveSession<TSession>(new SessionId(0, groupId)); | ||
|
||
public bool TryRemoveGroupUserSession<TSession>(long groupId, long userId) where TSession : class, ISession, new() | ||
=> TryRemoveSession<TSession>(new SessionId(userId, groupId)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.