Session, TempData, Cache Andres Käver, IT Kolledž 2015 1.

Slides:



Advertisements
Similar presentations
Not like the State of Virginia. What is State in ASP.NET? Services (like web services) are Stateless. This means if you make a second request to a server,
Advertisements

UFCE8V-20-3 Information Systems Development 3 (SHAPE HK)
ASP.NET Best Practices Dawit Wubshet Park University.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
CIS 451: ASP Sessions and Applications Dr. Ralph D. Westfall January, 2009.
Turners SharePoint Web Site How we did it. 2 Page Anatomy Custom Search Web Part Custom Search Web Part Data Form Web Parts Content Query Web Part HTML.
Caching MacDonald Ch. 26 MIS 424 MIS 424 Professor Sandvig Professor Sandvig.
Overview  Introduction to ASP.NET caching  Output caching  Fragment caching  Data caching 1.
XP Tutorial 9 New Perspectives on JavaScript, Comprehensive1 Working with Cookies Managing Data in a Web Site Using JavaScript Cookies.
Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
ASHIMA KALRA.  INTRODUCTION TO JSP INTRODUCTION TO JSP  IMPLICIT OBJECTS IMPLICIT OBJECTS  COOKIES COOKIES.
Microsoft ASP.NET: An Overview of Caching Holly Mazerolle Developer Support Engineer Microsoft Developer Support Microsoft Corporation.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
CSC 2720 Building Web Applications Cookies, URL-Rewriting, Hidden Fields and Session Management.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC - Models.
Microsoft ASP.NET: An Overview of Caching. 2 Overview  Introduction to ASP.NET caching  Output caching  Data caching  Difference between Data Caching.
LiveCycle Data Services Introduction Part 2. Part 2? This is the second in our series on LiveCycle Data Services. If you missed our first presentation,
Ideas to Improve SharePoint Usage 4. What are these 4 Ideas? 1. 7 Steps to check SharePoint Health 2. Avoid common Deployment Mistakes 3. Analyze SharePoint.
Murach’s ASP.NET 4.0/VB, C1© 2006, Mike Murach & Associates, Inc.Slide 1.
Maintaining State MacDonald Ch. 9 MIS 324 MIS 324 Professor Sandvig Professor Sandvig.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
Caching Chapter 12. Caching For high-performance apps Caching: storing frequently-used items in memory –Accessed more quickly Cached Web Form bypasses:
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
Christopher M. Pascucci Basic Structural Concepts of.NET Managing State & Scope.
ASP.NET State Management. Slide 2 Lecture Overview Client state management options Cookies Server state management options Application state Session state.
ASP.NET OPTIMIZATION. Why Optimize? $$$ Whether you build applications for customers or not, enhanced applications save money.
ASP.NET Caching - Pradeepa Chandramohan. What is Caching? Storing data in memory for quick access. In Web Application environment, data that is cached.
Module 7: Creating a Microsoft ASP.NET Web Application.
Anatomy of an ASP.NET Page. Slide 2 Compiling an ASP.NET Page An ASP.NET page is compiled when it is rendered for the first time A page is compiled to.
® IBM Software Group © 2007 IBM Corporation Best Practices for Session Management
State Management. Agenda View state Application cache Session state ProfilesCookies.
CP476 Internet Computing CGI1 Cookie –Cookie is a mechanism for a web server recall info of accessing of a client browser –A cookie is an object sent by.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
Getting started with ASP.NET MVC Dhananjay
State Management. Agenda View state Application cache Session state ProfilesCookies.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
Internationalization Andres Käver, IT Kolledž 2015.
Workflow Service Host Persistence (Instances) Persistence (Instances) Monitoring Activity Library Receive Send... Management Endpoint Persistence Behavior.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
Distributed Server Scheduler Eyal Serero Alex Fishgate Supervisor : Vitaly Suchin.
Introduction to ASP.NET, Second Edition2 Chapter Objectives.
BIT 286: Web Applications ASP.Net MVC. Objectives Applied MVC overview Controllers Intro to Routing Views ‘Convention over configuration’ Layout files.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
Page Redirection When you click a URL to reach to a page X but internally you are directed to another page Y that simply happens because of page re- direction.
Managing State Chapter 13.
Asp.Net MVC Conventions
Building Web Applications with Microsoft ASP
Caching Data in ASP.NET MVC
State Management.
ASP.NET Caching.
Web Caching? Web Caching:.
Web Systems Development (CSC-215)
Web Systems Development (CSC-215)
MIS Professor Sandvig MIS 424 Professor Sandvig
Data Structures and Database Applications View and Session Data
HTML5 and Local Storage.
ASP.NET 4.0 State Management Improvements – Deep Dive
This presentation document has been prepared by Vault Intelligence Limited (“Vault") and is intended for off line demonstration, presentation and educational.
Active server pages (ASP.NET)
Caching.
Client-Server Model: Requesting a Web Page
Hypertext Preprocessor
Presentation transcript:

Session, TempData, Cache Andres Käver, IT Kolledž

Session  System.Web.HttpContext.Current.Session  Works like ViewBag – you can save anything into it  Default lifetime of session is 20 minutes  Is persisted over all requests, unlike TempData  TempData is valid only for single redirect  Typecasting and checking for null values is required 2

Session  Lifetime 20 min  Web.config, system.web section 3

Session  Saving data to session  Accessing data from session 4 System.Web.HttpContext.Current.Session["sessionString"] = sessionValue; var myStr = TryCast( System.Web.HttpContext.Current.Session("sessionString"), [String]);

Session - Extension method 5 public static class SessionExtensions { public static T GetDataFromSession ( this HttpSessionStateBase session, string key) { return (T)session[key]; } public static void SetDataToSession ( this HttpSessionStateBase session, string key, object value) { session[key] = value; } Session.SetDataToSession ("key1", sessionValue); var value = Session.GetDataFromSession ("key1");

Session - methods  Add  Clear  Remove  RemoveAll, RemoveAt  IsNewSession  IsReadOnly 6

Cache  Is live data really needed?  Live data can be really expensive (cpu, database, bandwith, time)  In most cases, live data is not needed. Few minutes old data is good enough, but web page that loads few minutes – impossible.  After well structured and optimized code – cache can help. 7

Cache - Output  OutputCache atribute, with duration parameter  Duration is mandatory, time in seconds – for how long will this output stay in cache 8 [OutputCache(Duration=60)] public ActionResult Index() { return View(); }

Cache - Output  When to generate new content?  When parameters change: 9 [OutputCache(Duration = 60, VaryByParam = "search")] public ActionResult Contact(string search) { return View(); }

Cache - Output  When headers change 10 [OutputCache(Duration = 60, VaryByHeader = "Accept-Language")] public ActionResult Contact(string search) { return View(); }

Cache - Output  Location – location of cached item Any – stores the output cache on the client’s browser, on the proxy server (or any other server) that participates in the request, or on the server where the request is processed. Default value. Client – stores the output cache on the client’s browser. Downstream – stores the output cache on any cache-capable devices (other than the origin server) that participates in the request. Server – stores the output cache on the Web server. None – turns off the output cache. 11

Cache - Output  SqlDependency – set a dependency between the db and the cache. At any time something is changed within the tracked table(s), new instance of the action result will be created in the cache.  CacheProfile –make an Output cache profile in the Web.config – so no recompile is needed for cache configuration. 12

Cache - Output 13 <add name="MyCacheProfile" duration="30" varyByParam="id" location="Any" /> …… [OutputCache(CacheProfile = "MyCacheProfile")] public ActionResult Index() { return View(); }

Cache - Data  Cache only the data and not the whole page  Use “HttpContext.Cache”  Only one instance of the Cache class per application domain 14 public ActionResult Index(){ if (System.Web.HttpContext.Current.Cache["time"] == null){ System.Web.HttpContext.Current.Cache["time"] = DateTime.Now; } ViewBag.Time = ((DateTime)System.Web.HttpContext.Current.Cache["time"]).ToString(); return View(); }

Cache - Data 15 public Object Add( string key, Object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback )

Cache - Data  Key – the name with which to access item.  Value – the object that we store in the cache.  Dependencies – used to configure cache dependencies, indicating when object will be removed from the cache.  AbsoluteExpiration – configures the time for which object will stay in the cache.  SlidingExpiration – sets how much time after the last accessing cached object will expire. To be able to use this configure the AbsoluteExpiration property to Cache.NoAbsoluteExpiration. 16

Cache - Data  CacheItemPriority – defines the priority of the cached item – the items with a lower priority will be removed first when the server releases the system memory. The default value is normal.  CacheItemRemovedCallback – defines what happens when a data in the cache expires. This property needs an instance of the CacheItemRemovedCallback class. The method that we pass to the CacheItemRemovedCallback constructor should be ‘void’ with the following parameters – string key, Object val, CacheItemRemovedReason reason. 17