SESSION
A session is defined as the period of time that a
unique user interacts with a Web application. When a new user begins to
interact with the application, a new session ID is generated and
associated with all subsequent requests from that same client and stored
in a cookie on the client machine.
Session state is maintained in an instance of the
HttpSessionState class and is accessible through the Session property of
both the Page and HttpContext classes. It is not necessary for you to
instantiate the Session Object. An instance is automatically provided
for you. The Session object allows you to maintain a list of name/value
pairs that can be accessed by any Web page in your application.
vb.net Dim username As String
If Session("userName") IsNot Nothing Then
username = Session("userName")
Else
Session("userName") = "John"
End If
string username;
if (Session["userName"] != null)
{
username = (String)Session["userName"];
}
else
{
Session["userName"] = "John";
}
For every client Session data store separately,
means session data is stored as per client basis. Along with advantages,
some times session can causes performance issue for heavy traffic sites
because its stored on server memory and clients read data from the
server itself.
CACHING
Caching is the ability to store page output and data
in memory for the first time it is requested and later we can quickly
retrieve them for multiple, repetitious client requests. It is like
keeping a copy of something for later use, so we can reduce the load
factor on Web servers and database servers by implementing caching on a
Web application.
vb.net
Cache.Add("dataset", dataset, Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, New TimeSpan(0, 0, 60), System.Web.Caching.CacheItemPriority.[Default],Nothing)
C#
Cache.Add("dataset", dataset, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, 60), System.Web.Caching.CacheItemPriority.Default, null);
Retrieve data from cache
vb.netDim ds as New DataSetds = CType(Cache("dataset"),DataSet)
DataSet ds = new DataSet();ds = (DataSet) Cache["dataset"];
It becomes more efficient to serve a copy of the
initial response, rather than regenerating the same response for every
client request.
ASP.NET provides two types of caching, Application
Cache and Page Output Cache. The application cache, which provides a
programmatic way for you to store arbitrary data in memory using
key/value pairs. The Page output caching, which saves the output of page
processing and reuses the output instead of re-processing the page when
a user requests the page again.
VIEWSTATE
The ViewState indicates the status of the page when
submitted to the server. The retention of state is called the view state
and is stored within a hidden control on the page. When an ASP.NET page
is submitted to the server, the state of the controls is encoded and
sent to the server at every form submission in a hidden field known as
__VIEWSTATE. The server sends back the variable so that when the page is
re-rendered, the controls render at their last state, so no extra
programming is needed. If you try to view source code from browser you
can see that ASP .NET has added a hidden field in the form to maintain
the ViewState
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTkwNjc4NTIwMWRkL5PbikezkXhkI2kmrh3VUjVDGoU=" />
Maintaining the ViewState is the default setting
for ASP.NET Web Forms. By default, most ASP.NET controls enable view
state. If you want to not maintain the ViewState, include the directive
<%@ Page EnableViewState="false" %> at the top of an .aspx page or
add the attribute EnableViewState="false" to any control. You can also
maintain ViewState for all pages in a web.config file .
<configuration> <system.web> <pages buffer="true" enableSessionState="true" enableViewState="true" autoEventWireup="true" /> </system.web> </configuration>
View state is a great idea to store information in
a page, but if you wish to save more complex data, and keep them from
page to page, you should look into using cookies or sessions is better.
COOKIES
Cookies provide the ability to store small amounts of
data on a clients machine. It is often used to store user preferences,
session variables, or identity. When the user visits your Web site
another time, the application can retrieve the information it stored
earlier.
The browser is responsible for managing cookies on
a user system. Cookies can be created, accessed, and modified directly
by script running on the client and pass between the client and server
during requests.
Writing Cookies
vb.net Response.Cookies("userName").Value = "John"
Response.Cookies("userName").Expires = DateTime.Now.AddDays(1)
Response.Cookies["userName"].Value = "John";Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);
Reading Cookies
vb.net If Not Request.Cookies("userName") Is Nothing Then
Label1.Text = Server.HtmlEncode(Request.Cookies("userName").Value)
End If
if(Request.Cookies["userName"] != null)Label1.Text = Server.HtmlEncode(Request.Cookies["userName"].Value);
Cookies can be either temporary or persistent.
Temporary cookies are stored in the clients browser. These cookies are
saved only while your web browser is running. Persistent cookies, on
the other hand, are stored on the hard disk of the client computer as a
text file. They stay on your hard disk and can be accessed by web
servers until they are deleted or have expired. These cookies can be
retrieved by the Web application when the client establishes another
session with it.
Cookies are limited to 4096 bytes in size and are
only capable of storing strings. Browsers also impose limitations on how
many cookies your site can store on the users computer. Most browsers
allow only 20 cookies per site; if you try to store more, the oldest
cookies are discarded.
No comments:
Post a Comment