locked
Question about cache layer RRS feed

Answers

  • User753101303 posted

    Hi,

    Depends what you want. For example if you have a single web server you could cache data on this server but if you have multiple load balanced web servers you may want to use a distributed cache (which seems what memcached is). See  https://en.wikipedia.org/wiki/NoSQL for couchbase. And finally a usual relational db such as SQL Server borrow a bit of all that as it evolves...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 27, 2016 8:34 PM
  • User765422875 posted

    The code in that article uses the HttpRuntime.Cache and it was written in 2007. That being said, you have a few caching options with the latest version of ASP.NET.  Microsoft recommends using System.Runtime.Caching for all caching purposes. 

    From Microsoft:

    In the .NET Framework 3.5 and earlier versions, ASP.NET provided an in-memory cache implementation in the System.Web.Caching namespace. In previous versions of the .NET Framework, caching was available only in the System.Web namespace and therefore required a dependency on ASP.NET classes. In the .NET Framework 4, the System.Runtime.Caching namespace contains APIs that are designed for both Web and non-Web applications.

    There is also the .NET MemoryCache.

    The above solutions are not a good choice if you have a distributed architecture as you will not be able to easily synchronize your caches. If you do have a distributed architecture and require something robust - then I recommend Redis along with the .NET client developed by stackexchange.

    If you have single web server then System.Runtime.Caching or the MemoryCache are sufficient.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 27, 2016 8:43 PM
  • User895691971 posted

    Notice that you are posting this question in general ASP.NET and not Web Forms so I will share the answer from general ASP.NET's point of view. What caching is, depends mostly upon how your server and the relation to the web application is built. How often do you change the data that has to be accessed. Caching only works when the data to be responded with is same; static web pages. If your web page has to change too often, then chances are cache is just going to delay the overall performance of the application and (in my own opinion) doesn't make any sense.

    I have built a few applications and I have personally never worried about caching services or the cache providers in ASP.NET, because they are managed "perfectly" by the framework. There is no need to modify how IIS works. Secondly, Windows Server's Forefront TMG (formerly ISA Server) takes care of many things... I haven't yet seen Windows Server 2016 in action so cannot say how that acts but previous versions have a great control over the static data and most of the content is always cached. IIS Server itself also exposes these settings. But here is a rule of thumb that would give you an idea whether to cache a resource or not:

    1. Is that resource going to change?
    2. How often is the resource going to change?
    3. Is that resource same to all users, or do different user need to see a different view (like in case of changing the name field, date and time etc.)
    4. How much the caching improves the overall performance (not just a performance of single user, or the user connected on local network).

    By change I mean to say, is the resource dynamic or static. Noticing these things will help you understand how to build caching systems, or to fine tune them in your applications. Mostly, these changes are required for static web pages, because if the page is going to change then looking into the cache (when you have to build a new page each time) is just going to add extra overhead to the request-response time. 

    ASP.NET Core has a lot of static content in it, most pages are built down to native bytes and static HTML pages linked to CSS and JavaScript, which gives it a great execution speed, excellent performance and rocket-powered response generation speed. That is where you should (always!) consider adding the caching to the system. Because, you don't have to re-build the pages. 

    Long story short: That depends entirely on the content being cached, not the web application being used. Windows Server and IIS have their own implementation of caching and they are smart enough to know whether to cache a web resource or not! I won't bother tinkering with the configurations and would leave them as they are, unless I have to force framework to do something for me. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 4:52 AM
  • User36583972 posted

    Hi pamyral_279,

    In Web Application, Caching means to store something in memory that is being used frequently to provide better performance.

    As far as I know, Caching in ASP.Net has the following three types:

    1.page output caching

    This is a form of caching in ASP.Net that stores a copy of your Web page in the memory cache so that subsequent requests for the same Web page can be fetched directly from the cache.

    2.page fragment caching

    Page fragment caching is a caching strategy in which the Web page is cached partially.

    3.data caching

    ASP.Net exposes the Cache API for you to store data in the cache for retrieval later.

    Cache["key"] = "value";

    Is there best way to implement cache in web application

    You can try cache as often as you can and cache data properly in every layer of your application. When using data caching, you should implement a proper strategy to ensure that data in the cache is in sync with that in the data store.

    You can also take advantage of distributed cache managers like Memcached so that your caching strategy can also scale well and provide considerable performance gains -- you can use Memcached to store large data.

    Memcached is no new comer to the web. It is used by major players such as Wikipedia, Flickr, Twitter, Youtube, WordPress.com and Digg. The advantages of using Memcached are endless and getting it setup on your machine is really simple.

    Memcached for C# - A Walkthrough:

    http://deanhume.com/home/blogpost/memcached-for-c----a-walkthrough/62

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 8:15 AM
  • User765422875 posted

    No you should not have any issues.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 4:59 PM

All replies

  • User753101303 posted

    Hi,

    Depends what you want. For example if you have a single web server you could cache data on this server but if you have multiple load balanced web servers you may want to use a distributed cache (which seems what memcached is). See  https://en.wikipedia.org/wiki/NoSQL for couchbase. And finally a usual relational db such as SQL Server borrow a bit of all that as it evolves...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 27, 2016 8:34 PM
  • User765422875 posted

    The code in that article uses the HttpRuntime.Cache and it was written in 2007. That being said, you have a few caching options with the latest version of ASP.NET.  Microsoft recommends using System.Runtime.Caching for all caching purposes. 

    From Microsoft:

    In the .NET Framework 3.5 and earlier versions, ASP.NET provided an in-memory cache implementation in the System.Web.Caching namespace. In previous versions of the .NET Framework, caching was available only in the System.Web namespace and therefore required a dependency on ASP.NET classes. In the .NET Framework 4, the System.Runtime.Caching namespace contains APIs that are designed for both Web and non-Web applications.

    There is also the .NET MemoryCache.

    The above solutions are not a good choice if you have a distributed architecture as you will not be able to easily synchronize your caches. If you do have a distributed architecture and require something robust - then I recommend Redis along with the .NET client developed by stackexchange.

    If you have single web server then System.Runtime.Caching or the MemoryCache are sufficient.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 27, 2016 8:43 PM
  • User895691971 posted

    Notice that you are posting this question in general ASP.NET and not Web Forms so I will share the answer from general ASP.NET's point of view. What caching is, depends mostly upon how your server and the relation to the web application is built. How often do you change the data that has to be accessed. Caching only works when the data to be responded with is same; static web pages. If your web page has to change too often, then chances are cache is just going to delay the overall performance of the application and (in my own opinion) doesn't make any sense.

    I have built a few applications and I have personally never worried about caching services or the cache providers in ASP.NET, because they are managed "perfectly" by the framework. There is no need to modify how IIS works. Secondly, Windows Server's Forefront TMG (formerly ISA Server) takes care of many things... I haven't yet seen Windows Server 2016 in action so cannot say how that acts but previous versions have a great control over the static data and most of the content is always cached. IIS Server itself also exposes these settings. But here is a rule of thumb that would give you an idea whether to cache a resource or not:

    1. Is that resource going to change?
    2. How often is the resource going to change?
    3. Is that resource same to all users, or do different user need to see a different view (like in case of changing the name field, date and time etc.)
    4. How much the caching improves the overall performance (not just a performance of single user, or the user connected on local network).

    By change I mean to say, is the resource dynamic or static. Noticing these things will help you understand how to build caching systems, or to fine tune them in your applications. Mostly, these changes are required for static web pages, because if the page is going to change then looking into the cache (when you have to build a new page each time) is just going to add extra overhead to the request-response time. 

    ASP.NET Core has a lot of static content in it, most pages are built down to native bytes and static HTML pages linked to CSS and JavaScript, which gives it a great execution speed, excellent performance and rocket-powered response generation speed. That is where you should (always!) consider adding the caching to the system. Because, you don't have to re-build the pages. 

    Long story short: That depends entirely on the content being cached, not the web application being used. Windows Server and IIS have their own implementation of caching and they are smart enough to know whether to cache a web resource or not! I won't bother tinkering with the configurations and would leave them as they are, unless I have to force framework to do something for me. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 4:52 AM
  • User36583972 posted

    Hi pamyral_279,

    In Web Application, Caching means to store something in memory that is being used frequently to provide better performance.

    As far as I know, Caching in ASP.Net has the following three types:

    1.page output caching

    This is a form of caching in ASP.Net that stores a copy of your Web page in the memory cache so that subsequent requests for the same Web page can be fetched directly from the cache.

    2.page fragment caching

    Page fragment caching is a caching strategy in which the Web page is cached partially.

    3.data caching

    ASP.Net exposes the Cache API for you to store data in the cache for retrieval later.

    Cache["key"] = "value";

    Is there best way to implement cache in web application

    You can try cache as often as you can and cache data properly in every layer of your application. When using data caching, you should implement a proper strategy to ensure that data in the cache is in sync with that in the data store.

    You can also take advantage of distributed cache managers like Memcached so that your caching strategy can also scale well and provide considerable performance gains -- you can use Memcached to store large data.

    Memcached is no new comer to the web. It is used by major players such as Wikipedia, Flickr, Twitter, Youtube, WordPress.com and Digg. The advantages of using Memcached are endless and getting it setup on your machine is really simple.

    Memcached for C# - A Walkthrough:

    http://deanhume.com/home/blogpost/memcached-for-c----a-walkthrough/62

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 8:15 AM
  • User-513628628 posted

    Thank you all so much ! Forum is great !

    Thursday, July 28, 2016 12:58 PM
  • User-513628628 posted

    I have a question for deepalgor :

    If I change MemoryCache into HttpRuntime.Cache , i will not have any problem about architecture ! Currently , i only develop web in single web server !

    Thursday, July 28, 2016 1:03 PM
  • User765422875 posted

    No you should not have any issues.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 4:59 PM
  • User-513628628 posted

    Hi Deepalg

    This is code AddItemInCache :

     private void AddCacheItem(string rawKey, object value)
            {
                System.Web.Caching.Cache DataCache = HttpRuntime.Cache;
    
                // Make sure MasterCacheKeyArray[0] is in the cache - if not, add it
                if (DataCache[MasterCacheKeyArray[0]] == null)
                    DataCache[MasterCacheKeyArray[0]] = DateTime.Now;
    
                // Add a CacheDependency
                System.Web.Caching.CacheDependency dependency = new System.Web.Caching.CacheDependency(null, MasterCacheKeyArray);
                DataCache.Insert(GetCacheKey(rawKey), value, dependency, DateTime.Now.AddSeconds(CacheDuration), System.Web.Caching.Cache.NoSlidingExpiration);
            }

    Can you modify it into : System.Runtime.Caching ?
    Thank you so much !

    Tuesday, August 2, 2016 10:14 AM