A factory is just a function that you specify when using the main GetOrSet[Async]
method: basically it's the way you specify how to get a value when it is not in the cache or is expired.
Here's an example:
var id = 42;
var product = cache.GetOrSet<Product>(
$"product:{id}",
_ => GetProductFromDb(id), // THIS IS THE FACTORY
options => options.SetDuration(TimeSpan.FromMinutes(1))
);
FusionCache will search for the value in the cache (memory and distributed, if available) and, if nothing is there, will call the factory to obtain the value: it then saves it into the cache with the specified options, and returns it to the caller, all transparently.
Special care is put into calling just one factory per key, concurrently: this means that if 10 concurrent requests for the same key arrive at the same time and the data is not there, only one factory will be called, and the result will be stored and shared with all callers right away.
As you can see, when multiple concurrent GetOrSet[Async]
calls are made for the same key, only 1 of them will be executed: the returned value will be cached to be readily available, and then returned to all the callers for the same cache key.
This ensures that the data source (let's say a database) will not be overloaded with multiple requests for the same piece of data at the same time.