Skip to content

Commit 4bbd0f5

Browse files
committed
Set time to left as a static value
1 parent 86df6e8 commit 4bbd0f5

File tree

5 files changed

+9
-31
lines changed

5 files changed

+9
-31
lines changed

Cache/ICacheService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Cache_Aside_Pattern.Cache
66
public interface ICacheService
77
{
88

9-
void Add<TItem>(TItem item, string key);
9+
void Add<TItem>(string key, TItem item, TimeSpan timeToLeft);
1010

1111
TItem Get<TItem>(string key) where TItem : class;
1212
}

Cache/MemoryCacheService.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
11
using Microsoft.Extensions.Caching.Memory;
22
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading.Tasks;
63

74
namespace Cache_Aside_Pattern.Cache
85
{
96

107
public class MemoryCacheService : ICacheService
118
{
129
private readonly IMemoryCache _memoryCache;
13-
private readonly Dictionary<string, TimeSpan> _expirationConfiguration;
14-
15-
public MemoryCacheService(
16-
IMemoryCache memoryCache,
17-
Dictionary<string, TimeSpan> expirationConfiguration
18-
)
10+
public MemoryCacheService(IMemoryCache memoryCache)
1911
{
2012
_memoryCache = memoryCache;
21-
_expirationConfiguration = expirationConfiguration;
2213
}
2314

24-
public void Add<TItem>(TItem item, string key)
15+
public void Add<TItem>(string key, TItem item, TimeSpan timeToLeft)
2516
{
26-
var cachedObjectName = item.GetType().Name;
27-
var timespan = _expirationConfiguration[cachedObjectName];
2817

29-
_memoryCache.Set(key, item, timespan);
18+
_memoryCache.Set(key, item, timeToLeft);
3019
}
3120

3221
public TItem Get<TItem>(string key) where TItem : class

Controllers/EmployeeController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Cache_Aside_Pattern.Controllers
1414
[Route("[controller]")]
1515
public class EmployeeController : ControllerBase
1616
{
17-
17+
private readonly TimeSpan _timeToLeft = TimeSpan.FromMinutes(5);
1818
private readonly AppDbContext _appDbContext;
1919
private readonly ICacheService _cacheService;
2020

@@ -34,7 +34,7 @@ public IActionResult Get(int id)
3434
record = _appDbContext.Employees.FirstOrDefault(x=>x.Id == id);
3535
if (record == null) return NotFound();
3636

37-
_cacheService.Add(record, $"{id}");
37+
_cacheService.Add($"{record.Id}", record, _timeToLeft);
3838
}
3939

4040
return Ok(record);
@@ -47,7 +47,7 @@ public async Task<IActionResult> CreateAsync(Employee employee)
4747
{
4848
_appDbContext.Employees.Add(employee);
4949
await _appDbContext.SaveChangesAsync();
50-
_cacheService.Add(employee, $"{employee.Id}");
50+
_cacheService.Add($"{employee.Id}", employee, _timeToLeft);
5151
}
5252
else
5353
{
@@ -56,7 +56,7 @@ public async Task<IActionResult> CreateAsync(Employee employee)
5656
if (record == null) return NotFound();
5757
_appDbContext.Employees.Update(employee);
5858
await _appDbContext.SaveChangesAsync();
59-
_cacheService.Add(employee, $"{employee.Id}");
59+
_cacheService.Add($"{employee.Id}", employee, _timeToLeft);
6060
}
6161

6262
return Ok(employee);

Startup.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Hosting;
1010
using Microsoft.OpenApi.Models;
11-
using System;
12-
using System.Collections.Generic;
13-
using System.Linq;
1411

1512
namespace Cache_Aside_Pattern
1613
{
@@ -29,12 +26,7 @@ public void ConfigureServices(IServiceCollection services)
2926
services.AddMemoryCache();
3027
services.AddDbContext<AppDbContext>(item => item.UseSqlServer(Configuration.GetConnectionString("SqlServerConnection")));
3128

32-
33-
var children = this.Configuration.GetSection("Caching").GetChildren();
34-
Dictionary<string, TimeSpan> configuration =
35-
children.ToDictionary(child => child.Key, child => TimeSpan.Parse(child.Value));
36-
37-
services.AddSingleton<ICacheService>(x => new MemoryCacheService(x.GetService<IMemoryCache>(), configuration));
29+
services.AddSingleton<ICacheService>(x => new MemoryCacheService(x.GetService<IMemoryCache>()));
3830
services.AddControllers();
3931
services.AddSwaggerGen(c =>
4032
{

appsettings.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@
99
"ConnectionStrings": {
1010
"SqlServerConnection": "server=.; database=AppDB;Trusted_Connection=True;"
1111
},
12-
"Caching": {
13-
"Employee": "00:05:00"
14-
},
1512
"AllowedHosts": "*"
1613
}

0 commit comments

Comments
 (0)