Introduction
In this section we will touch base on one of important concepts in .Net Caching.
Happy job hunting......
(B) What is an application object?
Application object can be used in situation where we want data to be shared across users globally.
(I) what is the difference between Cache object and application object?
The main difference between the Cache and Application objects is that the Cache object provides cache-specific features, such as dependencies and expiration policies.
(I) How can get access to cache object?
The Cache object is defined in the 'System.Web.Caching' namespace. You can get a reference to the Cache object by using the Cache property of the Http Context class in the 'System.Web' namespace or by using the Cache property of the Page object.
(A) What are dependencies in cache and types of dependencies?
When you add an item to the cache, you can define dependency relationships that can force that item to be removed from the cache under specific activities of dependencies. Example if the cache object is dependent on file and when the file data changes you want the cache object to be update. Following are the supported dependency:-
-
File dependency: - Allows you to invalidate a specific cache item when a disk based file or files change.
-
Time-based expiration: - Allows you to invalidate a specific cache item depending on predefined time.
-
Key dependency:- Allows you to invalidate a specific cache item depending when another cached item changes.
(A) Can you show a simple code showing file dependency in cache?
Partial Class Default_aspx
Public Sub display Announcement()
Dim announcement As String
If Cache("'announcement"') Is Nothing Then
Dim file As New _
System.IO.StreamReader _
(Server.MapPath("'announcement.txt"'))
announcement = file.ReadToEnd
file. Close()
Dim depends As New _
System.Web.Caching.CacheDependency _
(Server.MapPath("'announcement.txt"'))
Cache.Insert("'announcement"', announcement, depends)
End If
Response.Write(CType(Cache("'announcement"'), String))
End Sub
Private Sub Page_Init(ByVal sender As Object, By Val e As System.EventArgs) Handles Me. nit
display Announcement()
End Sub
End Class
Note:- Above source code can be obtained from CD in "'CacheSample"' folder. "'Announcement.txt"' is in the same folder which you can play around to see the results.
Above given method display Announcement() displays banner text from Announcement.txt file which is lying in application path of the web directory. Above method, first checks whether the Cache object is nothing, if the cache object is nothing then it moves further to load the cache data from the file. Whenever the file data changes the cache object is removed and set to nothing.
(A) What is Cache Callback in Cache?
Cache object is dependent on its dependencies example file based, time based etc...Cache items remove the object when cache dependencies change.ASP.NET provides capability to execute a callback method when that item is removed from cache.
(A) What is scavenging?
When server running your ASP. NET application runs low on memory resources, items are removed from cache depending on cache item priority. Cache item priority is set when you add item to cache. By setting the cache item priority controls, the items scavenging are removed according to priority.
(B) What are different types of caching using cache object of ASP. NET?
You can use two types of output caching to cache information that is to be transmitted to and displayed in a Web browser:
-
Page Output Caching
Page output caching adds the response of page to cache object. Later when page is requested page is displayed from cache rather than creating the page object and displaying it. Page output caching is good if the site is fairly static. -
Page Fragment Caching
If parts of the page are changing, you can wrap the static sections as user controls and cache the user controls using page fragment caching.
(B) How can you cache different version of same page using ASP. NET cache object?
Output cache functionality is achieved by using "'OutputCache"' attribute on ASP. NET page header. Below is the syntax
<%@ Output Cache Duration="20" Location="Server" Vary By Param="state" Vary By Custom="minor version" Vary By Header="Accept-Language"%>
-
Vary By Param: - Caches different version depending on input parameters send through HTTP POST/GET.
-
Vary By Header: - Caches different version depending on the contents of the page header.
-
Vary By Custom: -Lets you customize the way the cache handles page variations by declaring the attribute and overriding the Get Vary By Custom String handler.
-
Vary By Control: -Caches different versions of a user control based on the value of properties of ASP objects in the control.
(A) How will implement Page Fragment Caching?
Page fragment caching involves the caching of a fragment of the page, rather than the entire page. When portions of the page are need to be dynamically created for each user request this is best method as compared to page caching. You can wrap Web Forms user control and cache the control so that these portions of the page do not need to be recreated each time.
(B) Can you compare ASP. NET sessions with classic ASP?
ASP. NET session caches per user session state. It basically uses "'HttpSessionState"' class.
Following are the limitations in classic ASP sessions:-
-
ASP session state is dependent on IIS process very heavily. So if IIS restarts ASP session variables are also recycled.ASP.NET session can be independent of the hosting environment thus ASP. NET session can be maintained even if IIS reboots.
-
ASP session state has no inherent solution to work with Web Farms.ASP.NET session can be stored in state server and SQL SERVER which can support multiple server.
-
ASP session only functions when browser supports cookies.ASP.NET session can be used with browser side cookies or independent of it.
(B) Which are the various modes of storing ASP.NET session?
-
In Proc: - In this mode Session, state is stored in the memory space of the Aspnet_wp.exe process. This is the default setting. If the IIS reboots or web application restarts then session state is lost.
-
State Server:-In this mode Session state is serialized and stored in a separate process (Aspnet_state.exe); therefore, the state can be stored on a separate computer (a state server).
-
SQL SERVER: - In this mode Session, state is serialized and stored in a SQL Server database.
Session state can be specified in <session State> element of application configuration file. Using State Server and SQL SERVER session state can be shared across web farms but note this comes at speed cost as ASP. NET needs to serialize and desterilize data over network repeatedly.
(A) Is Session End event supported in all session modes?
Session End event occurs only in "'Inproc mode"'. "'State Server"' and "'SQL SERVER"' do not have Session End event.
(A) What are the steps to configure State Server Mode?
Following are the things to remember so that State Server Mode works properly:-
-
State Server mode session data is stored in a different process so you must ensure that your objects are serializable.
-
<machine Key> elements in Web.config should be identical across all servers. So this ensures that encryption format is same across all computers.
-
IIS meta base (\LM\W3SVC\2) must be identical across all servers in that farm.
(A) What are the steps to configure SQL Server mode?
Following are the things to remember so that SQL SERVER Mode works properly:-
-
SQL SERVER mode session data is stored in a different process so you must ensure that your objects are serializable.
-
IIS met abase (\LM\W3SVC\2) must be identical across all servers in that farm.
-
By default Session objects are stored in "'Temped"', you can configure it store outside "'TempDB"' by running Microsoft provided SQL script.
Note:- "'TempDB"' database is re-created after SQL SERVER computer reboot.If you want to maintain session state with every reboot best is to run SQL Script and store session objects outside "'TempDB"' database.
(A) Where do you specify session state mode in ASP.NET?
<sessionState mode="'SQLServer"'
stateConnectionString="'tcpip=192.168.1.1:42424"
sqlConnectionString="'data source=192.168.1.1; Integrated Security=SSPI"'
cookieless="'false"'
timeout="'20"/>
Above is sample session state mode specified for SQL SERVER.
(B) What are the other ways you can maintain state?
Other than session variables, you can use the following technique to store state:
-
Hidden fields
-
View state
-
Hidden frames
-
Cookies
-
Query strings

Join the conversation! Your thoughts help the community grow.