I want to implement an expiration time on a Lazy object. The expiration cooldown must start with the first retrieve of the value. If we get the value, and the expiration time is passed, then we reexecute the function and reset expiration time.
I'm not familiar with extensions, partial keyword, and I don't know the best way to do that.
Thanks
EDIT :
The code so far :
NEW EDIT :
the new code :
public class LazyWithExpiration<T>
{
private volatile bool expired;
private TimeSpan expirationTime;
private Func<T> func;
private Lazy<T> lazyObject;
public LazyWithExpiration( Func<T> func, TimeSpan expirationTime )
{
this.expirationTime = expirationTime;
this.func = func;
Reset();
}
public void Reset()
{
lazyObject = new Lazy<T>( func );
expired = false;
}
public T Value
{
get
{
if ( expired )
Reset();
if ( !lazyObject.IsValueCreated )
{
Task.Factory.StartNew( () =>
{
Thread.Sleep( expirationTime );
expired = true;
} );
}
return lazyObject.Value;
}
}
}