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:
- Thanks for the reviews! Mr_Automation, Nateve, chubb5000, Travelerbell, LaCaren, ryanwebjackson, LuckyCoding
- SwanseaCon 2016 is coming up, September 12th – 13th in South Wales
- We have tickets to give away, tweet us at @CodingBlocks with the hashtag #SwanseaCon if you want to go!!!
- Awesome sounding topics like…Refactoring and Code Smells, 10x Developer Myth and Jurassic Park to Microservices
- Save yourself £450.00!!
- Reddit has been kind, Thank you so much!
- Great comments on the last (and really, all) episode: www.codingblocks.net/episode45
- Russell Hammet: Grace Hopper discussing, visualizing nanoseconds
- Carole Anne, always great perspective on our poll – and how discimination can masquerade as other things – great tie in with impostor syndrome
- Jeremy Shannon: Low Level Details for High Level Developers : https://docs.google.com/presentation/d/1azqLjBh2U0V5_-w_SXi6vDC8uiRq6VNW1s_-2B2Kho4/edit?usp=sharing
- Amazon wants to ship your package before you actually buy it! (example of temporal locality) http://blogs.wsj.com/digits/2014/01/17/amazon-wants-to-ship-your-package-before-you-buy-it/
- Intel and Micron’s memory breakthrough – 3D Xpoint
- Meetups on ReactJS + Elixir + RethinkDB, and Reactive apps
- 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
- Reduce database/service loads
- Reduce network traffic
- Improve performance
- Stale data!
- You get blamed for everything!
ASP.NET Caching – Pre-ASP.NET Core
- 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
- Non web-server scenario: .dat files, .config files, registry, AD information
- LifeCycle: Request, Session, Application
- Request: HttpContext.Items
- 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
- 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
- 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
123456<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"" %>
- Write your own provider! https://msdn.microsoft.com/en-us/magazine/gg650661.aspx
- 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”
- 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”
- ASP.NET Caching Overview https://msdn.microsoft.com/en-us/library/ms178597.aspx
- Cache Item Removal Notification https://msdn.microsoft.com/en-us/library/7kxdx246.aspx
- ASP.NET Core Distributed Cache https://docs.asp.net/en/latest/performance/caching/distributed.html
- ASP.NET Core Caching https://docs.asp.net/en/latest/performance/caching/index.html
- The HttpContext Items Collection http://odetocode.com/articles/111.aspx
- Mr. Automation – https://www.voidtools.com/
- WSL – https://msdn.microsoft.com/en-us/commandline/wsl/install_guide
- Want to make your friends go wow and look like a hacker? – madvikinggod
- 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