Skip to content

Commit e6a868b

Browse files
feat: Memory Cache settings
1 parent 63c0dee commit e6a868b

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

src/GettingThingsDone.ApplicationCore/Helpers/CacheHelper.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,28 @@
1010

1111
namespace GettingThingsDone.ApplicationCore.Helpers
1212
{
13+
// Get data from cache or from store
1314
public class CacheHelper
1415
{
1516
private readonly IAsyncRepository<Action> _actionRepository;
1617
private readonly IMemoryCache _memoryCache;
1718
public CacheHelper(IAsyncRepository<Action> actionRepository, IMemoryCache memoryCache)
1819
{
1920
_actionRepository = actionRepository;
20-
_memoryCache = memoryCache;
21+
_memoryCache = memoryCache;
2122
}
23+
24+
/// <summary>
25+
/// Get Actions from the DB or from the Cache if exists.
26+
/// </summary>
27+
/// <returns></returns>
2228
public async Task<List<ActionDto>> GetActions()
2329
{
24-
string cacheKey = "Actions-GetAll";
30+
string cacheKey = CacheKeys.ActionAll;
31+
2532
List<ActionDto> actionsAll;
2633
// Try to get from cache and convert, null if not allready cached.
27-
actionsAll = _memoryCache.Get(cacheKey) as List<ActionDto>;
34+
//actionsAll = _memoryCache.Get(cacheKey) as List<ActionDto>;
2835

2936
// TryGet returns true if the cache entry was found.
3037
// Othervise Set it into the cache
@@ -36,11 +43,19 @@ public async Task<List<ActionDto>> GetActions()
3643
.Select(action => action.TranslateTo<ActionDto>()) // Translate them into DTOs.
3744
.ToList();
3845

39-
// Store object into the cache
40-
_memoryCache.Set(cacheKey, actionsAll,
41-
new MemoryCacheEntryOptions()
42-
.SetAbsoluteExpiration(TimeSpan.FromSeconds(20)));
46+
// If there is any Set to cache
47+
if (actionsAll.Count() > 0)
48+
{
49+
// Store object into the cache
50+
_memoryCache.Set(cacheKey, actionsAll,
51+
new MemoryCacheEntryOptions()
52+
.SetSlidingExpiration(TimeSpan.FromSeconds(15)) // After last call.
53+
//.SetAbsoluteExpiration(TimeSpan.FromSeconds(20)) // Absolute cache life duration.
54+
//.SetPriority(CacheItemPriority.High) // If memory is low cache will be cleaned, with priority we can set in which order.
55+
);
56+
}
4357
}
58+
4459
return actionsAll;
4560
}
4661
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace GettingThingsDone.Contracts.Interface
2+
{
3+
/// <summary>
4+
/// Status of a service call.
5+
/// </summary>
6+
public static class CacheKeys
7+
{
8+
public const string ActionAll = "ActionAll";
9+
public const string Countries = "Countries";
10+
public const string Locations = "Locations";
11+
public const string Menu = "Menu";
12+
}
13+
}

src/GettingThingsDone.WebApi/Startup.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,20 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
105105
app.UseResponseCaching();
106106

107107
// Add and config Cache-Control - global settings.
108-
app.Use(async (context, next) =>
109-
{
110-
// For GetTypedHeaders, add: using Microsoft.AspNetCore.Http;
111-
context.Response.GetTypedHeaders().CacheControl =
112-
new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
113-
{
114-
Public = true,
115-
MaxAge = TimeSpan.FromSeconds(100)
116-
};
117-
context.Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.Vary] =
118-
new string[] { "lang" };
119-
120-
await next();
121-
});
108+
//app.Use(async (context, next) =>
109+
//{
110+
// // For GetTypedHeaders, add: using Microsoft.AspNetCore.Http;
111+
// context.Response.GetTypedHeaders().CacheControl =
112+
// new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
113+
// {
114+
// Public = true,
115+
// MaxAge = TimeSpan.FromSeconds(100)
116+
// };
117+
// context.Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.Vary] =
118+
// new string[] { "lang" };
119+
120+
// await next();
121+
//});
122122

123123
app.UseMvc();
124124
}

0 commit comments

Comments
 (0)