Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [375]

By Root 5461 0
it back to clients for the specified time. For pages that don’t get stale quickly, page-output caching is a kind of free performance booster.

In this chapter, I’ll cover the aspects of caching in a single server as well as caching in a distributed scenario.

Caching Application Data


Centered on the Cache object, the ASP.NET caching API is much more than simply a container of global data shared by all sessions, such as the Application object that I briefly discussed in the previous chapter. The Application object, by the way, is preserved only for backward compatibility with legacy applications. The Cache object is a smarter and thread-safe container that can automatically remove unused items, support various forms of dependencies, and optionally provide removal callbacks and priorities. New ASP.NET applications should use the Cache object and seriously consider the AppFabric Caching services if strong scalability is needed.

The Cache Class


The Cache class is defined in the System.Web.Caching namespace. The current instance of the application’s ASP.NET cache is returned by the Cache property of the HttpContext object or the Cache property of the Page object.

Fundamental Aspects of the Cache Object


The Cache object is unique in its capability to automatically scavenge the memory and get rid of unused items. Cached items can be prioritized and associated with various types of dependencies, such as disk files, other cached items, and database tables. When any of these items change, the cached item is automatically invalidated and removed. Aside from that, the Cache object provides the same dictionary-based and familiar programming interface as Session. Unlike Session, however, the Cache object does not store data on a per-user basis. Furthermore, when the session state is managed in-process, all currently running sessions are stored as distinct items in the ASP.NET Cache.

Keep in mind that an instance of the Cache class is created on a per-AppDomain basis and remains valid as long as that AppDomain is up and running. If you’re looking for a global repository object that, like Session, works across a Web farm or Web garden architecture, the Cache object is not for you. You have to resort to AppFabric Caching services or to some commercial frameworks (for example, ScaleOut or NCache) or open-source frameworks (for example, Memcached or SharedCache).

Properties of the Cache Class


The Cache class provides a few properties and public fields. Table 18-1 lists and describes them all.

Table 18-1. Cache Class Properties and Public Fields

Property

Description

Count

Gets the number of items stored in the cache.

EffectivePercentagePhysicalMemoryLimit

Gets the maximum percentage of memory that can be used before the scavenging process starts. The default value is 97.

EffectivePrivateBytesLimit

Returns the bytes of memory available to the cache.

Item

An indexer property that provides access to the cache item identified by the specified key.

NoAbsoluteExpiration

A static constant that indicates a given item will never expire.

NoSlidingExpiration

A static constant that indicates sliding expiration is disabled for a given item.

The NoAbsoluteExpiration field is of the DateTime type and is set to the DateTime.MaxValue date—that is, the largest possible date defined in the Microsoft .NET Framework. The NoSlidingExpiration field is of the TimeSpan type and is set to TimeSpan.Zero, meaning that sliding expiration is disabled. I’ll say more about sliding expiration shortly.

The Item property is a read/write property that can also be used to add new items to the cache. If the key specified as the argument of the Item property does not exist, a new entry is created. Otherwise, the existing entry is overwritten.

Cache["MyItem"] = value;

The data stored in the cache is generically considered to be of type object, whereas the key must be a case-sensitive string. When you insert a new item in the cache using the Item property, a number of default attributes are assumed. In particular,

Return Main Page Previous Page Next Page

®Online Book Reader