2018-06-03 00:33:31 +00:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace Kerobot.Services.GuildState
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-04-24 20:41:10 +00:00
|
|
|
|
/// Contains a guild state object and other useful metadata in regards to it.
|
2018-06-03 00:33:31 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
class StateInfo : IDisposable
|
|
|
|
|
{
|
|
|
|
|
static readonly TimeSpan TimeUntilStale = new TimeSpan(0, 15, 0);
|
2018-06-06 22:19:21 +00:00
|
|
|
|
|
2018-06-03 00:33:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Module-provided data.
|
|
|
|
|
/// </summary>
|
2018-06-06 22:19:21 +00:00
|
|
|
|
public object Data { get; }
|
2018-06-03 00:33:31 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Hash of the JToken used to generate the data. In certain casaes, it is used to check
|
|
|
|
|
/// if the configuration may be stale and needs to be reloaded.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _configHash;
|
2018-06-06 22:19:21 +00:00
|
|
|
|
|
|
|
|
|
private DateTimeOffset _lastStaleCheck;
|
2018-06-03 00:33:31 +00:00
|
|
|
|
|
|
|
|
|
public StateInfo(object data, int configHash)
|
|
|
|
|
{
|
2018-06-06 22:19:21 +00:00
|
|
|
|
Data = data;
|
2018-06-03 00:33:31 +00:00
|
|
|
|
_configHash = configHash;
|
2018-06-06 22:19:21 +00:00
|
|
|
|
_lastStaleCheck = DateTimeOffset.UtcNow;
|
2018-06-03 00:33:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2018-06-06 22:19:21 +00:00
|
|
|
|
if (Data is IDisposable dd) { dd.Dispose(); }
|
2018-06-03 00:33:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-06-06 22:19:21 +00:00
|
|
|
|
/// Checks if the current data may be stale, based on the last staleness check or
|
|
|
|
|
/// if the underlying configuration has changed.
|
2018-06-03 00:33:31 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsStale(JToken comparison)
|
|
|
|
|
{
|
2018-06-06 22:19:21 +00:00
|
|
|
|
if (DateTimeOffset.UtcNow - _lastStaleCheck > TimeUntilStale) return true;
|
2018-06-03 00:33:31 +00:00
|
|
|
|
if (comparison.GetHashCode() != _configHash) return true;
|
2018-06-06 22:19:21 +00:00
|
|
|
|
_lastStaleCheck = DateTimeOffset.UtcNow;
|
2018-06-03 00:33:31 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|