DEV Community

CodingBlocks

Episode 46 – Caching in the Application Framework

Storing smaller subsets of data in a faster, closer memory can make astronomical differences in performance. This episode we’re talking about the caching tools and techniques that application frameworks provide.

The original version of the shownotes can be found at:
http://www.codingblocks.net/episode46

New Poll!

#yop-poll-container-21_yp57c106f222dd5 { width: 1000; background:#fff; padding:10px; color:#555; overflow:hidden; font-size:12px; } #yop-poll-container-21_yp57c106f222dd5 input[type='text'] { margin:0px 0px 5px 0px; padding:2%; width:96%; text-indent:2%; font-size:12px; } .yop-poll-name-21_yp57c106f222dd5 { font-size:14px; font-weight:bold; } #yop-poll-questions-container-21_yp57c106f222dd5 { font-size:14px; margin:5px 0px; } .yop-poll-question-container-21_yp57c106f222dd5 { padding: 2px; } .yop-poll-question-21_yp57c106f222dd5 { font-style: italic; text-align: center; margin-bottom: 21px; margin-top: -10px; width: 100%; } .yop-poll-answers-21_yp57c106f222dd5 { } .yop-poll-answers-21_yp57c106f222dd5 ul { list-style: none outside none; margin: 0; padding: 0; } .yop-poll-li-answer-21_yp57c106f222dd5 { font-style:normal; margin:0px 0px 10px 0px; padding:0px; font-size:12px; margin-bottom:20px; margin-bottom:20px; } .yop-poll-li-answer-21_yp57c106f222dd5 input { margin:0px; float:none; } .yop-poll-li-answer-21_yp57c106f222dd5 label { margin:0px; font-style:normal; font-weight:normal; font-size:12px; float:none; } .yop-poll-results-21_yp57c106f222dd5 { font-size: 12px; font-style: italic; font-weight: normal; margin-left: 15px; } .yop-poll-customs-21_yp57c106f222dd5 { } .yop-poll-customs-21_yp57c106f222dd5 ul { list-style: none outside none; margin: 0; padding: 0; } .yop-poll-li-custom-21_yp57c106f222dd5 { padding:0px; margin:0px; font-size:14px; } /* Start CAPTCHA div style*/ #yop-poll-captcha-input-div-21_yp57c106f222dd5 { margin-top:5px; } #yop-poll-captcha-helpers-div-21_yp57c106f222dd5 { width:30px; float:left; margin-left:5px; height:0px; } #yop-poll-captcha-helpers-div-21_yp57c106f222dd5 img { margin-bottom:2px; } #yop-poll-captcha-image-div-21_yp57c106f222dd5 { margin-bottom:5px; } #yop_poll_captcha_image_21_yp57c106f222dd5 { float:left; } /* End CAPTCHA div style*/ .yop-poll-clear-21_yp57c106f222dd5 { clear:both; } #yop-poll-vote-21_yp57c106f222dd5 { } /* Start Result bar*/ .yop-poll-results-bar-21_yp57c106f222dd5 { background:#f5f5f5; height:10px; } .yop-poll-results-bar-21_yp57c106f222dd5 div { background:#555; height:10px; } /* End Result bar*/ /* Start Vote Button*/ #yop-poll-vote-21_yp57c106f222dd5 div#yop-poll-vote-21_yp57c106f222dd5 button { float:left; } #yop-poll-vote-21_yp57c106f222dd5 div#yop-poll-results-21_yp57c106f222dd5 { float: right; margin-bottom: 20px; margin-top: -20px; width: auto; } #yop-poll-vote-21_yp57c106f222dd5 div#yop-poll-results-21_yp57c106f222dd5 a { color:#555; text-decoration:underline; font-size:12px; } #yop-poll-vote-21_yp57c106f222dd5 div#yop-poll-back-21_yp57c106f222dd5 a { color:#555; text-decoration:underline; font-size:12px; }#yop-poll-vote-21_yp57c106f222dd5 div#yop-poll-archive-21_yp57c106f222dd5 a { color:#555; text-decoration:underline; font-size:12px; } #yop-poll-vote-21_yp57c106f222dd5 div { float:left; width:100%; } /* End Vote Button*/ /* Start Messages*/ #yop-poll-container-error-21_yp57c106f222dd5 { font-size:12px; font-style:italic; color:red; text-transform:lowercase; margin-bottom:20px; text-align:center; } #yop-poll-container-success-21_yp57c106f222dd5 { font-size:12px; font-style:italic; color:green; margin-bottom:20px; text-align:center; } /* End Messages*/ .yop-poll-clear-21_yp57c106f222dd5 { clear: both; }#yop-poll-container-21_yp57c106f222dd5 img { max-width: 1000; } .yop-poll-forms-display{}
What’s your favorite source of caffeine?
  • Coffee: Fancy stuff, Espresso, Cold Brew, etc
  • Coffee: Like, for Normal People
  • Energy Drinks: Red Bull / Monster, etc
  • Soda: Mountain Dew, Dr. Pepper, Coke Zero, etc
  • The Hard Stuff: 5 Hour Energy, Caffeine Pills
  • None: My body is a temple / wonderland
  • Other

Ready Player Oner

Podcast News

Topic: Caching in your Application Framework

  • Last episode, we talked about hardware latency numbers that “all programmers should know”
  • Relative speeds of cpu to sending packets to the other side of the world…300 million times slower!
  • Storing smaller subsets of data in a faster, closer memory can make astronomical differences
  • Knowing these numbers help you make smart architectural decisions about your app,
    For example – if you expect you’re traffic to come in quicker than your data changes, or if you don’t care about showing up-to-date data – you can get your data 600x faster by fetching it from RAM then from the internet!
  • Speed is important, both for UX – but also because the quicker you can handle requests * The less resources you need $$$
  • Today, we’re going to focus in on the types of caching that you can make use of as a programmer
  • We’re going to do this by focusing in on one specific framework: ASP.Net
  • DON’T WORRY – If you’re not a Microsofty!!!
  • The techniques we’re talking about are similar to caching you’ll see in other frameworks and languages – we just wanted to choose something we’re familiar with

ASP.NET and Application Caching Overview

What is ASP.NET?

  • Open-Source framework for creating web applications and web services
  • Provides tools for dealing with web requests, responses, sessions, cookies, as well as utilities for writing html
  • Works in conjunction w/ .Net, which provides abilities like system I/O, networking, everything else
  • When would you use ASP.NET over…NodeJs, RoR, PHP, etc
  • When would you NOT choose ASP.NET?

When it comes to writing ASP, there are a two major sides to it:

  • HTML Templating: Razor, DotLiquid
  • Compiled language: C#, VB, F#, Any .NET language

Framework provides a couple types of caching to the programmer: Application cache, page output cache, and attribute caching
Loads of other caching going on underneath the covers, GAC, Nuget, etc – but we’re focused on ASP.Net

Benefits of caching in the application

  • Reduce database/service loads
  • Reduce network traffic
  • Improve performance

Cons

  • Stale data!
  • Complexity
  • You get blamed for everything!

ASP.NET Caching – Pre-ASP.NET Core

Application Cache – uses Key Value Pairs

  • HttpRuntime.Cache
  • What to use for a cache key? a date? a userID? a guid?
  • Some good uses:
    • Looking up site/common settings
    • Shared connections connections (done automatically)
    • User Info
    • HttpRuntime.Cache
    • Non web-server scenario: .dat files, .config files, registry, AD information
    • LifeCycle: Request, Session, Application
    • Request: HttpContext.Items

Page Output Cache

  • Whole Page
  • Can even cache browser/mobile specific versions
  • User Control / ASP.NET Control
  • Extreme examples, Static site generators like Jekyll!
  • Can cache for a specific period of time
  • Cache based off browser version or language preference
  • Evict cache entries based on events

Method level caching

  • Caching the Result of a Method using PostSharp – http://doc.postsharp.net/example-cache
  • Extensible – can be used to cache to any type of data storage
  • Can be configured on a page by page basis or in bulk
  • Full page caching
  • Partial page caching
    • Control caching / fragment caching – done via user controls that are set as cacheable
    • Post cache substitution – whole page is cached but pieces of the page are substitutable as the content is put in non-cacheable sections – this is the inverse of control caching
  • Ability to cache based on page parameters

ASP.NET Core Caching

Extensible output caching

  • Don’t roll your’e own caching implimentation – .Net does it better!
  • Configure one or more custom output cache providers
  • These storage options can include local or remote disks, cloud storage, and distributed cache engines.
  • You can tailor to your needs – cache top 10 pages in memory and less served pages to disk
    Web.config example:
    <outputCache defaultProvider=""AspNetInternalProvider"">
     <providers>
       <add name=""DiskCache"" 
           type=""Test.OutputCacheEx.DiskOutputCacheProvider, DiskCacheProvider""/>
     </providers>
    </outputCache>

Defined per page or per control:

<%@ OutputCache Duration=""60"" VaryByParam=""None"" providerName=""DiskCache"" %>

Automatic data removal

  • Memory on the server is low – scavenging
  • Item in the cache has expired
    • Rolling expiration – if the page is hit, then the expiration is extended out by the predefined period of
    • Absolute expiration – the page expires at the given time regardless of how many times it’s accessed”
  • Item’s dependency changed
    • Key dependency – one item in the cache is dependent on another item in the cache using the k in kvp – if the depended upon key is removed so is the dependent item
    • File dependency – if the file is changed or deleted then the dependent item will be removed from the cache
    • SQL Server dependency – changes in a table or on changes in a row (SQL 2005 and above?)
    • Aggregate dependency – item depends on multiple items in the cache – if any of them change the dependent item is purged from the cache
    • Custom dependency – you come up with your own code to trigger clearing a cached item”

Notification of Cache Item Removal

  • Can be used to identify when an item is removed for cache tuning, or auto-filling the cache pre-emptively
  • CacheItemRemovedCallback delegate
    • This delegate must be available at all times, so a static class makes sense – must be thread safe
    • Typically this is put in a separate class from the Cache reader / writer because those typically only exist when being accessed”

Resources we Like

Tip of the Week

Show summary

  • Episode 45 we talked about hardware latency, and showed how saving copies of your data in faster/closer locations can save you buckets of time
  • This episode, 46, we focused on a particular web framework and discussed some of the tools that frameworks provide you – the programmer
  • Don’t forget there are a lot of other layers in place, ISP, CDN, Browser, etc
  • This episode we took a look at caching in ASP.Net as an example of the kinds of caching that are available to you in an application framework
  • In future episodes, we’re planning on diving into the computer “science” of caching, by looking at common data structures, algorithms, and some common strategies (like memoization, search engines, and CDN) that can make astronomical differences in performance

Episode source