Skip to content

Commit

Permalink
Be kinder to the cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryCordewener committed Jan 4, 2024
1 parent 841a2e8 commit 49249c8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
9 changes: 2 additions & 7 deletions TelnetNegotiationCore.TestServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ static async Task Main(string[] args)

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel(options =>
{
options.ListenLocalhost(4202, builder =>
{
builder.UseConnectionHandler<KestrelMockServer>();
});
}).UseStartup<Startup>();
.UseKestrel(options => options.ListenLocalhost(4202, builder => builder.UseConnectionHandler<KestrelMockServer>()))
.UseStartup<Startup>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public partial class TelnetInterpreter
/// <param name="t">The Trigger</param>
/// <returns>A Parameterized trigger</returns>
private StateMachine<State, Trigger>.TriggerWithParameters<OneOf<byte, Trigger>> ParameterizedTrigger(Trigger t)
=> _parameterizedTriggers.ParametarizedTrigger(TelnetStateMachine, t);
=> _parameterizedTriggers.ParameterizedTrigger(TelnetStateMachine, t);

/// <summary>
/// The Serilog style Logger
Expand Down
12 changes: 7 additions & 5 deletions TelnetNegotiationCore/Models/Trigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ namespace TelnetNegotiationCore.Models
/// </summary>
public class ParameterizedTriggers
{
private readonly Dictionary<Trigger, StateMachine<State, Trigger>.TriggerWithParameters<OneOf<byte,Trigger>>> _cache = new();
private readonly Dictionary<Trigger, StateMachine<State, Trigger>.TriggerWithParameters<OneOf<byte, Trigger>>> _cache = [];

/// <summary>
/// Returns a (cached) Parameterized Trigger.
/// </summary>
/// <param name="stm">State Machine</param>
/// <param name="t">The Trigger</param>
/// <returns>One of Byte or Trigger, allowing both the 255 byte range excluding standard triggers, and Triggers above the number</returns>
public StateMachine<State, Trigger>.TriggerWithParameters<OneOf<byte, Trigger>> ParametarizedTrigger(StateMachine<State, Trigger> stm, Trigger t)
public StateMachine<State, Trigger>.TriggerWithParameters<OneOf<byte, Trigger>> ParameterizedTrigger(StateMachine<State, Trigger> stm, Trigger t)
{
if (!_cache.ContainsKey(t))

if (_cache.TryGetValue(t, out StateMachine<State, Trigger>.TriggerWithParameters<OneOf<byte, Trigger>> value))
{
_cache.Add(t, stm.SetTriggerParameters<OneOf<byte, Trigger>>(t));
return value;
}
_cache.Add(t, stm.SetTriggerParameters<OneOf<byte, Trigger>>(t));
return _cache[t];
}
}
Expand All @@ -44,7 +46,7 @@ public static void ForAllTriggersExcept(IEnumerable<Trigger> except, Action<Trig
=> MoreLinq.MoreEnumerable.ForEach(AllTriggers.Except(except), f);

public static void ForAllTriggersButIAC(Action<Trigger> f)
=> ForAllTriggersExcept(new[] { Trigger.IAC }, f);
=> ForAllTriggersExcept([Trigger.IAC], f);
}

#pragma warning disable CA1069 // Enums values should not be duplicated
Expand Down

0 comments on commit 49249c8

Please sign in to comment.