Split out the Flashii interaction code into a separate library.
This commit is contained in:
parent
51f5c4c948
commit
2eba089a21
55 changed files with 769 additions and 552 deletions
SharpChatCommon/Configuration
34
SharpChatCommon/Configuration/CachedValue.cs
Normal file
34
SharpChatCommon/Configuration/CachedValue.cs
Normal file
|
@ -0,0 +1,34 @@
|
|||
namespace SharpChat.Configuration {
|
||||
public class CachedValue<T>(Config config, string name, TimeSpan lifetime, T? fallback) {
|
||||
private Config Config { get; } = config ?? throw new ArgumentNullException(nameof(config));
|
||||
private string Name { get; } = name ?? throw new ArgumentNullException(nameof(name));
|
||||
private object ConfigAccess { get; } = new();
|
||||
|
||||
private object? CurrentValue { get; set; } = default(T);
|
||||
private DateTimeOffset LastRead { get; set; }
|
||||
|
||||
public T? Value {
|
||||
get {
|
||||
lock(ConfigAccess) { // this lock doesn't really make sense since it doesn't affect other config calls
|
||||
DateTimeOffset now = DateTimeOffset.Now;
|
||||
if((now - LastRead) >= lifetime) {
|
||||
LastRead = now;
|
||||
CurrentValue = Config.ReadValue(Name, fallback);
|
||||
Logger.Debug($"Read {Name} ({CurrentValue})");
|
||||
}
|
||||
}
|
||||
return (T?)CurrentValue;
|
||||
}
|
||||
}
|
||||
|
||||
public static implicit operator T?(CachedValue<T?> val) => val.Value;
|
||||
|
||||
public void Refresh() {
|
||||
LastRead = DateTimeOffset.MinValue;
|
||||
}
|
||||
|
||||
public override string ToString() {
|
||||
return Value?.ToString() ?? string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue