You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/core/extensions/caching.md
+9-10Lines changed: 9 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -217,17 +217,20 @@ In the preceding C# code:
217
217
218
218
Consumers of this service are free to call `GetPhotosAsync` method, and handle photos accordingly. No `HttpClient` is required as the cache contains the photos.
219
219
220
+
The asynchronous signal is based on an encapsulated <xref:System.Threading.SemaphoreSlim> instance, within a generic-type constrained singleton. The `CacheSignal<T>` relies on an instance of `SemaphoreSlim`:
In the preceding C# code, the decorator pattern is used to wrap an instance of the `SemaphoreSlim`. Since the `CacheSignal<T>` is registered as a singleton, it can be used across all service lifetimes with any generic type — in this case, the `Photo`. It is responsible for signaling the seeding of the cache.
225
+
220
226
The `CacheWorker` is a subclass of <xref:Microsoft.Extensions.Hosting.BackgroundService>:
> You need to `override`<xref:Microsoft.Extensions.Hosting.BackgroundService.StartAsync%2A?displayProperty=nameWithType> and call `await _cacheSignal.WaitAsync()` in order to prevent a race condition between the starting of the `CacheWorker` and a call to `PhotoService.GetPhotosAsync`.
226
-
227
230
In the preceding C# code:
228
231
229
-
- The constructor requires an `ILogger`, `HttpClient`, `CacheSignal<Photo>`, and `IMemoryCache`.
230
-
- The defines an `_updateInterval`of three hours.
232
+
- The constructor requires an `ILogger`, `HttpClient`, and `IMemoryCache`.
233
+
- The `_updateInterval`is defined for three hours.
231
234
- The `ExecuteAsync` method:
232
235
- Loops while the app is running.
233
236
- Makes an HTTP request to `"https://jsonplaceholder.typicode.com/photos"`, and maps the response as an array of `Photo` objects.
@@ -236,11 +239,7 @@ In the preceding C# code:
236
239
- The call to <xref:System.Threading.Tasks.Task.Delay%2A?displayProperty=nameWithType> is awaited, given the update interval.
237
240
- After delaying for three hours, the cache is again updated.
238
241
239
-
The asynchronous signal is based on an encapsulated <xref:System.Threading.SemaphoreSlim> instance, within a generic-type constrained singleton. The `CacheSignal<T>` relies on an instance of `SemaphoreSlim`:
In the preceding C# code, the decorator pattern is used to wrap an instance of the `SemaphoreSlim`. Since the `CacheSignal<T>` is registered as a singleton, it can be used across all service lifetimes with any generic type — in this case, the `Photo`. It is responsible for signaling the seeding of the cache.
242
+
Consumers in the same process could ask the `IMemoryCache` for the photos, but the `CacheWorker` is responsible for updating the cache.
0 commit comments