locked
Abstract class Cache question RRS feed

  • Question

  • User1034446946 posted

    Hi

    I am using

    http://afana.me/archive/2013/11/01/aspnet-mvc-internationalization-store-strings-in-database-or-xml.aspx/

    which has a cache option, in the contructor it has

    Cache = false; // By default, disable caching for performance

    however surely cache being true would be better for performance, so why does it say oposite?

    Also can someone explain how the cache works in this example, it doesn't use any of the cache libraries, so sure after the a website request has been made it calls the the calls to get the information, then next time its called will it not be a fresh request and  empty?

    Also why is there a lock in the call?

    Any information would be appriciated.

    Friday, June 1, 2018 1:03 PM

All replies

  • User475983607 posted

    Cache = false; // By default, disable caching for performance

    however surely cache being true would be better for performance, so why does it say oposite?

    Keep in mind that the blog is about creating application resources.  Application resources are loaded once when the application starts.  The blog's subject dictates the architecture.  In this context, caching adds extra overhead because the cache expires which causes the code to re-populate the cache.  Enable cache if you want resources to update dring runtime.  Otherwise, the app must be restarted to refresh the cache.  Usually, resource files, like the web.config, are for items that do not change often.

    Also why is there a lock in the call?

    Locks serialize access to static resources.  You don't want to allow reads while the cache is being loaded.

    Resource file reference.

    https://msdn.microsoft.com/en-us/library/ms227427.aspx

    C# lock reference. 

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/lock-statement

    Friday, June 1, 2018 1:58 PM
  • User1034446946 posted

    Thanks for the info, so in th example everything is cached at the start of the appication with cache set to false and doesn't alter? And that changes is true is set, at which point cache has an expiry time?

    So there is an overhead if the life of the information is the same throught the applicate, but if you need the information to change its a viable options?

    How does the expiry work? eg how could I manage it?

    Although the article doesn't use resouce files in that example it uses a database. (the resource files are a previous article)

    Friday, June 1, 2018 2:27 PM
  • User475983607 posted

    I went ahead and downloaded the source code and there is no such code as...

    Cache = false; // By default, disable caching for performance

    The source code is actually...

            public BaseResourceProvider() {
                Cache = true; // By default, enable caching for performance
            }

    However, my comments on resources in ASP applications still applies just the code works opposite.  If Cache is true the value is read from memory otherwise the value is retrieved from the datastore whatever that might be.  

    Here is the code which is self explanatory.

            /// <summary>
            /// Returns a single resource for a specific culture
            /// </summary>
            /// <param name="name">Resorce name (ie key)</param>
            /// <param name="culture">Culture code</param>
            /// <returns>Resource</returns>
            public object GetResource(string name, string culture)
            {
                if (string.IsNullOrWhiteSpace(name))
                    throw new ArgumentException("Resource name cannot be null or empty.");
    
                if (string.IsNullOrWhiteSpace(culture))
                    throw new ArgumentException("Culture name cannot be null or empty.");
    
                // normalize
                culture = culture.ToLowerInvariant();
    
                if (Cache && resources == null) {
                    // Fetch all resources
                    
                    lock (lockResources) {
    
                        if (resources == null) {
                            resources = ReadResources().ToDictionary(r => string.Format("{0}.{1}", r.Culture.ToLowerInvariant(), r.Name));
                        }
                    }
                }
    
                if (Cache) {
                    return resources[string.Format("{0}.{1}", culture, name)].Value;
                }
    
                return ReadResource(name, culture).Value;
    
            }

    Friday, June 1, 2018 5:10 PM
  • User1034446946 posted

    Sorry it seems its been so long since i started looking it to it, i confused myself with the code I was working on.

    For some reason i never thought to look at the good from the point of view of the article, i just say it as a clean way of implementing a way to access information and possibly caching it.

    eg when i first look at the code i didn't see why if (Cache) didn't also check for the resourse, but its inline with the article.

    Another question, lets say I have an eccomerce site, and I have to cache lots of seperate bits of information, stock count, stock delivery time, product details for index, product details for product page

    Am i better using a central cache system which servers up all the options or use seperate ones, like the source codebut adapted for the information needed.

    Friday, June 1, 2018 10:17 PM
  • User-330142929 posted

    Hi EnenDaveyBoy,

    Another question, lets say I have an eccomerce site, and I have to cache lots of seperate bits of information, stock count, stock delivery time, product details for index, product details for product page

    Am i better using a central cache system which servers up all the options or use seperate ones, like the source codebut adapted for the information needed.

    Yes. As you understood. For general system optimization we use ASP.NET framework caching techniques such as page caching, partial page caching, or data caching (usually implemented by the system cache class). 

    To ease server pressure, we usually use a second-level system called external caching, such as Redis etc.

    Here is a relevant link, wish it is useful to you.

    https://stackoverflow.com/questions/38056786/in-memory-cache-vs-centralized-cache-in-a-distributed-system

    Best Regards.

    Abraham

    Thursday, June 21, 2018 3:13 AM