using System;

namespace TeslaConsole;

internal class Cached<T> where T : class
{
	private T mItem;

	private DateTime mNextExpireTime;

	private TimeSpan mCacheDuration;

	private object mSyncRoot = new object();

	public T Item
	{
		get
		{
			return mItem;
		}
		set
		{
			mItem = value;
			mNextExpireTime = DateTime.Now + mCacheDuration;
		}
	}

	public bool IsExpired => DateTime.Now > mNextExpireTime;

	public object SyncRoot => mSyncRoot;

	public Cached(TimeSpan cacheDuration)
	{
		mItem = null;
		mNextExpireTime = DateTime.MinValue;
		mCacheDuration = cacheDuration;
	}

	public void Expire()
	{
		mNextExpireTime = DateTime.MinValue;
	}
}
