Skip to content

Instantly share code, notes, and snippets.

@pparadis
Created January 5, 2017 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pparadis/fb805d63c351d7b84192fa02652df082 to your computer and use it in GitHub Desktop.
Save pparadis/fb805d63c351d7b84192fa02652df082 to your computer and use it in GitHub Desktop.
class CarsCache
{
private AsyncExpiringLazy<List<Car>> Cars { get; set; }
public CarService CarService { get; set; }
public CarsCache()
{
Cars = new AsyncExpiringLazy<List<Car>>(async metadata =>
{
return await Task.Run(() =>
{
return new ExpirationMetadata<List<Car>>
{
Result = CarService.GetAllCars(),
ValidUntil = DateTimeOffset.UtcNow.AddHours(12)
};
});
});
}
public Task<List<Car>> GetAllCars()
{
return Cars.Value();
}
}
// Par la suite, l'appel se fait ainsi
var cache = new CarsCache();
var cars = await cache.GetAllCars();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment