Generic ObjectCache with Expiration I sometimes need an object that caches some data but only for the specified amount of time. So I created a class that should handle this. The main goal was to make it generic unlike the MemoryCache and be a single object cache and not a general all-pupose cache.
ObjectCache
public class ObjectCache&lt;T&gt;
{
    private readonly object _valueExpiredMutex = new object();

    private T _value;

    public ObjectCache(TimeSpan expiration)
    {
        Expiration = expiration;
    }

    public EventHandler&lt;ObjectCacheEventArgs&lt;T&gt;&gt; ValueExpired { get; set; }

    public TimeSpan Expiration { get; set; }

    public DateTime? LastUpdate { get; internal set; }

    public bool IsExpired
    {
        get { return !LastUpdate.HasValue || DateTime.Now - LastUpdate.Value &gt; Expiration; }
    }

    public T GetValue()
    {
        if (!IsExpired)
        {
            return _value;
        }

        lock (_valueExpiredMutex)
        {
            _value = OnValueExpired();
            LastUpdate = DateTime.Now;
        }

        return _value;
    }

    private T OnValueExpired()
    {
        if (ValueExpired == null)
        {
            throw new InvalidOperationException(&quot;ValueExpired event handler not set.&quot;);
        }

        var e = new ObjectCacheEventArgs&lt;T&gt;();
        ValueExpired(this, e);
        return e.Value;
    }
}


LastUpdate has an internal setter so that I can set it in tests
I tried to implement it to be thread-safe but I'm not sure if I did it correctly with only one mutex for getting a new value.
Any kind of feedback is welcome ;-)

EventArgs
public class ObjectCacheEventArgs&lt;T&gt; : EventArgs
{
    public T Value { get; set; }
}


Test
[TestMethod]
public void TestObjectCache()
{
    var number = 1;

    var objectCache = new ObjectCache&lt;int&gt;(
        new TimeSpan(hours: 0, minutes: 21, seconds: 0));
    objectCache.ValueExpired += (sender, e) =&gt;
    {
        e.Value = number++;
    };
            
    Assert.IsTrue(objectCache.IsExpired);
    Assert.AreEqual(1, objectCache.GetValue());
    Assert.IsFalse(objectCache.IsExpired);

    objectCache.LastUpdate = DateTime.Now.AddMinutes(-30);
    Assert.IsTrue(objectCache.IsExpired);
    Assert.AreEqual(2, objectCache.GetValue());
}
IMO this a strange aproach. One needs to first call GetValue() to be able to set a value by using an eventhandler. I would expect from such an ObjectCache to provide a constructor which takes a value as parameter too.  

In addition I would add a property to ObjectCacheEventArgs&lt;T&gt; signaling a state, for instance an enum having the members ChangeValue, KeepAlive and SkipValue to be set by the handler of the event.
If this value is e.g set to ChangeValue then the _value should be changed. If it is set to KeepAlive the LastUpdate should be set to DateTime.Now. And for SkipValue you would need to add another var indicating that the value is skipped and set _value to default(T). 

It isn't obvious from a caller side of view that you have to add an eventhandler for that event. IMO this is a bad sign which one would maybe notice first at running the code. Such behaviour shouldn't be wanted and isn't expected. If one does not add an eventhandler he expects that he just don't get notified if this event happens.