Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer

Slides:



Advertisements
Similar presentations
Windows Basic and Dynamic Disk Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Advertisements

HTML Forms, GET, POST Methods Tran Anh Tuan Edit from Telerik Academy
Amazon S 3, App Engine Blobstore, Google Cloud Storage, Azure Blobs Svetlin Nakov Telerik Software Academy academy.telerik.com.
RPN and Shunting-yard algorithm Ivaylo Kenov Telerik Software Academy academy.telerik.com Technical Assistant
Telerik School Academy ASP.NET MVC.
Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor.
Telerik Software Academy Telerik School Academy.
Asynchronous Programming with C# and WinRT
Unleash the Power of JavaScript Tooling Telerik Software Academy End-to-end JavaScript Applications.
Telerik School Academy ASP.NET MVC.
Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team Telerik Software Academy.
Hybrid or Native?! Doncho Minkov Telerik Software Academy Senior Technical Trainer
Done already for your convenience! Telerik School Academy Unity 2D Game Development.
Processing Sequences of Elements Telerik School Academy C# Fundamentals – Part 1.
C# Fundamentals – Part I
NoSQL Concepts, Redis, MongoDB, CouchDB
Telerik Software Academy Telerik School Academy Creating E/R Diagrams with SQL Server.
The Business Plan and the Business Model Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System Analyst Telerik Corporation.
What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team Telerik Software Academy.
Making JavaScript code by template! Learning & Development Team Telerik Software Academy.
Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training Who, What, Why?
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Processing Matrices and Multidimensional Tables Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Learning & Development Telerik Software Academy.
Reading and Writing Text Files Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Telerik Software Academy ASP.NET Web Forms.
Classical OOP in JavaScript Classes and stuff Telerik Software Academy
Using Selenium for Mobile Web Testing Powered by KendoUI Telerik QA Academy Atanas Georgiev Senior QA Engineer KendoUI Team.
NoSQL Concepts, Redis, MongoDB, CouchDB Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
New features: classes, generators, iterators, etc. Telerik Academy Plus JavaScript.Next.
Throwing and Catching Exceptions Tran Anh Tuan Edit from Telerik Software Academy
Loops, Conditional Statements, Functions Tran Anh Tuan Edit from Telerik Academy
Telerik Software Academy ASP.NET Web Forms.
Private/Public fields, Module, Revealing Module Learning & Development Team Telerik Software Academy.
Building Data-Driven ASP.NET Web Forms Apps Telerik Software Academy ASP.NET Web Forms.
Course Introduction Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Nikolay Kostov Telerik Software Academy Senior Software Developer and Trainer
Telerik Software Academy End-to-end JavaScript Applications.
General and reusable solutions to common problems in software design Vasil Dininski Telerik Software Academy academy.telerik.com Intern at Telerik Academy.
Planning and Tracking Software Quality Yordan Dimitrov Telerik Corporation Team Leader, Team Pulse, Team Leader, Team Pulse, Telerik Corporation,
What you need to know Ivaylo Kenov Telerik Corporation Telerik Academy Student.
Data binding concepts, Bindings in WinJS George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
Pavel Kolev Telerik Software Academy Senior.Net Developer and Trainer
Objects, Properties, Primitive and Reference Types Learning & Development Team Telerik Software Academy.
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Doing the Canvas the "easy way"! Learning & Development Telerik Software Academy.
Creating and Running Your First C# Program Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Correctly Formatting the Source Code Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
The past, the present, the future Learning & Development Team Telerik Software Academy.
Learn to Design Error Steady Code Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Processing Sequences of Elements Telerik Software Academy C# Fundamentals – Part 2.
Telerik JavaScript Framework Telerik Software Academy Hybrid Mobile Applications.
Building Rock-Solid Software Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Telerik Software Academy Databases.
Things start to get serious Telerik Software Academy JavaScript OOP.
Learning & Development Mobile apps for iPhone & iPad.
Processing Matrices and Multidimensional Tables Telerik Software Academy C# Fundamentals – Part 2.
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Implementing Control Logic in C# Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical trainer
Inheritance, Abstraction, Encapsulation, Polymorphism Telerik Software Academy Mobile apps for iPhone & iPad.
Mocking tools for easier unit testing Telerik Software Academy High Quality Code.
What why and how? Telerik School Academy Unity 2D Game Development.
Windows Security Model Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Caching Data in ASP.NET MVC
Presentation transcript:

Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer

 Why do we need caching?  ASP.NET Output Caching  OutputCache page directive  Cache profiles  Post-cache substitution  Page fragment caching  Data caching  Cache dependencies 2

 Benefits of Caching  Performance  Speed – Reduced response time  Efficiency – Reduced infrastructure usage  CPU time, database utilization, network bandwidth  Reduce DB round trips  Costs of Caching  Staleness (out-of-date)  Need to check and refresh data 4

 For relatively static pages, rendered content can be cached  OutputCache page directive in ASP.NET  OutputCache action filter in ASP.NET MVC and ASP.NET Web API 6 [OutputCache(Duration=10)] public ActionResult MyAction() {... }

NameValuesDescription DurationnumberTime, in seconds, that the page or user control is cached Location'Any' 'Client' 'Downstream' 'Server' 'None' Controls the header and meta tags sent to clients indicating where this page can be cached. 'Any' means that the page can be cached on the browser client, a downstream server, or the server. 'Client' means that the page will be cached on the client browser only. 'Downstream' means that the page will be cached on a downstream server and the client. 'Server' means that the page will be cached on the server only. 'None' disables output caching for this page. VaryByCustom'Browser' custom string Either vary the output cache by browser name and version, or by a custom string, which must be handled in an overridden version of GetVaryByCustomString(). VaryByHeader'*' header names A semi-colon separated list of strings representing headers submitted by a client. VaryByParam'none' '*' param name A semi-colon separated list of strings representing query string values in a GET request, or variables in a POST request. 7

 3 types of caching with the 'location' attribute:  server – pages are cached in the worker process  client – pages are cached in client browsers that support it  proxy – pages are potentially cached by downstream proxies 8 ValueCache-Control headerExpires header?Cached on server? 'Any'publicYes 'Client'privateYesNo 'Downstream'publicYesNo 'Server'no-cacheNoYes 'None'no-cacheNo

 VaryByParam='none' means a unique instance of a page is cached per verb  One for GET, one for POST  If your page changes rendering based on query string or post body, you can use VaryByParam to cache additional versions  "*" means cache a unique page for each different post body and/or query string (beware)  "varname" means cache a unique page for different values of this variable (semi-colon delimited list for multiple variables) 9

 A semi-colon separated list of strings representing query string values in a GET request, or variables in a POST request. 10 ValueDescription 'none'One version of page cached per request type (GET, POST, HEAD) '*'N versions of page cached based on query string and/or POST body variables 'v1'N versions of page cached based on value of v1 variable in query string or POST body 'v1;v2'N versions of page cached based on value of v1 and v2 variables in query string or POST body

 Other options for creating additional cached pages  VaryByHeader – any HTTP header  VaryByCustom – specify 'Browser' for unique cache entry for each browser type  VaryByCustom with GetVaryByCustomString method in HttpApplication derivative  Return your own string indicating whether it's a separate cache entry or not 11

 Cache profiles  Stored in web.config file  No need to recompile 13 <system.web> <add name="ShortLived" duration="10" <add name="ShortLived" duration="10" varyByParam="none" location="Server" /> varyByParam="none" location="Server" /> <add name="LongLived" duration="3600" <add name="LongLived" duration="3600" varyByParam="none" location="Server" /> varyByParam="none" location="Server" /> </system.web>

 Output cache an entire page except some part  This "some part" must consist of a simple string  Use Response.WriteSubstitution with callback  Or use control with associated callback method 15

 Can elect to output cache an entire page except for specifically generated dynamic elements 16 protected static string GetCurrentTime(HttpContext ctx) { protected static string GetCurrentTime(HttpContext ctx) { return DateTime.Now.ToString(); return DateTime.Now.ToString(); }</script><html><body> </body></html>

 Common for portions of a page to remain static  Menus, navigation bars, footers, headers, etc.  To cache a page fragment, encapsulate in.ascx and mark with OutputCache directive  Shared property indicates whether one instance is cached to share across pages  VaryByControl indicates a control value influences unique cache entries 18

 Internally, OutputCaching is built using a sophisticated data cache  Available to you directly through Page.Cache for your own caching needs if (this.Cache["time"] == null) { this.Cache["time"] = DateTime.Now; this.Cache["time"] = DateTime.Now;} this.currentTimeSpan.InnerText = ((DateTime)this.Cache["time"]).ToString(); ((DateTime)this.Cache["time"]).ToString(); 21

 When adding cache entries, several attributes can be specified  Each insertion creates a new CacheEntry 22 PropertyTypeDescription KeystringUnique key used to identify this entry in the cache DependencyCacheDependencyA dependency this cache entry has -either on a file, a directory, or another cache entry -which when changed, should cause this entry to be flushed ExpiresDateTimeA fixed date and time after which this cache entry should be flushed SlidingExpirationTimeSpanThe time between which the object was last accessed and when the object should be flushed from the cache PriorityCacheItemPriorityHow important this item is to keep in the cache compared to other cache entries (used when deciding how to remove cache objects during scavenging) OnRemoveCallbackCacheItemRemoved (Callback) A delegate which can be registered with a cache entry for invocation upon removal

 Example of setting cache entry parameters 23 if (this.Cache["time"] == null) { // this.Cache["time"] = DateTime.Now; // this.Cache["time"] = DateTime.Now; Cache.Insert( Cache.Insert( "time", // key "time", // key DateTime.Now, // object DateTime.Now, // object null, // dependencies null, // dependencies DateTime.Now.AddSeconds(10), // absolute exp. DateTime.Now.AddSeconds(10), // absolute exp. TimeSpan.Zero, // sliding exp. TimeSpan.Zero, // sliding exp. CacheItemPriority.Default, // priority CacheItemPriority.Default, // priority null); // callback delegate null); // callback delegate} this.currentTimeSpan.InnerText = ((DateTime)this.Cache["time"]).ToString(); ((DateTime)this.Cache["time"]).ToString();

 Objects can be explicitly taken out of the cache  Explicitly by calling Remove  Cache can remove item implicitly for a variety of reasons (Data expiration, Memory consumption)  Low priority data removed first  Can register for removal notification, including reason 24

 Cache dependencies can ensure that data is not stale  Cache entry can be flushed when:  A file changes  A directory changes  Another cache entry is removed  Something in the database changes  SQL Cache Dependencies 26

форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране

1. Create new project from the default ASP.NET Web Forms application template and:  Make About.aspx page cacheable for 1 hour  Create cacheable user control 2. * Add a new web forms page that shows the list of all files in a given directory and caches them till the directory changes 3. * Learn more about caching by reading about  Data source caching  SQL cache dependencies  xploring-Caching-in-ASP-NET xploring-Caching-in-ASP-NET xploring-Caching-in-ASP-NET 29

 C# Telerik Academy  csharpfundamentals.telerik.com csharpfundamentals.telerik.com  Telerik Software Academy  academy.telerik.com academy.telerik.com  Telerik Facebook  facebook.com/TelerikAcademy facebook.com/TelerikAcademy  Telerik Software Academy Forums  forums.academy.telerik.com forums.academy.telerik.com 30