Skip to content

Absolute expiration not working with TimeSpan #84

Closed
@mtschneiders

Description

@mtschneiders

I'm not sure if this should be a supported scenario, but when setting Absolute Expiration with a TimeSpan, the cache entry does not expire.

For example, the following test fails:

IAppCache cache = new CachingService(new MemoryCacheProvider(new MemoryCache(new MemoryCacheOptions())));

cache.GetOrAdd<int?>("KEY", entry =>
{
	entry.SetAbsoluteExpiration(TimeSpan.FromSeconds(10));
	return 1;
});

Thread.Sleep(TimeSpan.FromSeconds(11));
Assert.Null(cache.Get<int?>("KEY")); //Fails

However, when we set the expiration with a DateTimeOffset, it works:

IAppCache cache = new CachingService(new MemoryCacheProvider(new MemoryCache(new MemoryCacheOptions())));

cache.GetOrAdd<int?>(KEY, entry =>
{
	entry.SetAbsoluteExpiration(DateTimeOffset.UtcNow.AddSeconds(10));
	return 1;
});

Thread.Sleep(TimeSpan.FromSeconds(11));

Assert.Null(cache.Get<int?>(KEY)); //Works

Meanwhile, when using .NET Core MemoryCache, setting the expiration with a TimeSpan works normally:

IMemoryCache cache = new MemoryCache(new MemoryCacheOptions());

cache.GetOrCreate<int?>("KEY", entry =>
{
	entry.SetAbsoluteExpiration(TimeSpan.FromSeconds(10));
	return 1;
});

Thread.Sleep(TimeSpan.FromSeconds(11));

Assert.Null(cache.Get<int?>("KEY")); //Works

The question is, should it be possible to set the Absolute Expiration with a TimeSpan or is it out of the library scope?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions