locked
load data to cache at startup and keep using them all project RRS feed

  • Question

  • User-1471881183 posted

    hello all,

    i have a lookpup table, which has 6 country names, table name is tlkpCountry.

    I have written controller to Read and the function also gives me exact result.

    my query is,

    1. i would like to call above Read function while application startup and keep it in cache memory
    2. then, in all controller get this Cache data and display in RadioButton
    3. then, from the controller I have to find which country is selected from the RadioButton

    could you please post some sample code, I found some links from net but, they are not useful. so, kindly help me.

    Tuesday, May 4, 2021 1:53 PM

Answers

  • User475983607 posted

    Cache uses a standard programming pattern which does not match your requirements.  Cache has a timeout which requires a check to see if cache exists.

    Get cache. 

    • If cached is empty, populate cache.
    • Return cache.

    Your requirements describes application configuration.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 5, 2021 10:58 AM
  • User-474980206 posted

    with only 6 entries you hardly need a cache. just load a static list at startup.

    public static class GlobalData
    {
          private static string[] _countyNames;
    
          public static void LoadCountryNames(MyDbContext db)
          {
                 _countryNames = db.CountryNames.Select(c => c.Name).ToList();
          } 
          public static string[] CountryNames => _countyNames?.ToArray() ?? new string[]{}; //always a copy
    }

    load in startup:

        GlobalData.LoadCountryNames(dbContext);

    whenever you need the list its:

        var names = GlobalData.CountryNames;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 5, 2021 8:25 PM

All replies

  • User1686398519 posted

    Hi winseealn, 

    I want to confirm some questions with you:

    1. in cache memory

      • If you want to use the in-memory cache in ASP.NET Core, you can refer to this document.
    2. For how to put data in a MemoryCache on startup, you can refer to this link.

    Best Regards,

    YihuiSun

    Wednesday, May 5, 2021 8:35 AM
  • User475983607 posted

    Cache uses a standard programming pattern which does not match your requirements.  Cache has a timeout which requires a check to see if cache exists.

    Get cache. 

    • If cached is empty, populate cache.
    • Return cache.

    Your requirements describes application configuration.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 5, 2021 10:58 AM
  • User-1471881183 posted

    @yihuisun thanks for your response

    one of my controller/action has this vital data so, i want to call that action from startup then keep this data in cache. so, may i know how to call a controller/action and get data from startup?

    thank you

    Wednesday, May 5, 2021 12:20 PM
  • User475983607 posted

    winseealn@hotmail.com

    one of my controller/action has this vital data so, i want to call that action from startup then keep this data in cache. so, may i know how to call a controller/action and get data from startup?

    It's not clear why you are struggling after reading the linked documentation.  Can you show us the code you're designed after learning how Memory cache works?

    IMHO, your question is related to configuration not cache.   Again cache expires.  So you'll need to update your requirements to handle a timeout. 

    Configuration reference. 

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-5.0

    Wednesday, May 5, 2021 12:25 PM
  • User-474980206 posted

    with only 6 entries you hardly need a cache. just load a static list at startup.

    public static class GlobalData
    {
          private static string[] _countyNames;
    
          public static void LoadCountryNames(MyDbContext db)
          {
                 _countryNames = db.CountryNames.Select(c => c.Name).ToList();
          } 
          public static string[] CountryNames => _countyNames?.ToArray() ?? new string[]{}; //always a copy
    }

    load in startup:

        GlobalData.LoadCountryNames(dbContext);

    whenever you need the list its:

        var names = GlobalData.CountryNames;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 5, 2021 8:25 PM