-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathDbTimeDistributedController.cs
More file actions
40 lines (33 loc) · 1.21 KB
/
DbTimeDistributedController.cs
File metadata and controls
40 lines (33 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using LazyCache;
using Microsoft.AspNetCore.Mvc;
namespace CacheDatabaseQueriesApiSample.Controllers
{
public class DbTimeDistributedController : Controller
{
private readonly IDistributedAppCache _distributedCache;
private readonly string cacheKey = "DbTimeController.Get";
private readonly DbTimeContext dbContext;
public DbTimeDistributedController(DbTimeContext context, IDistributedAppCache distributedCache)
{
dbContext = context;
_distributedCache = distributedCache;
}
[HttpGet]
[Route("api/ddbtime")]
public DbTimeEntity Get()
{
Func<DbTimeEntity> actionThatWeWantToCache = () => dbContext.GeDbTime();
var cachedDatabaseTime = _distributedCache.GetOrAdd(cacheKey, actionThatWeWantToCache);
return cachedDatabaseTime;
}
[HttpDelete]
[Route("api/ddbtime")]
public IActionResult DeleteFromCache()
{
_distributedCache.Remove(cacheKey);
var friendlyMessage = new { Message = $"Item with key '{cacheKey}' removed from server in-memory cache" };
return Ok(friendlyMessage);
}
}
}