Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET Database Programming Weekend Crash Course - J. Butler, T. Caudill.pdf
Скачиваний:
31
Добавлен:
24.05.2014
Размер:
3.32 Mб
Скачать

146

Saturday Afternoon

Using the slidingExpiration parameter

To expire the data from the last time it was accessed, you can use the slidingExpiration parameter as in Listing 14-5:

Listing 14-5 Example of using sliding expiration

<%@ Page Language=”vb” %> <HTML>

<HEAD>

<SCRIPT LANGUAGE=”VB” RUNAT=”server”>

Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs) Dim sMyName as String ‘ Creates a new String Object

Dim dt as DateTime Dim ts as TimeSpan ts.FromSeconds(30)

If Cache.Get(“sMyName”) = Nothing Then

Cache.Insert(“sMyName”, “Tony Caudill”,Nothing,dt.MaxValue,ts) End If

sMyName = Cache.Get(“sMyName”) Response.Write(sMyName)

End Sub </SCRIPT>

</HEAD>

<HTML>

<BODY>

</BODY>

</HTML>

In the code above, we used dt.MaxValue to indicate that there was no absolute expiration, and ts.FromSeconds(30) to indicate that the item should expire on a sliding basis, 30 seconds from the time it was last accessed. This will ensure that the data remains cached once every 30 seconds as long as the page is accessed.

File and Key Dependency and Scavenging

File and Key Dependency-based caching are most likely to be used when you are storing global configuration information in an XML or other physical file that is read from very frequently but changes infrequently. You can create a file dependency that initiates a cache update whenever the date/time stamp on the file is updated.

When inserting an object into cache you can use the optional methods

CacheItemPriority, CacheItemPriorityDecay, and CacheItemRemovedCallback to build rules for scavenging the expiration of cached items. By setting the CacheItemPriority, you are setting the relative cost of the object, as expressed by the CacheItemPriority enumerator. This value is used by the cache when it evicts objects. All things being equal, objects with a lower cost are removed from the cache before objects with a higher cost,

in order to save memory or other application resources. By setting a cached items CacheItemPriority value to a selection such as AboveNormal, the cache will remove these items only after lower priority items have been removed. This provides you very granular, rule-based control in sophisticated caching scenarios. An example of using these