AspNetCore has a built-in memory cache that you can use to store pieces of data that are shared between requests.
Register the cache at startup...
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.UseMvcWithDefaultRoute();
}
}
And you can inject it like...
public class HomeController : Controller
{
private IMemoryCache _cache;
public HomeController(IMemoryCache memoryCache)
{
_cache = memoryCache;
}
public IActionResult Index()
{
string cultures = _cache[CacheKeys.Cultures] as CultureInfo[];
return View();
}
To make it work application wide, you can use a facade service with strongly-typed members combined with some sort of cache refresh pattern:
- Attempt to get the value from the cache
- If the attempt fails
- Lookup the data from the data source
- Repopulate the cache
- Return the value
public CultureInfo[] Cultures { get { return GetCultures(); } }
private CultureInfo[] GetCultures()
{
CultureInfo[] result;
// Look for cache key.
if (!_cache.TryGetValue(CacheKeys.Cultures, out result))
{
// Key not in cache, so get data.
result = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
// Set cache options.
var cacheEntryOptions = new MemoryCacheEntryOptions()
// Keep in cache for this time, reset time if accessed.
.SetSlidingExpiration(TimeSpan.FromMinutes(60));
// Save data in cache.
_cache.Set(CacheKeys.Cultures, result, cacheEntryOptions);
}
return result;
}
Of course, you could clean that up by making it into a service that accepts the cache as a dependency which you can inject wherever it is needed, but that is the general idea.
Note also there is a distributed cache in case you want to share data between web servers.