-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move chat sessions to their own cache.
- Loading branch information
Showing
8 changed files
with
103 additions
and
47 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
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,3 @@ | ||
namespace Remotely.Server.Models.Messages; | ||
|
||
public record ChatSessionsChangedMessage(); |
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,57 @@ | ||
using Remotely.Shared.ViewModels; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Remotely.Server.Services; | ||
|
||
public interface IChatSessionCache | ||
{ | ||
void AddOrUpdate( | ||
string deviceId, | ||
ChatSession chatSession, | ||
Func<string, ChatSession, ChatSession> updateFactory); | ||
|
||
bool ContainsKey(string deviceId); | ||
ICollection<ChatSession> GetAllSessions(); | ||
ChatSession GetOrAdd(string deviceId, Func<string, ChatSession> factory); | ||
bool TryGetSession(string deviceId, [NotNullWhen(true)] out ChatSession? session); | ||
bool TryRemove(string deviceId, [NotNullWhen(true)] out ChatSession? session); | ||
} | ||
|
||
public class ChatSessionCache : IChatSessionCache | ||
{ | ||
private readonly ConcurrentDictionary<string, ChatSession> _sessions = new(); | ||
|
||
public void AddOrUpdate( | ||
string deviceId, | ||
ChatSession chatSession, | ||
Func<string, ChatSession, ChatSession> updateFactory) | ||
{ | ||
_sessions.AddOrUpdate(deviceId, chatSession, updateFactory); | ||
} | ||
|
||
public bool ContainsKey(string deviceId) | ||
{ | ||
return _sessions.ContainsKey(deviceId); | ||
} | ||
|
||
public ICollection<ChatSession> GetAllSessions() => _sessions.Values; | ||
|
||
public ChatSession GetOrAdd(string deviceId, Func<string, ChatSession> factory) | ||
{ | ||
return _sessions.GetOrAdd(deviceId, factory); | ||
} | ||
|
||
public bool TryGetSession(string deviceId, [NotNullWhen(true)] out ChatSession? session) | ||
{ | ||
return _sessions.TryGetValue(deviceId, out session); | ||
} | ||
|
||
public bool TryRemove(string deviceId, [NotNullWhen(true)] out ChatSession? session) | ||
{ | ||
return _sessions.TryRemove(deviceId, out session); | ||
} | ||
} |
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