Created
January 5, 2017 01:34
-
-
Save pparadis/fb805d63c351d7b84192fa02652df082 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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