0

Using Razorpage I have a 5 Select Boxes populated onPageLoad from SQL queries using C# Lists containing Key and Value

When saving the form I need to get hold of the key and values in these lists again

So to prevent more queries to SQL I would like to store the list (key, value pair) in memory on load so I can access it later using C#

how can I do that?

I am using ASP.NET Core v2 and C#

Thanks

Thomas

2
  • Are those values session based or the same for everyone? If they are the same for everyone you could save them in a MemoryCache cache item. Commented Dec 19, 2017 at 14:17
  • yes they are the same for all users Commented Dec 19, 2017 at 14:19

1 Answer 1

2

You can use IMemoryCache

In your Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMemoryCache();
    }
}

In your controller

public class HomeController : Controller
{
    private IMemoryCache _cache;

    public HomeController(IMemoryCache memoryCache)
    {
        _cache = memoryCache;
    }
}

And then fetch it

 var cacheEntry = _cache.GetOrCreate("your-cache-key", entry =>
 {
    entry.SlidingExpiration = TimeSpan.FromMinutes(10);

    // return your key/value list;
    return new Dictionary<string,string>();
 }

See for more information In-memory caching in ASP.NET Core

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.