You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
876 B
35 lines
876 B
namespace Connected.Caching;
|
|
|
|
internal class Entry : IEntry
|
|
{
|
|
public Entry(string id, object? instance, TimeSpan duration, bool slidingExpiration)
|
|
{
|
|
Id = id;
|
|
Instance = instance;
|
|
SlidingExpiration = slidingExpiration;
|
|
Duration = duration;
|
|
|
|
if (Duration > TimeSpan.Zero)
|
|
ExpirationDate = DateTime.UtcNow.AddTicks(duration.Ticks);
|
|
}
|
|
|
|
public bool SlidingExpiration { get; }
|
|
private DateTime ExpirationDate { get; set; }
|
|
public TimeSpan Duration { get; set; }
|
|
|
|
public object? Instance { get; }
|
|
public string Id { get; }
|
|
public bool Expired => ExpirationDate != DateTime.MinValue && ExpirationDate < DateTime.UtcNow;
|
|
|
|
public void Hit()
|
|
{
|
|
if (SlidingExpiration && Duration > TimeSpan.Zero)
|
|
ExpirationDate = DateTime.UtcNow.AddTicks(Duration.Ticks);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Instance is IDisposable disposable)
|
|
disposable.Dispose();
|
|
}
|
|
} |